From 863cffeb36e7061bb78885f8b2797040103ea7b1 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 5 Feb 2015 16:04:02 +0000 Subject: [PATCH] Tests for StreamBlock --- wagtail/wagtailadmin/tests/test_blocks.py | 160 +++++++++++++++++++++- 1 file changed, 158 insertions(+), 2 deletions(-) diff --git a/wagtail/wagtailadmin/tests/test_blocks.py b/wagtail/wagtailadmin/tests/test_blocks.py index 78c94bea9..2f6acb5da 100644 --- a/wagtail/wagtailadmin/tests/test_blocks.py +++ b/wagtail/wagtailadmin/tests/test_blocks.py @@ -113,8 +113,8 @@ class TestStructBlock(unittest.TestCase): def test_render(self): class LinkBlock(blocks.StructBlock): - title = blocks.FieldBlock(forms.CharField(label="Title")) - link = blocks.FieldBlock(forms.URLField(label="Link")) + title = blocks.FieldBlock(forms.CharField(), label="Title") + link = blocks.FieldBlock(forms.URLField(), label="Link") block = LinkBlock() html = block.render({ @@ -213,3 +213,159 @@ class TestListBlock(unittest.TestCase): self.assertIn('', html) self.assertIn('', html) self.assertIn('', html) + + +class TestStreamBlock(unittest.TestCase): + def test_initialisation(self): + block = blocks.StreamBlock([ + ('heading', blocks.FieldBlock(forms.CharField())), + ('paragraph', blocks.FieldBlock(forms.CharField())), + ]) + + self.assertEqual(list(block.child_blocks.keys()), ['heading', 'paragraph']) + + def test_initialisation_from_subclass(self): + class ArticleBlock(blocks.StreamBlock): + heading = blocks.FieldBlock(forms.CharField()) + paragraph = blocks.FieldBlock(forms.CharField()) + + block = ArticleBlock() + + self.assertEqual(list(block.child_blocks.keys()), ['heading', 'paragraph']) + + def test_initialisation_from_subclass_with_extra(self): + class ArticleBlock(blocks.StreamBlock): + heading = blocks.FieldBlock(forms.CharField()) + paragraph = blocks.FieldBlock(forms.CharField()) + + block = ArticleBlock([ + ('intro', blocks.FieldBlock(forms.CharField())) + ]) + + self.assertEqual(list(block.child_blocks.keys()), ['heading', 'paragraph', 'intro']) + + def test_initialisation_with_multiple_subclassses(self): + class ArticleBlock(blocks.StreamBlock): + heading = blocks.FieldBlock(forms.CharField()) + paragraph = blocks.FieldBlock(forms.CharField()) + + class ArticleWithIntroBlock(ArticleBlock): + intro = blocks.FieldBlock(forms.CharField()) + + block = ArticleWithIntroBlock() + + self.assertEqual(list(block.child_blocks.keys()), ['heading', 'paragraph', 'intro']) + + @unittest.expectedFailure # Field order doesn't match inheritance order + def test_initialisation_with_mixins(self): + class ArticleBlock(blocks.StreamBlock): + heading = blocks.FieldBlock(forms.CharField()) + paragraph = blocks.FieldBlock(forms.CharField()) + + class IntroMixin(blocks.StreamBlock): + intro = blocks.FieldBlock(forms.CharField()) + + class ArticleWithIntroBlock(ArticleBlock, IntroMixin): + pass + + block = ArticleWithIntroBlock() + + self.assertEqual(list(block.child_blocks.keys()), ['heading', 'paragraph', 'intro']) + + def render_article(self, data): + class ArticleBlock(blocks.StreamBlock): + heading = blocks.FieldBlock(forms.CharField()) + paragraph = blocks.FieldBlock(forms.CharField()) + + block = ArticleBlock() + value = block.to_python(data) + + return block.render(value) + + def test_render(self): + html = self.render_article([ + { + 'type': 'heading', + 'value': "My title", + }, + { + 'type': 'paragraph', + 'value': 'My first paragraph', + }, + { + 'type': 'paragraph', + 'value': 'My second paragraph', + }, + ]) + + self.assertIn('
My title
', html) + self.assertIn('
My first paragraph
', html) + self.assertIn('
My second paragraph
', html) + + @unittest.expectedFailure + def test_render_unknown_type(self): + # This can happen if a developer removes a type from their StreamBlock + html = self.render_article([ + { + 'type': 'foo', + 'value': "Hello", + }, + ]) + + def render_form(self): + class ArticleBlock(blocks.StreamBlock): + heading = blocks.FieldBlock(forms.CharField()) + paragraph = blocks.FieldBlock(forms.CharField()) + + block = ArticleBlock() + value = block.to_python([ + { + 'type': 'heading', + 'value': "My title", + }, + { + 'type': 'paragraph', + 'value': 'My first paragraph', + }, + { + 'type': 'paragraph', + 'value': 'My second paragraph', + }, + ]) + return block.render_form(value, prefix='myarticle') + + def test_render_form_wrapper_class(self): + html = self.render_form() + + self.assertIn('
', html) + + def test_render_form_count_field(self): + html = self.render_form() + + self.assertIn('', html) + + def test_render_form_delete_field(self): + html = self.render_form() + + self.assertIn('', html) + + def test_render_form_order_fields(self): + html = self.render_form() + + self.assertIn('', html) + self.assertIn('', html) + self.assertIn('', html) + + def test_render_form_type_fields(self): + html = self.render_form() + + self.assertIn('', html) + self.assertIn('', html) + self.assertIn('', html) + + def test_render_form_value_fields(self): + html = self.render_form() + + self.assertIn('', html) + self.assertIn('', html) + self.assertIn('', html)