def test_invalid_input_missing_source_key(self):
     input_str = '{"People": [{"Name": "Bob", "Age": 49}, {"Name": "Sue", "Age": 42}]}'
     field = ListField(
         "Stuff", EmbeddedDictField(ScalarField("Name"), ScalarField("Age")), alti_key="person"
     )
     input_data = json.loads(input_str)
     with self.assertRaises(ListFieldSourceKeyNotFoundException):
         field.parse(data=input_data, context={"parent_alti_key": "test_parent"})
 def test_invalid_input_not_list(self):
     input_str = '{"People": "foo"}'
     field = ListField(
         "People", EmbeddedDictField(ScalarField("Name"), ScalarField("Age")), alti_key="person"
     )
     input_data = json.loads(input_str)
     with self.assertRaises(ListFieldValueNotAListException):
         field.parse(data=input_data, context={"parent_alti_key": "test_parent"})
Пример #3
0
    def test_valid_strings_input(self):
        input_str = '{"Animals": ["cow", "pig", "human"]}'
        field = ListField("Animals", EmbeddedScalarField())
        expected_output_data = [
            {
                "pred": "animals",
                "obj": "cow",
                "type": "simple"
            },
            {
                "pred": "animals",
                "obj": "pig",
                "type": "simple"
            },
            {
                "pred": "animals",
                "obj": "human",
                "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_dicts_input_with_alti_key(self):
        input_str = '{"People": [{"Name": "Bob", "Age": 49}, {"Name": "Sue", "Age": 42}]}'
        field = ListField(
            "People", EmbeddedDictField(ScalarField("Name"), ScalarField("Age")), alti_key="person"
        )

        input_data = json.loads(input_str)
        link_collection = field.parse(data=input_data, context={})

        expected_link_collection = LinkCollection(
            multi_links=(
                MultiLink(
                    pred="person",
                    obj=LinkCollection(
                        simple_links=(
                            SimpleLink(pred="name", obj="Bob"),
                            SimpleLink(pred="age", obj=49),
                        ),
                    ),
                ),
                MultiLink(
                    pred="person",
                    obj=LinkCollection(
                        simple_links=(
                            SimpleLink(pred="name", obj="Sue"),
                            SimpleLink(pred="age", obj=42),
                        ),
                    ),
                ),
            ),
        )
        self.assertEqual(link_collection, expected_link_collection)
    def test_optional(self):
        input_str = "{}"
        field = ListField("People", EmbeddedScalarField(), alti_key="person", optional=True)

        input_data = json.loads(input_str)
        link_collection = field.parse(data=input_data, context={})

        self.assertEqual(link_collection, LinkCollection())
    def test_allow_scalar(self):
        input_str = '{"People": "bob"}'
        field = ListField("People", EmbeddedScalarField(), alti_key="person", allow_scalar=True)

        input_data = json.loads(input_str)
        link_collection = field.parse(data=input_data, context={})

        expected_link_collection = LinkCollection(
            simple_links=(SimpleLink(pred="person", obj="bob"),),
        )
        self.assertEqual(link_collection, expected_link_collection)
Пример #7
0
    def test_optional(self):
        input_str = "{}"
        field = ListField("People",
                          EmbeddedScalarField(),
                          alti_key="person",
                          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)
Пример #8
0
    def test_valid_dicts_input_with_alti_key(self):
        input_str = '{"People": [{"Name": "Bob", "Age": 49}, {"Name": "Sue", "Age": 42}]}'
        field = ListField("People",
                          EmbeddedDictField(ScalarField("Name"),
                                            ScalarField("Age")),
                          alti_key="person")
        expected_output_data = [
            {
                "pred":
                "person",
                "obj": [
                    {
                        "pred": "name",
                        "obj": "Bob",
                        "type": "simple"
                    },
                    {
                        "pred": "age",
                        "obj": 49,
                        "type": "simple"
                    },
                ],
                "type":
                "multi",
            },
            {
                "pred":
                "person",
                "obj": [
                    {
                        "pred": "name",
                        "obj": "Sue",
                        "type": "simple"
                    },
                    {
                        "pred": "age",
                        "obj": 42,
                        "type": "simple"
                    },
                ],
                "type":
                "multi",
            },
        ]

        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_strings_input(self):
        input_str = '{"Animals": ["cow", "pig", "human"]}'
        field = ListField("Animals", EmbeddedScalarField())

        input_data = json.loads(input_str)
        link_collection = field.parse(data=input_data, context={})

        expected_link_collection = LinkCollection(
            simple_links=(
                SimpleLink(pred="animals", obj="cow"),
                SimpleLink(pred="animals", obj="pig"),
                SimpleLink(pred="animals", obj="human"),
            ),
        )
        self.assertEqual(link_collection, expected_link_collection)
Пример #10
0
    def test_allow_scalar(self):
        input_str = '{"People": "bob"}'
        field = ListField("People",
                          EmbeddedScalarField(),
                          alti_key="person",
                          allow_scalar=True)
        expected_output_data = ({
            "pred": "person",
            "obj": "bob",
            "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)