def test_deserializer(self): """ """ body = { '@type': 'TestToken', 'title': 'Test Organization xxx', } with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.json'), 'r') as f: body['resource'] = json.load(f) request = TestRequest(BODY=json.dumps(body)) obj = create(self.portal, body['@type'], id_=None, title=body['title']) deserializer = queryMultiAdapter((obj, request), IDeserializeFromJson) assert deserializer is not None deserializer(validate_all=True) rename(obj) self.assertTrue(IJSONValue.providedBy(obj.resource)) # Test error handling body['resource'] = 'hello error' request = TestRequest(BODY=json.dumps(body)) obj = create(self.portal, body['@type'], id_=None, title=body['title']) deserializer = queryMultiAdapter((obj, request), IDeserializeFromJson) try: deserializer(validate_all=True) raise AssertionError('Code should not come here! Because invalid fhir json data is provided!') except Invalid: pass
def test_fromUnicode(self): """ """ with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.schema.json'), 'r') as f: json_object_schema = json.load(f) with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.json'), 'r') as f: json_str = f.read() fhir_field = field.JSON(title=six.text_type('Organization resource'), json_schema=json_object_schema) try: fhir_resource_value = fhir_field.fromUnicode(json_str) except Invalid as exc: raise AssertionError( 'Code should not come here! as should return valid JSONValue.\n{0!s}' .format(exc)) self.assertTrue(IJSONValue.providedBy(fhir_resource_value)) # Test with invalid json string try: invalid_data = '{hekk: invalg, 2:3}' fhir_field.fromUnicode(invalid_data) raise AssertionError( 'Code should not come here! invalid json string is provided') except Invalid as exc: self.assertIn('Invalid JSON String', str(exc))
def test_from_iterable(self): """ """ with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.json'), 'r') as f: json_object_iter = json.load(f) with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.schema.json'), 'r') as f: json_object_schema = json.load(f) fhir_field = field.JSON(title=six.text_type('Organization resource'), json_schema=json_object_schema) try: fhir_resource_value = fhir_field.from_iterable(json_object_iter) except Invalid as exc: raise AssertionError( 'Code should not come here! as should return valid JSONValue.\n{0!s}' .format(exc)) self.assertEqual(fhir_resource_value['resourceType'], json_object_iter['resourceType']) with open(os.path.join(FHIR_FIXTURE_PATH, 'Products.array.json'), 'r') as f: json_array_iter = json.load(f) fhir_field.json_schema = None try: fhir_resource_value = fhir_field.from_iterable(json_array_iter) except Invalid as exc: raise AssertionError( 'Code should not come here! as should return valid JSONValue.\n{0!s}' .format(exc)) self.assertTrue(IJSONValue.providedBy(fhir_resource_value))
def toWidgetValue(self, value): if IJSONValue.providedBy(value): return value elif value in (NOVALUE, None, ''): return '' elif isinstance(value, six.string_types): return IJSON(self.field).fromUnicode(value) raise ValueError( 'Can not convert {0!s} to an IJSONValue'.format(value) )
def toWidgetValue(self, value): """ """ if value in (None, '', NOVALUE): return '' if IJSONValue.providedBy(value): if self.widget.mode in ('input', 'hidden'): return value.stringify() elif self.widget.mode == 'display': return value.stringify() if isinstance(value, six.string_types): return value raise ValueError( 'Can not convert {0:s} to unicode'.format(repr(value)) )
def assert_field(self, context, field, json_dict): """ """ # Deserialize to field value deserializer = queryMultiAdapter( (field, context, self.request), IFieldDeserializer ) self.assertIsNotNone(deserializer) field_value = deserializer(json_dict) # Value type is derived from right interface self.assertTrue(IJSONValue.providedBy(field_value)) # Test from string data field_value2 = deserializer(json.dumps(json_dict)) self.assertTrue(field_value, field_value2) try: deserializer(['I am invalid']) raise AssertionError('Code should not come here! because invalid data type is provided.') except ValueError: pass
def test_json_value(self): """ """ with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.json'), 'r') as f: json_object_iter = json.load(f) with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.schema.json'), 'r') as f: json_object_schema = json.load(f) json_object_value = value.JSONObjectValue(json_object_iter, schema=json_object_schema) self.assertTrue(IJSONValue.providedBy(json_object_value)) self.assertEqual(json_object_iter.keys(), json_object_value.keys()) with open(os.path.join(FHIR_FIXTURE_PATH, 'Products.array.json'), 'r') as f: json_array_iter = json.load(f) with open(os.path.join(FHIR_FIXTURE_PATH, 'Products.array.schema.json'), 'r') as f: json_array_schema = json.load(f) json_array_value = value.JSONArrayValue(json_array_iter, schema=json_array_schema) self.assertTrue(IJSONValue.providedBy(json_array_value)) json_object_value_empty = value.JSONObjectValue(NO_VALUE) json_array_value_empty = value.JSONArrayValue(NO_VALUE) self.assertTrue(IJSONValue.providedBy(json_object_value_empty)) self.assertEqual(0, len(json_object_value_empty)) self.assertTrue(IJSONValue.providedBy(json_array_value_empty)) self.assertEqual(0, len(json_array_value_empty)) # TEST: stringify self.assertIsInstance(json_object_value.stringify(), six.string_types) self.assertEqual(len(json_object_value.stringify()), len(json.dumps(json_object_iter))) self.assertEqual(json_array_value.stringify(), json.dumps(json_array_iter)) # json indent, prettification, should be new line exists self.assertNotIn('\\n', json_object_value.stringify(True)) self.assertEqual(json_object_value_empty.stringify(), '{}') # noqa: P103 self.assertEqual(json_array_value_empty.stringify(), '[]') # TEST: patch patch_data = [ {'path': '/text/status', 'value': 'patched!', 'op': 'replace'} ] json_object_value.patch(patch_data) self.assertEqual('patched!', json_object_value['text']['status']) # Empty Object can't be patcahed! try: json_object_value_empty.patch(patch_data) raise AssertionError('Code should not come here! because member `text` should not found in empty object') except Invalid as exc: self.assertIn('member \'text\' not found in {}', str(exc)) # noqa: P103 # Array is not patchable try: json_array_value.patch(patch_data) raise AssertionError( 'Code should not come here! because array data is not patchable!' ) except Invalid as exc: self.assertEqual('json patch is not applicable on array type value!', str(exc)) # wrong patch format! patch_data = {'hello': 123} try: json_object_value.patch(patch_data) raise AssertionError('Code should not come here! because wrong type data is provided for patch!') except WrongType: pass # wrong path! patch_data = [ {'path': '/text/fake path', 'value': 'patched!', 'Invalid Option': 'replace'} ] # Test getting original error from json patcher try: json_object_value.patch(patch_data) raise AssertionError( 'Code should not come here! because wrong patch data is provided for patch and invalid format as well!' ) except Invalid as exc: self.assertIn("does not contain 'op' member", str(exc))