Skip to content

Add demo tests for Wagtail bakery demo project #534 #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
167 changes: 167 additions & 0 deletions bakerydemo/base/tests/test_blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
from django.test import SimpleTestCase, TestCase
from wagtail.models import Page
from wagtail.rich_text import RichText
from wagtail.test.utils import WagtailTestUtils
from wagtail.test.utils.form_data import nested_form_data, rich_text, streamfield

from bakerydemo.base.blocks import (
BaseStreamBlock,
BlockQuote,
CaptionedImageBlock,
HeadingBlock,
)


class HeadingBlockTest(TestCase):
"""
Test for the HeadingBlock
"""
def test_heading_block_renders(self):
block = HeadingBlock()
html = block.render({
'heading_text': 'This is a test heading',
'size': 'h2',
})
self.assertIn('<h2>This is a test heading</h2>', html)

def test_heading_block_is_valid(self):
block = HeadingBlock()
# Should be valid with size
self.assertTrue(block.clean({
'heading_text': 'Test Heading',
'size': 'h2'
}))

# Should be valid without size (it's optional)
self.assertTrue(block.clean({
'heading_text': 'Test Heading',
'size': ''
}))

# Should raise an error if heading_text is empty
with self.assertRaises(Exception):
block.clean({
'heading_text': '',
'size': 'h2'
})


class BlockQuoteTest(SimpleTestCase):
"""
Test for the BlockQuote
"""
def test_blockquote_renders(self):
block = BlockQuote()
html = block.render({
'text': 'This is a test quote',
'attribute_name': 'Test Author',
})
self.assertIn('<blockquote>', html)
self.assertIn('This is a test quote', html)
self.assertIn('Test Author', html)

def test_blockquote_renders_without_attribution(self):
block = BlockQuote()
html = block.render({
'text': 'This is a test quote',
'attribute_name': '',
})
self.assertIn('<blockquote>', html)
self.assertIn('This is a test quote', html)
# Attribution element should not be present
self.assertNotIn('class="attribution"', html)

def test_blockquote_clean(self):
block = BlockQuote()

# Basic validation
self.assertTrue(block.clean({
'text': 'Test quote',
'attribute_name': 'Test Author'
}))

# Valid without attribution (optional)
self.assertTrue(block.clean({
'text': 'Test quote',
'attribute_name': ''
}))

# Should raise an error if text is empty
with self.assertRaises(Exception):
block.clean({
'text': '',
'attribute_name': 'Author'
})


class BaseStreamBlockTest(TestCase, WagtailTestUtils):
"""
Test for the BaseStreamBlock which contains all of the other blocks
"""
def test_stream_block_to_python(self):
"""Test that StreamField blocks can be converted from Python representation"""
block = BaseStreamBlock()
value = block.to_python([
{
'type': 'heading_block',
'value': {
'heading_text': 'Test Heading',
'size': 'h2',
},
},
{
'type': 'paragraph_block',
'value': '<p>This is a test paragraph with <b>bold text</b>.</p>',
},
{
'type': 'block_quote',
'value': {
'text': 'Test quote',
'attribute_name': 'Test Author',
},
},
])

# Check the value was correctly parsed
self.assertEqual(len(value), 3)
self.assertEqual(value[0].block_type, 'heading_block')
self.assertEqual(value[0].value['heading_text'], 'Test Heading')
self.assertEqual(value[0].value['size'], 'h2')

# Second block should be a paragraph
self.assertEqual(value[1].block_type, 'paragraph_block')

# Third block should be a block quote
self.assertEqual(value[2].block_type, 'block_quote')
self.assertEqual(value[2].value['text'], 'Test quote')
self.assertEqual(value[2].value['attribute_name'], 'Test Author')

def test_streamfield_form_representation(self):
"""
Test the nested form data utility with StreamField data
"""
# This demonstrates how to create the POST data for a form containing a StreamField
post_data = nested_form_data({
'title': 'Test Page',
'body': streamfield([
('heading_block', {
'heading_text': 'Test Heading',
'size': 'h2',
}),
('paragraph_block', rich_text('<p>Test paragraph</p>')),
]),
})

# The result should look like this structure
self.assertIn('body-count', post_data)
self.assertEqual(post_data['body-count'], '2')

# Check the heading block is correctly represented
self.assertEqual(post_data['body-0-type'], 'heading_block')
self.assertEqual(post_data['body-0-value-heading_text'], 'Test Heading')
self.assertEqual(post_data['body-0-value-size'], 'h2')

# Check the paragraph block is correctly represented - it's now stored as JSON, not plain text
self.assertEqual(post_data['body-1-type'], 'paragraph_block')
# Just check that it contains the paragraph text rather than an exact match
self.assertIn('Test paragraph', post_data['body-1-value'])
150 changes: 150 additions & 0 deletions bakerydemo/base/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from django.test import TestCase
from wagtail.models import Page, Site
from wagtail.test.utils import WagtailPageTestCase

from bakerydemo.base.models import FormPage, HomePage, FormField


class FormPageTest(WagtailPageTestCase):
"""
Test FormPage functionality
"""
@classmethod
def setUpTestData(cls):
# Get root page
cls.root = Page.get_first_root_node()

# Create a site
Site.objects.create(
hostname="testserver",
root_page=cls.root,
is_default_site=True,
site_name="testserver",
)

# Create a homepage with required fields
cls.home = HomePage(
title="Home",
hero_text="Welcome to the Bakery",
hero_cta="Learn More",
)
cls.root.add_child(instance=cls.home)

# Create a form page
cls.form_page = FormPage(
title="Contact Us",
thank_you_text="<p>Thank you for your submission!</p>",
)
cls.home.add_child(instance=cls.form_page)

# Add some form fields
cls.form_field_name = FormField.objects.create(
page=cls.form_page,
sort_order=1,
label="Your Name",
field_type="singleline",
required=True,
clean_name="your_name",
)

cls.form_field_email = FormField.objects.create(
page=cls.form_page,
sort_order=2,
label="Your Email",
field_type="email",
required=True,
clean_name="your_email",
)

cls.form_field_message = FormField.objects.create(
page=cls.form_page,
sort_order=3,
label="Your Message",
field_type="multiline",
required=True,
clean_name="your_message",
)

def test_form_field_creation(self):
"""Test that form fields are created correctly"""
form_fields = FormField.objects.filter(page=self.form_page)
self.assertEqual(form_fields.count(), 3)

# Check field properties
name_field = form_fields.get(label="Your Name")
self.assertEqual(name_field.field_type, "singleline")
self.assertTrue(name_field.required)

email_field = form_fields.get(label="Your Email")
self.assertEqual(email_field.field_type, "email")
self.assertTrue(email_field.required)

message_field = form_fields.get(label="Your Message")
self.assertEqual(message_field.field_type, "multiline")
self.assertTrue(message_field.required)

def test_form_submission_create(self):
"""Test creating a form submission directly"""
# Create a submission
form_data = {
'your_name': 'Test User',
'your_email': '[email protected]',
'your_message': 'This is a test message',
}

submission_class = self.form_page.get_submission_class()

# Create a submission
submission = submission_class.objects.create(
page=self.form_page,
form_data=form_data,
)

# Check it was saved
self.assertEqual(submission_class.objects.count(), 1)

# Check the data was saved correctly
saved_submission = submission_class.objects.first()
submission_data = saved_submission.get_data()
self.assertEqual(submission_data['your_name'], 'Test User')
self.assertEqual(submission_data['your_email'], '[email protected]')
self.assertEqual(submission_data['your_message'], 'This is a test message')


class ContactFormTest(TestCase):
"""
Test suite for ContactForm functionality including:
- Form validation
- Field requirements
- Data processing
- Form submission handling
"""
def test_contact_form_submission(self):
"""
Verify that contact form submission:
- Accepts valid form data
- Processes all required fields correctly
- Handles optional fields appropriately
- Returns expected response
"""
# ... existing code ...

def test_contact_form_validation(self):
"""
Verify that form validation:
- Rejects missing required fields
- Validates email format
- Enforces field length limits
- Provides appropriate error messages
"""
# ... existing code ...

def test_contact_form_optional_fields(self):
"""
Verify that optional fields:
- Can be left empty
- Are processed correctly when provided
- Don't affect form validation
- Are stored properly when submitted
"""
# ... existing code ...
39 changes: 39 additions & 0 deletions bakerydemo/base/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.test import TestCase

class HomePageTest(TestCase):
"""
Test suite for HomePage model functionality including:
- Page creation and hierarchy
- Content management
- Template rendering
- Child page relationships
"""
def test_home_page_creation(self):
"""
Verify that HomePage:
- Can be created with required fields
- Inherits correct page type
- Has proper template assignment
- Maintains correct page hierarchy
"""
# ... existing code ...

def test_home_page_content(self):
"""
Verify that HomePage content:
- Can be updated and retrieved
- Maintains content integrity
- Renders correctly in templates
- Handles rich text fields properly
"""
# ... existing code ...

def test_home_page_children(self):
"""
Verify that HomePage child relationships:
- Can have child pages added
- Maintain correct parent-child relationships
- Support proper page ordering
- Handle child page types correctly
"""
# ... existing code ...
Empty file.
Loading