Commit a5b7c298 authored by Adrian Holovaty's avatar Adrian Holovaty
Browse files

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

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
parent 55e453a0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -198,7 +198,7 @@ class ForeignRelatedObjectsDescriptor(object):
                            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):
+28 −27
Original line number Diff line number Diff line
@@ -10,8 +10,9 @@ 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.
@@ -41,32 +42,32 @@ datetime.datetime(2005, 7, 28, 0, 0)

# 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 @@ DoesNotExist: Article matching query does not exist.
# 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 @@ datetime.datetime(2005, 7, 28, 0, 0)
>>> 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 @@ datetime.datetime(2005, 7, 28, 0, 0)

# 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 @@ AttributeError: Manager isn't accessible via Article instances

# 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>]

"""

+1 −1
Original line number Diff line number Diff line
@@ -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 = """
+4 −4
Original line number Diff line number Diff line
@@ -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 @@ API_TESTS = """
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):
+8 −8
Original line number Diff line number Diff line
@@ -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 @@ API_TESTS = """
>>> 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 @@ AttributeError: type object 'Book' has no attribute 'objects'
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>]
"""
Loading