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
1 changed files with 15 additions and 23 deletions

View File

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