Tests for StreamBlock

This commit is contained in:
Karl Hobley 2015-02-05 16:04:02 +00:00
parent 144a130e44
commit 863cffeb36

View file

@ -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('<input id="links-0-value-link" name="links-0-value-link" type="url" value="http://www.wagtail.io" />', html)
self.assertIn('<input id="links-1-value-title" name="links-1-value-title" type="text" value="Django" />', html)
self.assertIn('<input id="links-1-value-link" name="links-1-value-link" type="url" value="http://www.djangoproject.com" />', 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('<div class="block-heading">My title</div>', html)
self.assertIn('<div class="block-paragraph">My first paragraph</div>', html)
self.assertIn('<div class="block-paragraph">My second paragraph</div>', 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('<div class="sequence">', html)
def test_render_form_count_field(self):
html = self.render_form()
self.assertIn('<input type="hidden" name="myarticle-count" id="myarticle-count" value="3">', html)
def test_render_form_delete_field(self):
html = self.render_form()
self.assertIn('<input type="hidden" id="myarticle-0-deleted" name="myarticle-0-deleted" value="">', html)
def test_render_form_order_fields(self):
html = self.render_form()
self.assertIn('<input type="hidden" id="myarticle-0-order" name="myarticle-0-order" value="0">', html)
self.assertIn('<input type="hidden" id="myarticle-1-order" name="myarticle-1-order" value="1">', html)
self.assertIn('<input type="hidden" id="myarticle-2-order" name="myarticle-2-order" value="2">', html)
def test_render_form_type_fields(self):
html = self.render_form()
self.assertIn('<input type="hidden" id="myarticle-0-type" name="myarticle-0-type" value="heading">', html)
self.assertIn('<input type="hidden" id="myarticle-1-type" name="myarticle-1-type" value="paragraph">', html)
self.assertIn('<input type="hidden" id="myarticle-2-type" name="myarticle-2-type" value="paragraph">', html)
def test_render_form_value_fields(self):
html = self.render_form()
self.assertIn('<input id="myarticle-0-value" name="myarticle-0-value" type="text" value="My title" />', html)
self.assertIn('<input id="myarticle-1-value" name="myarticle-1-value" type="text" value="My first paragraph" />', html)
self.assertIn('<input id="myarticle-2-value" name="myarticle-2-value" type="text" value="My second paragraph" />', html)