I'm attempting to write unit tests for an article system so I want several articles published on different dates to test that the correct number of articles are displayed. The trouble is it seems like only 1 object is created despite me calling the factory 3 times.
factories/article.py;
class PublishedArticleFactory(DjangoModelFactory):
"""
Initial published article factory.
"""
title = u'Test Article'
tags = u'tag1, tag2'
content = u'This is an article and I\'m not at all imaginative.'
author = factory.SubFactory(StandardUserFactory)
published = True
published_timestamp = str_now()
edited_timestamp = None
slug = factory.lazy_attribute(lambda a: slugify(a.title))
class Meta:
model = 'articles.Article'
django_get_or_create = ('slug', )
tests/test_views.py
class TestArticlesViews(TestCase):
def setUp(self):
# Every test needs a client.
self.client = Client()
self.article1 = PublishedArticleFactory.create(
published_timestamp=str(
datetime.datetime.strptime('10/01/2015', '%d/%m/%Y')
)
)
self.article2 = PublishedArticleFactory.create(
published_timestamp=str(
datetime.datetime.strptime('15/01/2015', '%d/%m/%Y')
)
)
self.article3 = PublishedArticleFactory.create(
published_timestamp=str(
datetime.datetime.strptime('01/03/2015', '%d/%m/%Y')
)
)
def test_calendar(self):
# Issue a GET request.
response = self.client.get('/articles/2015/')
# Check that the response is 200 OK.
self.assertEqual(response.status_code, 200)
print '\ncontext: ', response.context['object_list'], '\n'
print '\narticle: ', self.article1.published_timestamp, '\n'
print '\narticle: ', self.article2.published_timestamp, '\n'
print '\narticle: ', self.article3.published_timestamp, '\n'
This test should render all articles published in 2015, but instead I just get a single object;
context: [<Article: Article object>]
article: 2015-01-10 00:00:00
article: 2015-01-10 00:00:00+00:00
article: 2015-01-10 00:00:00+00:00
It seems that article2 and article3 aren't created as separate objects, where am I going wrong?
Aucun commentaire:
Enregistrer un commentaire