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 8, 2025
1 parent 834981f commit 81a6971
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
54 changes: 54 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,54 @@
from django.test import TestCase

from jsonschema import Draft202012Validator

from openforms.formio.components.vanilla import TextField, Select


class ComponentAsJsonSchemaTests(TestCase):

validator = Draft202012Validator

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

self.validator.check_schema(schema)

def test_text_field(self):
component = {"multiple": False, "label": "Text field", "key": "text"}
with self.subTest("single"):
properties = TextField.as_json_schema(component)

self.check_schema(properties)

with self.subTest("multiple"):
component["multiple"] = True
properties = TextField.as_json_schema(component)

self.check_schema(properties)

def test_select(self):
component = {
"label": "Select",
"key": "select",
"data": {
"values": [
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
],
},
"type": "select",
}
with self.subTest("single"):
properties = Select.as_json_schema(component)
self.check_schema(properties)

with self.subTest("multiple"):
component["multiple"] = True
properties = Select.as_json_schema(component)
self.check_schema(properties)
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 81a6971

Please sign in to comment.