Skip to content

Commit 45d89a2

Browse files
committed
Create test data in specific test cases
1 parent f6223f1 commit 45d89a2

5 files changed

+36
-15
lines changed

bar/blog/tests/base_test_case.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ class BaseBlogTestCase(TestCase):
66

77
def setUp(self):
88
super().setUp()
9-
tags = [Tag.objects.create(name='Foo'), Tag.objects.create(name='Bar'), Tag.objects.create(name='Spam')]
10-
self.authors = []
11-
for author_num in range(5):
12-
author = Author.objects.create(
13-
bio='Bio #{}'.format(author_num), email='author{}@e.co'.format(author_num),
14-
username='author-{}'.format(author_num))
15-
author.set_password('v3rys3cr31')
16-
for article_num in range(10):
17-
article = Article.objects.create(
18-
title='Article #{}'.format(author_num * 10 + article_num), content='foo bar', author=author,
19-
comments_on=(article_num % 2 == 0))
20-
article.tags.add(*tags)
21-
self.authors.append(author)
9+
10+
def _create_tags(self):
11+
return [Tag.objects.create(name='Foo'), Tag.objects.create(name='Bar'), Tag.objects.create(name='Spam')]
12+
13+
def _create_author(self, username, email, bio, password):
14+
author = Author.objects.create(bio=bio, email=email, username=username)
15+
author.set_password(password)
16+
return author
17+
18+
def _create_article(self, title, content, author, comments_on, tags):
19+
article = Article.objects.create(title=title, content=content, author=author, comments_on=comments_on)
20+
if tags:
21+
article.tags.add(*tags)
22+
return article

bar/blog/tests/tests_articles_list.py

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ def test_foo_bar(self):
1111
def test_url_resolving(self):
1212
self.assertEqual(reverse('articles_list'), '/blog/')
1313

14+
15+
class ArticlesListPaginationTestCase(BaseBlogTestCase):
16+
17+
def setUp(self):
18+
super().setUp()
19+
self.tags = self._create_tags()
20+
for author_num in range(5):
21+
author = self._create_author(
22+
'author-{}'.format(author_num), 'author-{}@e.co'.format(author_num), 'Bio #{}'.format(author_num),
23+
'v3rys3cr31')
24+
for article_num in range(10):
25+
self._create_article(
26+
'Article #{}'.format(author_num*10+article_num), 'foo bar', author, True, self.tags)
27+
1428
def test_first_20_articles_are_on_the_page(self):
1529
resp = self.client.get(reverse('articles_list'))
1630
for i in range(20):

bar/blog/tests/tests_articles_tags.py

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
class ArticlesTagsTestCase(BaseBlogTestCase):
77

8+
def setUp(self):
9+
super().setUp()
10+
tags = self._create_tags()
11+
author = self._create_author('foobar', 'foobar@e.co', 'spam', 'v3rys3cr31')
12+
self._create_article('Spam', 'foo bar', author, True, tags)
13+
814
def test_foo_bar(self):
915
self.assertEqual(3, 2+1)
1016

bar/blog/tests/tests_author_page.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ def test_url_resolving(self):
1212
self.assertEqual(reverse('author_page', kwargs=dict(username='foo-bar')), '/blog/authors/foo-bar')
1313

1414
def test_author_name_on_its_page(self):
15-
author = self.authors[0]
15+
author = self._create_author('foobar', 'foobar@e.co', 'spam', 'v3rys3cr31')
1616
resp = self.client.get(reverse('author_page', kwargs=dict(username=author.username)))
1717
self.assertIn(author.username, str(resp.content))

bar/blog/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ArticlesListView(ListView):
1111
model = Article
1212
context_object_name = 'articles'
1313
paginate_by = 20
14-
queryset = Article.objects.all()
14+
queryset = Article.objects.all().order_by('id')
1515

1616
def get_context_data(self, **kwargs):
1717
context = super().get_context_data(**kwargs)

0 commit comments

Comments
 (0)