jsonifier: fix field validation
An invalid field must be skipped in any case if not in strict mode. Error message has been downgraded to warning since is not really broken till you use string mode. Log tests reworked.pull/3205/head
parent
f58c7447ca
commit
2e7ee2269b
|
@ -119,15 +119,15 @@ class Base(models.AbstractModel):
|
|||
if strict:
|
||||
# let it fail
|
||||
rec._fields[field_name] # pylint: disable=pointless-statement
|
||||
if not tools.config["test_enable"]:
|
||||
# If running live, log proper error
|
||||
# so that techies can track it down
|
||||
_logger.error(
|
||||
"%(model)s.%(fname)s not available",
|
||||
{"model": self._name, "fname": field_name},
|
||||
)
|
||||
else:
|
||||
if not tools.config["test_enable"]:
|
||||
# If running live, log proper error
|
||||
# so that techies can track it down
|
||||
_logger.warning(
|
||||
"%(model)s.%(fname)s not available",
|
||||
{"model": self._name, "fname": field_name},
|
||||
)
|
||||
raise SwallableException()
|
||||
|
||||
return True
|
||||
|
||||
def _jsonify_record_handle_function(self, rec, field_dict, strict):
|
||||
|
|
|
@ -385,24 +385,25 @@ class TestParser(TransactionCase):
|
|||
def test_bad_parsers_fail_gracefully(self):
|
||||
rec = self.category
|
||||
|
||||
logger_patch_path = "odoo.addons.jsonifier.models.models._logger.error"
|
||||
|
||||
# logging is disabled when testing as it's useless and makes build fail.
|
||||
# logging is disabled when testing as it makes too much noise
|
||||
tools.config["test_enable"] = False
|
||||
|
||||
logger_name = "odoo.addons.jsonifier.models.models"
|
||||
bad_field_name = ["Name"]
|
||||
with mock.patch(logger_patch_path) as mocked_logger:
|
||||
with self.assertLogs(logger=logger_name, level="WARNING") as capt:
|
||||
rec.jsonify(bad_field_name, one=True)
|
||||
mocked_logger.assert_called()
|
||||
self.assertIn("res.partner.category.Name not availabl", capt.output[0])
|
||||
|
||||
bad_function_name = {"fields": [{"name": "name", "function": "notafunction"}]}
|
||||
with mock.patch(logger_patch_path) as mocked_logger:
|
||||
with self.assertLogs(logger=logger_name, level="WARNING") as capt:
|
||||
rec.jsonify(bad_function_name, one=True)
|
||||
mocked_logger.assert_called()
|
||||
self.assertIn(
|
||||
"res.partner.category.notafunction not available", capt.output[0]
|
||||
)
|
||||
|
||||
bad_subparser = {"fields": [({"name": "name"}, [{"name": "subparser_name"}])]}
|
||||
with mock.patch(logger_patch_path) as mocked_logger:
|
||||
with self.assertLogs(logger=logger_name, level="WARNING") as capt:
|
||||
rec.jsonify(bad_subparser, one=True)
|
||||
mocked_logger.assert_called()
|
||||
self.assertIn("res.partner.category.name not relational", capt.output[0])
|
||||
|
||||
tools.config["test_enable"] = True
|
||||
|
|
Loading…
Reference in New Issue