From 789a02b31e189b762f35d209006a2f09aaeaa0d2 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 15 Nov 2016 14:20:22 +0100 Subject: [PATCH] Used setUpTestData() in ordering tests. --- tests/ordering/tests.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/tests/ordering/tests.py b/tests/ordering/tests.py index 5e9f6dc986..f19cbdeddc 100644 --- a/tests/ordering/tests.py +++ b/tests/ordering/tests.py @@ -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'})