1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Used setUpTestData() in ordering tests.

This commit is contained in:
Mariusz Felisiak 2016-11-15 14:20:22 +01:00 committed by Tim Graham
parent 6e222dae56
commit 789a02b31e

View File

@ -10,19 +10,17 @@ from .models import Article, Author, Reference
class OrderingTests(TestCase): class OrderingTests(TestCase):
def setUp(self):
self.a1 = Article.objects.create( @classmethod
headline="Article 1", pub_date=datetime(2005, 7, 26) def setUpTestData(cls):
) cls.a1 = Article.objects.create(headline="Article 1", pub_date=datetime(2005, 7, 26))
self.a2 = Article.objects.create( cls.a2 = Article.objects.create(headline="Article 2", pub_date=datetime(2005, 7, 27))
headline="Article 2", pub_date=datetime(2005, 7, 27) cls.a3 = Article.objects.create(headline="Article 3", pub_date=datetime(2005, 7, 27))
) cls.a4 = Article.objects.create(headline="Article 4", pub_date=datetime(2005, 7, 28))
self.a3 = Article.objects.create( cls.author_1 = Author.objects.create()
headline="Article 3", pub_date=datetime(2005, 7, 27) cls.author_2 = Author.objects.create()
) for i in range(2):
self.a4 = Article.objects.create( Author.objects.create()
headline="Article 4", pub_date=datetime(2005, 7, 28)
)
def test_default_ordering(self): def test_default_ordering(self):
""" """
@ -212,16 +210,10 @@ class OrderingTests(TestCase):
""" """
'pk' works as an ordering option in Meta. 'pk' works as an ordering option in Meta.
""" """
Author.objects.create(pk=1)
Author.objects.create(pk=2)
Author.objects.create(pk=3)
Author.objects.create(pk=4)
self.assertQuerysetEqual( self.assertQuerysetEqual(
Author.objects.all(), [ Author.objects.all(),
4, 3, 2, 1 list(reversed(range(1, Author.objects.count() + 1))),
], attrgetter("pk"),
attrgetter("pk")
) )
def test_order_by_fk_attname(self): def test_order_by_fk_attname(self):
@ -230,7 +222,7 @@ class OrderingTests(TestCase):
from inheriting its related model ordering option (#19195). from inheriting its related model ordering option (#19195).
""" """
for i in range(1, 5): for i in range(1, 5):
author = Author.objects.create(pk=i) author = Author.objects.get(pk=i)
article = getattr(self, "a%d" % (5 - i)) article = getattr(self, "a%d" % (5 - i))
article.author = author article.author = author
article.save(update_fields={'author'}) article.save(update_fields={'author'})