""" Test for forms example (10_forms_demo.py). This test ensures the forms example runs correctly and produces expected output. """ import pytest import sys from pathlib import Path import importlib.util # Add examples to path examples_dir = Path(__file__).parent.parent.parent / "examples" sys.path.insert(0, str(examples_dir)) # Load the demo module spec = importlib.util.spec_from_file_location( "forms_demo", examples_dir / "10_forms_demo.py" ) demo_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(demo_module) def test_forms_demo_imports(): """Test that the forms demo can be imported without errors.""" assert demo_module is not None def test_form_submit_callback(): """Test that form submit callbacks work correctly.""" form_submit_callback = demo_module.form_submit_callback demo_module.form_submissions = [] callback = form_submit_callback("test_form") callback({"field1": "value1"}) assert len(demo_module.form_submissions) == 1 assert demo_module.form_submissions[0][0] == "test_form" assert demo_module.form_submissions[0][1] == {"field1": "value1"} def test_create_example_1_text_fields(): """Test creating text fields example.""" create_example_1 = demo_module.create_example_1_text_fields from pyWebLayout.concrete.page import Page page = create_example_1() assert isinstance(page, Page) assert page.size == (500, 600) def test_create_example_2_number_fields(): """Test creating number fields example.""" create_example_2 = demo_module.create_example_2_number_fields from pyWebLayout.concrete.page import Page page = create_example_2() assert isinstance(page, Page) assert page.size == (500, 600) def test_create_example_3_selection_fields(): """Test creating selection fields example.""" create_example_3 = demo_module.create_example_3_selection_fields from pyWebLayout.concrete.page import Page page = create_example_3() assert isinstance(page, Page) assert page.size == (500, 600) def test_create_example_4_complete_form(): """Test creating complete form example.""" create_example_4 = demo_module.create_example_4_complete_form from pyWebLayout.concrete.page import Page page = create_example_4() assert isinstance(page, Page) assert page.size == (500, 700) def test_all_form_field_types(): """Test that all FormFieldType values are demonstrated.""" from pyWebLayout.abstract.functional import FormFieldType # All field types that should be in examples expected_types = { FormFieldType.TEXT, FormFieldType.PASSWORD, FormFieldType.EMAIL, FormFieldType.URL, FormFieldType.TEXTAREA, FormFieldType.NUMBER, FormFieldType.DATE, FormFieldType.TIME, FormFieldType.RANGE, FormFieldType.COLOR, FormFieldType.CHECKBOX, FormFieldType.RADIO, FormFieldType.SELECT, FormFieldType.HIDDEN } # This test verifies that the example includes all major field types # (the actual verification would happen by inspecting the forms, # but this confirms the enum exists) assert len(expected_types) == 14 def test_combine_pages_into_grid(): """Test combining pages into grid.""" combine_pages_into_grid = demo_module.combine_pages_into_grid from pyWebLayout.concrete.page import Page from pyWebLayout.style.page_style import PageStyle # Create a few pages page_style = PageStyle() pages = [ Page(size=(200, 200), style=page_style), Page(size=(200, 200), style=page_style), Page(size=(200, 200), style=page_style), Page(size=(200, 200), style=page_style) ] combined = combine_pages_into_grid(pages, "Test Title") # Should create a combined image from PIL import Image assert isinstance(combined, Image.Image) def test_main_function(): """Test that the main function runs without errors.""" main = demo_module.main # Run main (will create output files) result = main() # Should return combined image and form submissions assert result is not None assert len(result) == 2 combined_image, form_submissions = result # Should be a PIL image from PIL import Image assert isinstance(combined_image, Image.Image) # Form submissions should be a list assert isinstance(form_submissions, list) # Output file should exist output_dir = Path("docs/images") assert (output_dir / "example_10_forms.png").exists() if __name__ == "__main__": pytest.main([__file__, "-v"])