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

erp5_json_form: fix all tests

parent dd10ac56
......@@ -26,6 +26,7 @@
##############################################################################
import json
import re
from DateTime import DateTime
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
......@@ -47,7 +48,7 @@ class TestJSONForm(ERP5TypeTestCase):
createZODBPythonScript(
self.portal.portal_skins.custom,
name,
"title, form_reference",
"title='DEFAULT', **kwargs",
"""return {
"datetime": DateTime().ISO8601(),
"title": title,
......@@ -78,6 +79,7 @@ class TestJSONForm(ERP5TypeTestCase):
def test_call_valid_json(self):
"""
Calls basic JSON Form.
"""
schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema",
......@@ -91,18 +93,19 @@ class TestJSONForm(ERP5TypeTestCase):
data = {
"title": "foo"
}
method = "test_ERP5Site_processSimpleStringAsJSON"
method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method)
json_form = self.fixJSONForm(method_id, schema, after_method)
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": "https://json-schema.org/draft/2019-09/schema",
......@@ -116,14 +119,15 @@ class TestJSONForm(ERP5TypeTestCase):
data = {
"title": "foo"
}
method = "test_ERP5Site_processSimpleStringAsJSON"
self.fixJSONForm(method, schema, "")
method_id = "test_ERP5Site_processSimpleStringAsJSON"
json_form = self.fixJSONForm(method_id, schema, "")
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):
"""
Reports JSON validation errors when configured.
"""
schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema",
......@@ -141,20 +145,18 @@ class TestJSONForm(ERP5TypeTestCase):
"title": 2,
"number": "2"
}
method = "test_ERP5Site_processSimpleStringAsJSON"
method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method)
json_form = self.fixJSONForm(method_id, schema, after_method)
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'"]]}
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)))
error = {}
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'"]]
self.assertRaisesRegexp(ValueError, re.escape(json.dumps(error)), json_form, json.dumps(json_data), list_error=True)
def test_call_valid_datetime_format(self):
"""
Validates and formats datetime according to ISO8601.
"""
schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema",
......@@ -169,18 +171,20 @@ class TestJSONForm(ERP5TypeTestCase):
data = {
"timestamp": DateTime().ISO8601()
}
method = "test_ERP5Site_processSimpleStringAsJSON"
method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method)
json_form = self.fixJSONForm(method_id, schema, after_method)
self.tic()
result = getattr(self.portal, method)(data)
self.assertEqual(
json.loads(json.dumps(data)),
json.loads(result)['content']
)
result = json.loads(json_form(json.dumps(data)))
self.assertIn("title", result)
self.assertEqual("DEFAULT", result["title"])
self.assertIn("datetime", result)
self.assertEqual(data["timestamp"], result["datetime"])
def test_call_invalid_datetime_format(self):
"""
Rejects invalid datetime format.
"""
schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema",
......@@ -195,24 +199,18 @@ class TestJSONForm(ERP5TypeTestCase):
json_data = {
"timestamp": "2018-11-13T20:20:67"
}
method = "test_ERP5Site_processSimpleStringAsJSON"
method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method)
json_form = self.fixJSONForm(method_id, schema, after_method)
self.tic()
self.assertRaises(ValueError, getattr(self.portal, method), json_data, list_error=True)
error = {
"my-schema.json": [[
"Validation Error", u"'2018-11-13T20:20:67' is not a u'date-time'"
]]
}
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)))
error = {}
error[json_form.absolute_url() + "/getInputJSONSchema"] = [["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)
def test_empty_data_with_required_property(self):
"""
Rejects form missing a required property.
"""
schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema",
......@@ -225,21 +223,14 @@ class TestJSONForm(ERP5TypeTestCase):
"required": ["title"]
}"""
data = {}
method = "test_ERP5Site_processSimpleStringAsJSON"
method_id = "test_ERP5Site_processSimpleStringAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method)
json_form = self.fixJSONForm(method_id, schema, after_method)
self.tic()
self.assertRaises(ValueError, getattr(self.portal, method), data, list_error=True)
error = {
"my-schema.json": [[
'Validation Error', u"u'title' is a required property"
]]
}
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)))
error = {}
error[json_form.absolute_url() + "/getInputJSONSchema"] = [['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)
def test_supports_argument_mappings(self):
"""
......@@ -274,8 +265,8 @@ class TestJSONForm(ERP5TypeTestCase):
mapping_type="output"
)
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.assertEqual("foo", result["output_name"])
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