-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add test for JSON schemas of formio components - Add test for form to JSON schema
- Loading branch information
1 parent
834981f
commit 81a6971
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|