Permalink
Browse files

Changed all model unit tests to use __str__() instead of __repr__(). …

…Also slightly changed related-object DoesNotExist exception message to use repr instead of str

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3075 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 55e453a commit a5b7c298164e3c1547988c734d6155c17f57b1d7 @adrianholovaty adrianholovaty committed Jun 4, 2006
@@ -198,7 +198,7 @@ def remove(self, *objs):
setattr(obj, rel_field.name, None)
obj.save()
else:
- raise rel_field.rel.to.DoesNotExist, "'%s' is not related to '%s'." % (obj, instance)
+ raise rel_field.rel.to.DoesNotExist, "%r is not related to %r." % (obj, instance)
remove.alters_data = True
def clear(self):
@@ -712,7 +712,7 @@ def __init__(self, to, num_in_admin=0, related_name=None,
self.related_name = related_name
self.filter_interface = filter_interface
if limit_choices_to is None:
- limit_choices_to = {}
+ limit_choices_to = {}
self.limit_choices_to = limit_choices_to
self.edit_inline = False
self.raw_id_admin = raw_id_admin
@@ -9,9 +9,10 @@
class Article(models.Model):
headline = models.CharField(maxlength=100, default='Default headline')
pub_date = models.DateTimeField()
-
- def __repr__(self):
+
+ def __str__(self):
return self.headline
+
API_TESTS = """
# No articles are in the system yet.
@@ -39,34 +40,34 @@ def __repr__(self):
>>> a.headline = 'Area woman programs in Python'
>>> a.save()
-# Article.objects.all() returns all the articles in the database.
+# Article.objects.all() returns all the articles in the database.
>>> Article.objects.all()
-[Area woman programs in Python]
+[<Article: Area woman programs in Python>]
# Django provides a rich database lookup API.
>>> Article.objects.get(id__exact=1)
-Area woman programs in Python
+<Article: Area woman programs in Python>
>>> Article.objects.get(headline__startswith='Area woman')
-Area woman programs in Python
+<Article: Area woman programs in Python>
>>> Article.objects.get(pub_date__year=2005)
-Area woman programs in Python
+<Article: Area woman programs in Python>
>>> Article.objects.get(pub_date__year=2005, pub_date__month=7)
-Area woman programs in Python
+<Article: Area woman programs in Python>
>>> Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28)
-Area woman programs in Python
+<Article: Area woman programs in Python>
# The "__exact" lookup type can be omitted, as a shortcut.
>>> Article.objects.get(id=1)
-Area woman programs in Python
+<Article: Area woman programs in Python>
>>> Article.objects.get(headline='Area woman programs in Python')
-Area woman programs in Python
+<Article: Area woman programs in Python>
>>> Article.objects.filter(pub_date__year=2005)
-[Area woman programs in Python]
+[<Article: Area woman programs in Python>]
>>> Article.objects.filter(pub_date__year=2004)
[]
>>> Article.objects.filter(pub_date__year=2005, pub_date__month=7)
-[Area woman programs in Python]
+[<Article: Area woman programs in Python>]
# Django raises an Article.DoesNotExist exception for get() if the parameters
# don't match any object.
@@ -84,7 +85,7 @@ def __repr__(self):
# shortcut for primary-key exact lookups.
# The following is identical to articles.get(id=1).
>>> Article.objects.get(pk=1)
-Area woman programs in Python
+<Article: Area woman programs in Python>
# Model instances of the same type and same ID are considered equal.
>>> a = Article.objects.get(pk=1)
@@ -222,7 +223,7 @@ def __repr__(self):
>>> s1 = Article.objects.filter(id__exact=1)
>>> s2 = Article.objects.filter(id__exact=2)
>>> s1 | s2
-[Area woman programs in Python, Second article]
+[<Article: Area woman programs in Python>, <Article: Second article>]
>>> s1 & s2
[]
@@ -232,34 +233,34 @@ def __repr__(self):
# You can get items using index and slice notation.
>>> Article.objects.all()[0]
-Area woman programs in Python
+<Article: Area woman programs in Python>
>>> Article.objects.all()[1:3]
-[Second article, Third article]
+[<Article: Second article>, <Article: Third article>]
>>> s3 = Article.objects.filter(id__exact=3)
>>> (s1 | s2 | s3)[::2]
-[Area woman programs in Python, Third article]
+[<Article: Area woman programs in Python>, <Article: Third article>]
# Slices (without step) are lazy:
>>> Article.objects.all()[0:5].filter()
-[Area woman programs in Python, Second article, Third article, Fourth article, Article 6]
+[<Article: Area woman programs in Python>, <Article: Second article>, <Article: Third article>, <Article: Fourth article>, <Article: Article 6>]
# Slicing again works:
>>> Article.objects.all()[0:5][0:2]
-[Area woman programs in Python, Second article]
+[<Article: Area woman programs in Python>, <Article: Second article>]
>>> Article.objects.all()[0:5][:2]
-[Area woman programs in Python, Second article]
+[<Article: Area woman programs in Python>, <Article: Second article>]
>>> Article.objects.all()[0:5][4:]
-[Article 6]
+[<Article: Article 6>]
>>> Article.objects.all()[0:5][5:]
[]
# Some more tests!
>>> Article.objects.all()[2:][0:2]
-[Third article, Fourth article]
+[<Article: Third article>, <Article: Fourth article>]
>>> Article.objects.all()[2:][:2]
-[Third article, Fourth article]
+[<Article: Third article>, <Article: Fourth article>]
>>> Article.objects.all()[2:][2:3]
-[Article 6]
+[<Article: Article 6>]
# Note that you can't use 'offset' without 'limit' (on some dbs), so this doesn't work:
>>> Article.objects.all()[2:]
@@ -308,10 +309,10 @@ def __repr__(self):
# Bulk delete test: How many objects before and after the delete?
>>> Article.objects.all()
-[Area woman programs in Python, Second article, Third article, Fourth article, Article 6, Default headline, Article 7, Updated article 8]
+[<Article: Area woman programs in Python>, <Article: Second article>, <Article: Third article>, <Article: Fourth article>, <Article: Article 6>, <Article: Default headline>, <Article: Article 7>, <Article: Updated article 8>]
>>> Article.objects.filter(id__lte=4).delete()
>>> Article.objects.all()
-[Article 6, Default headline, Article 7, Updated article 8]
+[<Article: Article 6>, <Article: Default headline>, <Article: Article 7>, <Article: Updated article 8>]
"""
@@ -20,7 +20,7 @@ class Person(models.Model):
name = models.CharField(maxlength=20)
gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)
- def __repr__(self):
+ def __str__(self):
return self.name
API_TESTS = """
@@ -12,7 +12,7 @@ class Person(models.Model):
first_name = models.CharField(maxlength=30, db_column='firstname')
last_name = models.CharField(maxlength=30, db_column='last')
- def __repr__(self):
+ def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
API_TESTS = """
@@ -24,13 +24,13 @@ def __repr__(self):
1
>>> Person.objects.all()
-[John Smith]
+[<Person: John Smith>]
>>> Person.objects.filter(first_name__exact='John')
-[John Smith]
+[<Person: John Smith>]
>>> Person.objects.get(first_name__exact='John')
-John Smith
+<Person: John Smith>
>>> Person.objects.filter(firstname__exact='John')
Traceback (most recent call last):
@@ -23,7 +23,7 @@ class Person(models.Model):
fun = models.BooleanField()
objects = PersonManager()
- def __repr__(self):
+ def __str__(self):
return "%s %s" % (self.first_name, self.last_name)
# An example of a custom manager that sets get_query_set().
@@ -39,7 +39,7 @@ class Book(models.Model):
published_objects = PublishedBookManager()
authors = models.ManyToManyField(Person, related_name='books')
- def __repr__(self):
+ def __str__(self):
return self.title
# An example of providing multiple custom managers.
@@ -55,7 +55,7 @@ class Car(models.Model):
cars = models.Manager()
fast_cars = FastCarManager()
- def __repr__(self):
+ def __str__(self):
return self.name
API_TESTS = """
@@ -64,7 +64,7 @@ def __repr__(self):
>>> p2 = Person(first_name='Droopy', last_name='Dog', fun=False)
>>> p2.save()
>>> Person.objects.get_fun_people()
-[Bugs Bunny]
+[<Person: Bugs Bunny>]
# The RelatedManager used on the 'books' descriptor extends the default manager
>>> from modeltests.custom_managers.models import PublishedBookManager
@@ -89,19 +89,19 @@ def __repr__(self):
True
>>> Book.published_objects.all()
-[How to program]
+[<Book: How to program>]
>>> c1 = Car(name='Corvette', mileage=21, top_speed=180)
>>> c1.save()
>>> c2 = Car(name='Neon', mileage=31, top_speed=100)
>>> c2.save()
>>> Car.cars.order_by('name')
-[Corvette, Neon]
+[<Car: Corvette>, <Car: Neon>]
>>> Car.fast_cars.all()
-[Corvette]
+[<Car: Corvette>]
# Each model class gets a "_default_manager" attribute, which is a reference
# to the first manager defined in the class. In this case, it's "cars".
>>> Car._default_manager.order_by('name')
-[Corvette, Neon]
+[<Car: Corvette>, <Car: Neon>]
"""
@@ -11,16 +11,16 @@ class Article(models.Model):
headline = models.CharField(maxlength=100)
pub_date = models.DateField()
- def __repr__(self):
+ def __str__(self):
return self.headline
def was_published_today(self):
return self.pub_date == datetime.date.today()
- def get_articles_from_same_day_1(self):
+ def articles_from_same_day_1(self):
return Article.objects.filter(pub_date=self.pub_date).exclude(id=self.id)
- def get_articles_from_same_day_2(self):
+ def articles_from_same_day_2(self):
"""
Verbose version of get_articles_from_same_day_1, which does a custom
database query for the sake of demonstration.
@@ -47,12 +47,12 @@ def get_articles_from_same_day_2(self):
# Test the custom methods.
>>> a.was_published_today()
False
->>> a.get_articles_from_same_day_1()
-[Beatles reunite]
->>> a.get_articles_from_same_day_2()
-[Beatles reunite]
->>> b.get_articles_from_same_day_1()
-[Area man programs in Python]
->>> b.get_articles_from_same_day_2()
-[Area man programs in Python]
+>>> a.articles_from_same_day_1()
+[<Article: Beatles reunite>]
+>>> a.articles_from_same_day_2()
+[<Article: Beatles reunite>]
+>>> b.articles_from_same_day_1()
+[<Article: Area man programs in Python>]
+>>> b.articles_from_same_day_2()
+[<Article: Area man programs in Python>]
"""
@@ -14,7 +14,7 @@ class Employee(models.Model):
class Meta:
ordering = ('last_name', 'first_name')
- def __repr__(self):
+ def __str__(self):
return "%s %s" % (self.first_name, self.last_name)
class Business(models.Model):
@@ -23,68 +23,68 @@ class Business(models.Model):
class Meta:
verbose_name_plural = 'businesses'
- def __repr__(self):
+ def __str__(self):
return self.name
API_TESTS = """
>>> dan = Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
>>> dan.save()
>>> Employee.objects.all()
-[Dan Jones]
+[<Employee: Dan Jones>]
>>> fran = Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
>>> fran.save()
>>> Employee.objects.all()
-[Fran Bones, Dan Jones]
+[<Employee: Fran Bones>, <Employee: Dan Jones>]
>>> Employee.objects.get(pk='ABC123')
-Dan Jones
+<Employee: Dan Jones>
>>> Employee.objects.get(pk='XYZ456')
-Fran Bones
+<Employee: Fran Bones>
>>> Employee.objects.get(pk='foo')
Traceback (most recent call last):
...
DoesNotExist: Employee matching query does not exist.
# Use the name of the primary key, rather than pk.
>>> Employee.objects.get(employee_code__exact='ABC123')
-Dan Jones
+<Employee: Dan Jones>
# Fran got married and changed her last name.
>>> fran = Employee.objects.get(pk='XYZ456')
>>> fran.last_name = 'Jones'
>>> fran.save()
>>> Employee.objects.filter(last_name__exact='Jones')
-[Dan Jones, Fran Jones]
+[<Employee: Dan Jones>, <Employee: Fran Jones>]
>>> Employee.objects.in_bulk(['ABC123', 'XYZ456'])
-{'XYZ456': Fran Jones, 'ABC123': Dan Jones}
+{'XYZ456': <Employee: Fran Jones>, 'ABC123': <Employee: Dan Jones>}
>>> b = Business(name='Sears')
>>> b.save()
>>> b.employees.add(dan, fran)
>>> b.employees.all()
-[Dan Jones, Fran Jones]
+[<Employee: Dan Jones>, <Employee: Fran Jones>]
>>> fran.business_set.all()
-[Sears]
+[<Business: Sears>]
>>> Business.objects.in_bulk(['Sears'])
-{'Sears': Sears}
+{'Sears': <Business: Sears>}
>>> Business.objects.filter(name__exact='Sears')
-[Sears]
+[<Business: Sears>]
>>> Business.objects.filter(pk='Sears')
-[Sears]
+[<Business: Sears>]
# Queries across tables, involving primary key
>>> Employee.objects.filter(business__name__exact='Sears')
-[Dan Jones, Fran Jones]
+[<Employee: Dan Jones>, <Employee: Fran Jones>]
>>> Employee.objects.filter(business__pk='Sears')
-[Dan Jones, Fran Jones]
+[<Employee: Dan Jones>, <Employee: Fran Jones>]
>>> Business.objects.filter(employees__employee_code__exact='ABC123')
-[Sears]
+[<Business: Sears>]
>>> Business.objects.filter(employees__pk='ABC123')
-[Sears]
+[<Business: Sears>]
>>> Business.objects.filter(employees__first_name__startswith='Fran')
-[Sears]
+[<Business: Sears>]
"""
Oops, something went wrong. Retry.

0 comments on commit a5b7c29

Please sign in to comment.