Commit 17a93a42 authored by Titouan Soulard's avatar Titouan Soulard

erp5_json_form: fix all tests

parent dd10ac56
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
############################################################################## ##############################################################################
import json import json
import re
from DateTime import DateTime from DateTime import DateTime
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
...@@ -47,7 +48,7 @@ class TestJSONForm(ERP5TypeTestCase): ...@@ -47,7 +48,7 @@ class TestJSONForm(ERP5TypeTestCase):
createZODBPythonScript( createZODBPythonScript(
self.portal.portal_skins.custom, self.portal.portal_skins.custom,
name, name,
"title, form_reference", "title='DEFAULT', **kwargs",
"""return { """return {
"datetime": DateTime().ISO8601(), "datetime": DateTime().ISO8601(),
"title": title, "title": title,
...@@ -78,6 +79,7 @@ class TestJSONForm(ERP5TypeTestCase): ...@@ -78,6 +79,7 @@ class TestJSONForm(ERP5TypeTestCase):
def test_call_valid_json(self): def test_call_valid_json(self):
""" """
Calls basic JSON Form.
""" """
schema = """{ schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema", "$schema": "https://json-schema.org/draft/2019-09/schema",
...@@ -91,18 +93,19 @@ class TestJSONForm(ERP5TypeTestCase): ...@@ -91,18 +93,19 @@ class TestJSONForm(ERP5TypeTestCase):
data = { data = {
"title": "foo" "title": "foo"
} }
method = "test_ERP5Site_processSimpleStringAsJSON" method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp() after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method) json_form = self.fixJSONForm(method_id, schema, after_method)
self.tic() self.tic()
result = getattr(self.portal, method)(json.dumps(data))
self.assertEqual(
json.loads(result),
data,
)
def test_call_no_after_method_id_valid_json(self): result = json.loads(json_form(json.dumps(data)))
self.assertIn("title", result)
self.assertEqual(result["title"], data["title"])
self.assertIn("datetime", result)
def test_call_no_after_method_id(self):
""" """
Raises when no after method is defined (misconfiguration).
""" """
schema = """{ schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema", "$schema": "https://json-schema.org/draft/2019-09/schema",
...@@ -116,14 +119,15 @@ class TestJSONForm(ERP5TypeTestCase): ...@@ -116,14 +119,15 @@ class TestJSONForm(ERP5TypeTestCase):
data = { data = {
"title": "foo" "title": "foo"
} }
method = "test_ERP5Site_processSimpleStringAsJSON" method_id = "test_ERP5Site_processSimpleStringAsJSON"
self.fixJSONForm(method, schema, "") json_form = self.fixJSONForm(method_id, schema, "")
self.tic() self.tic()
result = getattr(self.portal, method)(data)
self.assertEqual('Nothing to do', result) self.assertRaisesRegexp(NotImplementedError, "No after method", json_form, json.dumps(data))
def test_call_invalid_json_list_errors(self): def test_call_invalid_json_list_errors(self):
""" """
Reports JSON validation errors when configured.
""" """
schema = """{ schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema", "$schema": "https://json-schema.org/draft/2019-09/schema",
...@@ -141,20 +145,18 @@ class TestJSONForm(ERP5TypeTestCase): ...@@ -141,20 +145,18 @@ class TestJSONForm(ERP5TypeTestCase):
"title": 2, "title": 2,
"number": "2" "number": "2"
} }
method = "test_ERP5Site_processSimpleStringAsJSON" method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp() after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method) json_form = self.fixJSONForm(method_id, schema, after_method)
self.tic() self.tic()
self.assertRaises(ValueError, getattr(self.portal, method), json_data, list_error=True)
error = {"my-schema.json": [[u"Validation Error", u"'2' is not of type u'integer'"], [u"Validation Error", u"2 is not of type u'string'"]]} error = {}
try: error[json_form.absolute_url() + "/getInputJSONSchema"] = [[u"Validation Error", "u'2' is not of type u'integer'"], [u"Validation Error", "2 is not of type u'string'"]]
getattr(self.portal, method)(json_data, list_error=True) self.assertRaisesRegexp(ValueError, re.escape(json.dumps(error)), json_form, json.dumps(json_data), list_error=True)
raise ValueError("No error raised during processing")
except ValueError as e:
self.assertEqual(error, json.loads(str(e)))
def test_call_valid_datetime_format(self): def test_call_valid_datetime_format(self):
""" """
Validates and formats datetime according to ISO8601.
""" """
schema = """{ schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema", "$schema": "https://json-schema.org/draft/2019-09/schema",
...@@ -169,18 +171,20 @@ class TestJSONForm(ERP5TypeTestCase): ...@@ -169,18 +171,20 @@ class TestJSONForm(ERP5TypeTestCase):
data = { data = {
"timestamp": DateTime().ISO8601() "timestamp": DateTime().ISO8601()
} }
method = "test_ERP5Site_processSimpleStringAsJSON" method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp() after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method) json_form = self.fixJSONForm(method_id, schema, after_method)
self.tic() self.tic()
result = getattr(self.portal, method)(data)
self.assertEqual( result = json.loads(json_form(json.dumps(data)))
json.loads(json.dumps(data)), self.assertIn("title", result)
json.loads(result)['content'] self.assertEqual("DEFAULT", result["title"])
) self.assertIn("datetime", result)
self.assertEqual(data["timestamp"], result["datetime"])
def test_call_invalid_datetime_format(self): def test_call_invalid_datetime_format(self):
""" """
Rejects invalid datetime format.
""" """
schema = """{ schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema", "$schema": "https://json-schema.org/draft/2019-09/schema",
...@@ -195,24 +199,18 @@ class TestJSONForm(ERP5TypeTestCase): ...@@ -195,24 +199,18 @@ class TestJSONForm(ERP5TypeTestCase):
json_data = { json_data = {
"timestamp": "2018-11-13T20:20:67" "timestamp": "2018-11-13T20:20:67"
} }
method = "test_ERP5Site_processSimpleStringAsJSON" method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp() after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method) json_form = self.fixJSONForm(method_id, schema, after_method)
self.tic() self.tic()
self.assertRaises(ValueError, getattr(self.portal, method), json_data, list_error=True)
error = { error = {}
"my-schema.json": [[ error[json_form.absolute_url() + "/getInputJSONSchema"] = [["Validation Error", "u'2018-11-13T20:20:67' is not a u'date-time'"]]
"Validation Error", u"'2018-11-13T20:20:67' is not a u'date-time'" self.assertRaisesRegexp(ValueError, re.escape(json.dumps(error)), json_form, json.dumps(json_data), list_error=True)
]]
}
try:
getattr(self.portal, method)(json_data, list_error=True)
raise ValueError("No error raised during processing")
except ValueError as e:
self.assertEqual(error, json.loads(str(e)))
def test_empty_data_with_required_property(self): def test_empty_data_with_required_property(self):
""" """
Rejects form missing a required property.
""" """
schema = """{ schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema", "$schema": "https://json-schema.org/draft/2019-09/schema",
...@@ -225,21 +223,14 @@ class TestJSONForm(ERP5TypeTestCase): ...@@ -225,21 +223,14 @@ class TestJSONForm(ERP5TypeTestCase):
"required": ["title"] "required": ["title"]
}""" }"""
data = {} data = {}
method = "test_ERP5Site_processSimpleStringAsJSON" method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp() after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method) json_form = self.fixJSONForm(method_id, schema, after_method)
self.tic() self.tic()
self.assertRaises(ValueError, getattr(self.portal, method), data, list_error=True)
error = { error = {}
"my-schema.json": [[ error[json_form.absolute_url() + "/getInputJSONSchema"] = [['Validation Error', u"u'title' is a required property"]]
'Validation Error', u"u'title' is a required property" self.assertRaisesRegexp(ValueError, re.escape(json.dumps(error)), json_form, json.dumps(data), list_error=True)
]]
}
try:
getattr(self.portal, method)(data, list_error=True)
raise ValueError("No error raised during processing")
except ValueError, e:
self.assertEqual(error, json.loads(str(e)))
def test_supports_argument_mappings(self): def test_supports_argument_mappings(self):
""" """
...@@ -274,8 +265,8 @@ class TestJSONForm(ERP5TypeTestCase): ...@@ -274,8 +265,8 @@ class TestJSONForm(ERP5TypeTestCase):
mapping_type="output" mapping_type="output"
) )
self.tic() self.tic()
method = getattr(self.portal, method_id)
result = json.loads(method(json.dumps(input_dict))) result = json.loads(json_form(json.dumps(input_dict)))
self.assertIn("output_name", result) self.assertIn("output_name", result)
self.assertEqual("foo", result["output_name"]) self.assertEqual("foo", result["output_name"])
self.assertNotIn("name", result) self.assertNotIn("name", result)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment