Skip to content
Permalink
Browse files
Create test data in specific test cases
  • Loading branch information
dizballanze committed May 23, 2018
1 parent f6223f1 commit 45d89a2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
@@ -6,16 +6,17 @@ class BaseBlogTestCase(TestCase):

def setUp(self):
super().setUp()
tags = [Tag.objects.create(name='Foo'), Tag.objects.create(name='Bar'), Tag.objects.create(name='Spam')]
self.authors = []
for author_num in range(5):
author = Author.objects.create(
bio='Bio #{}'.format(author_num), email='author{}@e.co'.format(author_num),
username='author-{}'.format(author_num))
author.set_password('v3rys3cr31')
for article_num in range(10):
article = Article.objects.create(
title='Article #{}'.format(author_num * 10 + article_num), content='foo bar', author=author,
comments_on=(article_num % 2 == 0))
article.tags.add(*tags)
self.authors.append(author)

def _create_tags(self):
return [Tag.objects.create(name='Foo'), Tag.objects.create(name='Bar'), Tag.objects.create(name='Spam')]

def _create_author(self, username, email, bio, password):
author = Author.objects.create(bio=bio, email=email, username=username)
author.set_password(password)
return author

def _create_article(self, title, content, author, comments_on, tags):
article = Article.objects.create(title=title, content=content, author=author, comments_on=comments_on)
if tags:
article.tags.add(*tags)
return article
@@ -11,6 +11,20 @@ def test_foo_bar(self):
def test_url_resolving(self):
self.assertEqual(reverse('articles_list'), '/blog/')


class ArticlesListPaginationTestCase(BaseBlogTestCase):

def setUp(self):
super().setUp()
self.tags = self._create_tags()
for author_num in range(5):
author = self._create_author(
'author-{}'.format(author_num), 'author-{}@e.co'.format(author_num), 'Bio #{}'.format(author_num),
'v3rys3cr31')
for article_num in range(10):
self._create_article(
'Article #{}'.format(author_num*10+article_num), 'foo bar', author, True, self.tags)

def test_first_20_articles_are_on_the_page(self):
resp = self.client.get(reverse('articles_list'))
for i in range(20):
@@ -5,6 +5,12 @@

class ArticlesTagsTestCase(BaseBlogTestCase):

def setUp(self):
super().setUp()
tags = self._create_tags()
author = self._create_author('foobar', 'foobar@e.co', 'spam', 'v3rys3cr31')
self._create_article('Spam', 'foo bar', author, True, tags)

def test_foo_bar(self):
self.assertEqual(3, 2+1)

@@ -12,6 +12,6 @@ def test_url_resolving(self):
self.assertEqual(reverse('author_page', kwargs=dict(username='foo-bar')), '/blog/authors/foo-bar')

def test_author_name_on_its_page(self):
author = self.authors[0]
author = self._create_author('foobar', 'foobar@e.co', 'spam', 'v3rys3cr31')
resp = self.client.get(reverse('author_page', kwargs=dict(username=author.username)))
self.assertIn(author.username, str(resp.content))
@@ -11,7 +11,7 @@ class ArticlesListView(ListView):
model = Article
context_object_name = 'articles'
paginate_by = 20
queryset = Article.objects.all()
queryset = Article.objects.all().order_by('id')

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

0 comments on commit 45d89a2

Please sign in to comment.