Converted test fixtures to setUpTestData methods

This commit is contained in:
Josh Smeaton 2015-02-23 11:53:57 +11:00
parent d6969abf23
commit 39a7eed1bb
69 changed files with 2077 additions and 2819 deletions

View file

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import datetime
@ -7,7 +8,7 @@ from django.test import TestCase, override_settings, skipUnlessDBFeature
from django.test.utils import requires_tz_support
from django.utils import timezone
from .models import Book, BookSigning
from .models import Artist, Author, Book, BookSigning, Page
def _make_books(n, base_date):
@ -19,9 +20,25 @@ def _make_books(n, base_date):
pubdate=base_date - datetime.timedelta(days=i))
class TestDataMixin(object):
@classmethod
def setUpTestData(cls):
cls.artist1 = Artist.objects.create(name='Rene Magritte')
cls.author1 = Author.objects.create(name='Roberto Bolaño', slug='roberto-bolano')
cls.author2 = Author.objects.create(name='Scott Rosenberg', slug='scott-rosenberg')
cls.book1 = Book.objects.create(name='2066', slug='2066', pages=800, pubdate=datetime.date(2008, 10, 1))
cls.book1.authors.add(cls.author1)
cls.book2 = Book.objects.create(
name='Dreaming in Code', slug='dreaming-in-code', pages=300, pubdate=datetime.date(2006, 5, 1)
)
cls.page1 = Page.objects.create(
content='I was once bitten by a moose.', template='generic_views/page_template.html'
)
@override_settings(ROOT_URLCONF='generic_views.urls')
class ArchiveIndexViewTests(TestCase):
fixtures = ['generic-views-test-data.json']
class ArchiveIndexViewTests(TestDataMixin, TestCase):
def test_archive_view(self):
res = self.client.get('/dates/books/')
@ -138,8 +155,7 @@ class ArchiveIndexViewTests(TestCase):
@override_settings(ROOT_URLCONF='generic_views.urls')
class YearArchiveViewTests(TestCase):
fixtures = ['generic-views-test-data.json']
class YearArchiveViewTests(TestDataMixin, TestCase):
def test_year_view(self):
res = self.client.get('/dates/books/2008/')
@ -243,8 +259,7 @@ class YearArchiveViewTests(TestCase):
@override_settings(ROOT_URLCONF='generic_views.urls')
class MonthArchiveViewTests(TestCase):
fixtures = ['generic-views-test-data.json']
class MonthArchiveViewTests(TestDataMixin, TestCase):
def test_month_view(self):
res = self.client.get('/dates/books/2008/oct/')
@ -370,8 +385,7 @@ class MonthArchiveViewTests(TestCase):
@override_settings(ROOT_URLCONF='generic_views.urls')
class WeekArchiveViewTests(TestCase):
fixtures = ['generic-views-test-data.json']
class WeekArchiveViewTests(TestDataMixin, TestCase):
def test_week_view(self):
res = self.client.get('/dates/books/2008/week/39/')
@ -467,8 +481,7 @@ class WeekArchiveViewTests(TestCase):
@override_settings(ROOT_URLCONF='generic_views.urls')
class DayArchiveViewTests(TestCase):
fixtures = ['generic-views-test-data.json']
class DayArchiveViewTests(TestDataMixin, TestCase):
def test_day_view(self):
res = self.client.get('/dates/books/2008/oct/01/')
@ -585,14 +598,13 @@ class DayArchiveViewTests(TestCase):
@override_settings(ROOT_URLCONF='generic_views.urls')
class DateDetailViewTests(TestCase):
fixtures = ['generic-views-test-data.json']
class DateDetailViewTests(TestDataMixin, TestCase):
def test_date_detail_by_pk(self):
res = self.client.get('/dates/books/2008/oct/01/1/')
res = self.client.get('/dates/books/2008/oct/01/%s/' % self.book1.pk)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.context['object'], Book.objects.get(pk=1))
self.assertEqual(res.context['book'], Book.objects.get(pk=1))
self.assertEqual(res.context['object'], self.book1)
self.assertEqual(res.context['book'], self.book1)
self.assertTemplateUsed(res, 'generic_views/book_detail.html')
def test_date_detail_by_slug(self):
@ -601,9 +613,9 @@ class DateDetailViewTests(TestCase):
self.assertEqual(res.context['book'], Book.objects.get(slug='dreaming-in-code'))
def test_date_detail_custom_month_format(self):
res = self.client.get('/dates/books/2008/10/01/1/')
res = self.client.get('/dates/books/2008/10/01/%s/' % self.book1.pk)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.context['book'], Book.objects.get(pk=1))
self.assertEqual(res.context['book'], self.book1)
def test_date_detail_allow_future(self):
future = (datetime.date.today() + datetime.timedelta(days=60))
@ -628,14 +640,14 @@ class DateDetailViewTests(TestCase):
Refs #16918.
"""
res = self.client.get(
'/dates/books/get_object_custom_queryset/2006/may/01/2/')
'/dates/books/get_object_custom_queryset/2006/may/01/%s/' % self.book2.pk)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.context['object'], Book.objects.get(pk=2))
self.assertEqual(res.context['book'], Book.objects.get(pk=2))
self.assertEqual(res.context['object'], self.book2)
self.assertEqual(res.context['book'], self.book2)
self.assertTemplateUsed(res, 'generic_views/book_detail.html')
res = self.client.get(
'/dates/books/get_object_custom_queryset/2008/oct/01/1/')
'/dates/books/get_object_custom_queryset/2008/oct/01/9999999/')
self.assertEqual(res.status_code, 404)
def test_get_object_custom_queryset_numqueries(self):