def test_article_field(self): """Should be able to create article field""" testfield = ArticleField('Title', many=True) article_1 = DispatchTestHelpers.create_article( self.client, headline='Test headline 1', slug='test-article-1') article_2 = DispatchTestHelpers.create_article( self.client, headline='Test headline 2', slug='test-article-2') data = [article_1.data['id'], article_2.data['id']] try: testfield.validate(data) except InvalidField: self.fail( 'Field data is valid, exception should not have been thrown') json = testfield.to_json(data) # Test some example entries self.assertEqual(json[0]['id'], 1) self.assertEqual(json[1]['id'], 2) self.assertEqual(json[0]['headline'], u'Test headline 1') self.assertEqual(json[1]['headline'], u'Test headline 2')
def test_article_doesnt_exist(self): """Test the case where an article id for an article that doesn't exist is passed as data""" testfield = ArticleField('Title') id = -1 try: testfield.get_single(id) self.fail('Field data is invalid, exception should have been thrown') except Article.DoesNotExist: pass
def test_article_singular_data(self): """Test the case where ArticleField is initialized with many, but given 1 piece of data""" testfield = ArticleField('Title', many=True) article_1 = DispatchTestHelpers.create_article(self.client, headline='Test headline 1', slug='test-article-1') data = article_1.data['id'] try: testfield.validate(data) self.fail('Field data is invalid, exception should have been thrown') except InvalidField: pass
def test_article_false_many(self): """Test the case where many is false when you have more than 1 article""" testfield = ArticleField('Title') article_1 = DispatchTestHelpers.create_article(self.client, headline='Test headline 1', slug='test-article-1') article_2 = DispatchTestHelpers.create_article(self.client, headline='Test headline 2', slug='test-article-2') data = [article_1.data['id'], article_2.data['id']] try: testfield.validate(data) self.fail('Field data is invalid, exception should have been thrown') except InvalidField: pass
class OneYearLater(Template): id = 'one-year-later' name = 'Feature: One Year Later' title = CharField('Title') subtitle = CharField('Subtitle') snippet = TextField('Snippet') video_src = CharField('Video Source File') next_article = ArticleField('Preview Article') next_title = CharField('Preview Title') next_snippet = TextField('Preview Snippet') article_then = ArticleField('Then Article') article_now = ArticleField('Now Article') article_next = ArticleField('Next Article') about = TextField('About')
class FeaturedArticle(Widget): id = 'featured-article' name = 'Featured Article' zones = (HomepageZone, ) template = 'components/article-banner.html' article = ArticleField('Article')
def test_article_prepare_data(self): """Should be able to return prepared data for the template""" testfield = ArticleField('Title', many=True) article_1 = DispatchTestHelpers.create_article(self.client, headline='Test headline 1', slug='test-article-1') article_2 = DispatchTestHelpers.create_article(self.client, headline='Test headline 2', slug='test-article-2') data = [article_1.data['id'], article_2.data['id']] try: testfield.validate(data) except InvalidField: self.fail('Field data is valid, exception should not have been thrown') result = testfield.prepare_data(data) self.assertEqual(result[0].title, article_1.data['headline']) self.assertEqual(result[1].title, article_2.data['headline'])
def test_article_single_id(self): """Should be able to create article field with only 1 id""" testfield = ArticleField('Title') article = DispatchTestHelpers.create_article(self.client) data = article.data['id'] try: testfield.validate(data) except InvalidField: self.fail('Field data is valid, exception should not have been thrown') json = testfield.to_json(data) # Test some example entries self.assertEqual(json['id'], 1) self.assertEqual(json['headline'], 'Test headline')
class TestWidgetR(Widget): id = 'test-widget-r' name = 'Test Widget R' template = 'widget/test-widget.html' zones = [TestZone] title = CharField('Title', required=True) description = TextField('Description', required=True) article = ArticleField('Featured article', required=True) image = ImageField('Featured image', required=True) widget = WidgetField('Featured Widget', [TestWidgetSub], required=True)
class TestWidget2(Widget): id = 'test-widget-2' name = 'Test widget 2' template = 'widgets/test-widget.html' zones = [TestZone] title = CharField('Title 2') description = TextField('Description 2') article = ArticleField('Featured article 2') image = ImageField('Featured image 2') widget = WidgetField('Featured Widget 2', [TestWidgetSub])
class FoodInsecurity(Template): id = 'food-insecurity' name = 'Feature: Food Insecurity' title = CharField('Title') subtitle = CharField('Subtitle') snippet = TextField('Snippet') video_src = CharField('Video Source File') next_article = ArticleField('Preview Article') next_title = CharField('Preview Title') next_snippet = TextField('Preview Snippet') article_first = ArticleField('First Article') article_first_preview = CharField('First Article Preview') article_second = ArticleField('Second Article') article_second_preview = CharField('Second Article Preview') article_third = ArticleField('Third Article') article_third_preview = CharField('Third Article Preview') about = TextField('About')
class TestWidget(Widget): id = 'test-widget' name = 'Test widget' template = 'widgets/test-widget.html' accepted_keywords = ('extra', ) zones = [TestZone] title = CharField('Title') description = TextField('Description') article = ArticleField('Featured article') image = ImageField('Featured image')
def test_article_to_json_no_data(self): """Passing data=None to to_json returns None""" testfield = ArticleField('Title') data = None self.assertEqual(testfield.to_json(data), None) testfield = ArticleField('Title', many=True) data = None self.assertEqual(testfield.to_json(data), [])