Skip to content

Commit

Permalink
✅ [#4980] Add tests
Browse files Browse the repository at this point in the history
- Add test for JSON schemas of formio components
- Add test for form to JSON schema
  • Loading branch information
viktorvanwijk committed Jan 9, 2025
1 parent 31bcb5d commit 0740d05
Show file tree
Hide file tree
Showing 2 changed files with 348 additions and 0 deletions.
261 changes: 261 additions & 0 deletions src/openforms/formio/tests/test_component_json_schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
from django.test import TestCase

from jsonschema import Draft202012Validator

from openforms.formio.components.custom import (
BSN,
AddressNL,
Cosign,
Date,
Datetime,
Iban,
LicensePlate,
Map,
Postcode,
)
from openforms.formio.components.vanilla import (
Checkbox,
Content,
Currency,
EditGrid,
Email,
File,
Number,
PhoneNumber,
Radio,
Select,
SelectBoxes,
Signature,
TextArea,
TextField,
Time,
)


# TODO-4980: add typing and include all values for the component dicts
class CustomComponentValidJsonSchemaTests(TestCase):

validator = Draft202012Validator

def check_schema(self, properties):
schema = {
"$schema": self.validator.META_SCHEMA["$id"],
**properties,
}

self.validator.check_schema(schema)

# TODO-4980: currently all components are testing for `multiple=True` which is not
# useful for all
def _test_component(self, component_class, component):
with self.subTest(f"single {component["label"]}"):
schema = component_class.as_json_schema(component)

print(schema)
self.check_schema(schema)

with self.subTest(f"multiple {component["label"]}"):
component["multiple"] = True
schema = component_class.as_json_schema(component)

self.check_schema(schema)

def test_checkbox(self):
component_class = Checkbox
component = {"label": "Checkbox", "key": "checkbox"}
self._test_component(component_class, component)

def test_content(self):
component_class = Content
component = {
"label": "Content", "key": "content", "html": "ASDF", "type": "content"
}
with self.assertRaises(NotImplementedError):
component_class.as_json_schema(component)

def test_currency(self):
component_class = Currency
component = {"label": "Currency", "key": "currency"}
self._test_component(component_class, component)

def test_edit_grid(self):
component_class = EditGrid
component = {
"key": "repeatingGroup",
"label": "Repeating Group Outer",
"type": "editgrid",
"components": [
{
"key": "repeatingGroupInner",
"label": "Repeating Group Inner",
"type": "editgrid",
"components": [
{
"key": "textFieldInner",
"label": "Text field",
"type": "textfield",
},
],
},
{
"key": "eMailadres",
"label": "Email address",
"type": "email",
},
],
}
self._test_component(component_class, component)

def test_email(self):
component_class = Email
component = {"label": "Email field", "key": "email"}
self._test_component(component_class, component)

def test_file(self):
component_class = File
component = {"label": "File", "key": "file"}
self._test_component(component_class, component)

def test_number(self):
component_class = Number
component = {"label": "Number", "key": "number"}
self._test_component(component_class, component)

def test_phone_number(self):
component_class = PhoneNumber
component = {"label": "Phone number", "key": "phone"}
self._test_component(component_class, component)

def test_radio(self):
component_class = Radio
component = {
"label": "Radio",
"key": "radio",
"values": [
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
],
}
self._test_component(component_class, component)

def test_select(self):
component_class = Select
component = {
"label": "Select",
"key": "select",
"data": {
"values": [
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
],
},
"type": "select",
}
self._test_component(component_class, component)

def test_select_boxes(self):
component_class = SelectBoxes
component = {
"label": "Select boxes",
"key": "selectBoxes",
"values": [
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
]
}
self._test_component(component_class, component)

def test_signature(self):
component_class = Signature
component = {"label": "Signature", "key": "signature"}
self._test_component(component_class, component)

def test_text_area(self):
component_class = TextArea
component = {"label": "TextArea", "key": "textArea"}
self._test_component(component_class, component)

def test_text_field(self):
component_class = TextField
component = {"label": "Text field", "key": "text"}
self._test_component(component_class, component)

def test_time(self):
component_class = Time
component = {"label": "Time", "key": "time"}
self._test_component(component_class, component)


class VanillaComponentValidJsonSchemaTests(TestCase):
validator = Draft202012Validator

def check_schema(self, properties):
schema = {
"$schema": self.validator.META_SCHEMA["$id"],
**properties,
}

self.validator.check_schema(schema)

# TODO-4980: currently all components are testing for `multiple=True` which is not
# useful for all
def _test_component(self, component_class, component):
with self.subTest(f"single {component["label"]}"):
schema = component_class.as_json_schema(component)

print(schema)
self.check_schema(schema)

with self.subTest(f"multiple {component["label"]}"):
component["multiple"] = True
schema = component_class.as_json_schema(component)

self.check_schema(schema)

def test_bsn(self):
component_class = BSN
component = {"label": "BSN", "key": "bsn"}
self._test_component(component_class, component)

def test_address_nl(self):
component_class = AddressNL
component = {"label": "Address NL", "key": "addressNL"}
self._test_component(component_class, component)

def test_cosign(self):
component_class = Cosign
component = {"label": "Cosign", "key": "cosign"}
self._test_component(component_class, component)

def test_date(self):
component_class = Date
component = {"label": "Date", "key": "date"}
self._test_component(component_class, component)

def test_date_time(self):
component_class = Datetime
component = {"label": "DateTime", "key": "datetime"}
self._test_component(component_class, component)

def test_iban(self):
component_class = Iban
component = {"label": "Iban", "key": "iban"}
self._test_component(component_class, component)

def test_license_plate(self):
component_class = LicensePlate
component = {"label": "License Plate", "key": "licenseplate"}
self._test_component(component_class, component)

def test_map(self):
component_class = Map
component = {"label": "Map", "key": "map"}
self._test_component(component_class, component)

def test_postcode(self):
component_class = Postcode
component = {"label": "Postcode", "key": "postcode"}
self._test_component(component_class, component)



87 changes: 87 additions & 0 deletions src/openforms/forms/tests/test_form_to_json_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from django.test import TestCase

from openforms.forms.tests.factories import (
FormDefinitionFactory,
FormFactory,
FormStepFactory,
)
from openforms.forms.utils import form_variables_to_json_schema


class FormToJsonSchemaTestCase(TestCase):
def test_form_to_json_schema(self):
form = FormFactory.create()
form_def_1 = FormDefinitionFactory.create(
configuration={
"components": [
{"key": "firstName", "type": "textfield", "label": "First Name"},
{
"key": "lastName",
"type": "textfield",
"multiple": True,
"label": "Last Name",
},
{
"label": "Select",
"key": "select",
"data": {
"values": [
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
],
"dataSrc": "manual",
"json": "",
"url": "",
"resource": "",
"custom": "",
},
"type": "select",
"multiple": True,
},
{
"type": "selectboxes",
"key": "selectboxes",
"values": [
{"label": "Option 1", "value": "option1"},
{"label": "Option 2", "value": "option2"},
],
},
{
"label": "Radio",
"key": "radio",
"type": "radio",
"values": [
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
],
"dataSrc": "manual",
},
]
}
)
form_step_1 = FormStepFactory.create(form=form, form_definition=form_def_1)

form_def_2 = FormDefinitionFactory.create(
configuration={
"components": [
{"key": "file", "type": "file", "label": "File"},
]
}
)
form_step_2 = FormStepFactory.create(form=form, form_definition=form_def_2)

vars_to_include = (
"firstName",
"lastName",
"select",
"selectboxes",
"radio",
"file",
"auth_bsn",
"today",
)
var_properties = form_variables_to_json_schema(form, vars_to_include)

from pprint import pprint
pprint(var_properties)

0 comments on commit 0740d05

Please sign in to comment.