def test_value_not_scalar(self): input_str = '{"FieldName": [1, 2, 3]}' field = ScalarField("FieldName") input_data = json.loads(input_str) with self.assertRaises(ScalarFieldValueNotAScalarException): field.parse(data=input_data, context={})
def test_key_absent_without_optional(self): input_str = "{}" field = ScalarField("FieldName") input_data = json.loads(input_str) with self.assertRaises(ScalarFieldSourceKeyNotFoundException): field.parse(data=input_data, context={})
def test_key_absent_with_optional(self): input_str = "{}" field = ScalarField("FieldName", optional=True) input_data = json.loads(input_str) link_collection = field.parse(data=input_data, context={}) self.assertEqual(link_collection, LinkCollection())
def test_key_absent_with_optional(self): input_str = "{}" field = ScalarField("FieldName", optional=True) expected_output_data = [] input_data = json.loads(input_str) links = field.parse(data=input_data, context={}) output_data = [link.to_dict() for link in links] self.assertCountEqual(output_data, expected_output_data)
def test_key_absent_with_default(self): input_str = "{}" field = ScalarField("FieldName", default_value="DefaultValue") input_data = json.loads(input_str) link_collection = field.parse(data=input_data, context={}) expected_link_collection = LinkCollection(simple_links=(SimpleLink( pred="field_name", obj="DefaultValue"), ), ) self.assertEqual(link_collection, expected_link_collection)
def test_key_present_with_optional(self): input_str = '{"FieldName": "Value"}' field = ScalarField("FieldName", optional=True) input_data = json.loads(input_str) link_collection = field.parse(data=input_data, context={}) expected_link_collection = LinkCollection(simple_links=(SimpleLink( pred="field_name", obj="Value"), ), ) self.assertEqual(link_collection, expected_link_collection)
def test_valid_input_with_alti_key(self): input_str = '{"FieldName": "Value"}' field = ScalarField("FieldName", alti_key="alti_field_name") input_data = json.loads(input_str) link_collection = field.parse(data=input_data, context={}) expected_link_collection = LinkCollection(simple_links=(SimpleLink( pred="alti_field_name", obj="Value"), ), ) self.assertEqual(link_collection, expected_link_collection)
def test_key_absent_with_default(self): input_str = "{}" field = ScalarField("FieldName", default_value="DefaultValue") expected_output_data = [{ "pred": "field_name", "obj": "DefaultValue", "type": "simple" }] input_data = json.loads(input_str) links = field.parse(data=input_data, context={}) output_data = [link.to_dict() for link in links] self.assertCountEqual(output_data, expected_output_data)
def test_key_present_with_optional(self): input_str = '{"FieldName": "Value"}' field = ScalarField("FieldName", optional=True) expected_output_data = [{ "pred": "field_name", "obj": "Value", "type": "simple" }] input_data = json.loads(input_str) links = field.parse(data=input_data, context={}) output_data = [link.to_dict() for link in links] self.assertCountEqual(output_data, expected_output_data)
def test_valid_input_with_alti_key(self): input_str = '{"FieldName": "Value"}' field = ScalarField("FieldName", alti_key="alti_field_name") expected_output_data = [{ "pred": "alti_field_name", "obj": "Value", "type": "simple" }] input_data = json.loads(input_str) links = field.parse(data=input_data, context={}) output_data = [link.to_dict() for link in links] self.assertCountEqual(output_data, expected_output_data)