def test_merge_two_dicts(self):
        dict1 = convert_yaml_to_dict(SampleSchema.TEST.ONE)
        dict2 = convert_yaml_to_dict(SampleSchema.TEST.TWO_THAT_REFERENCES_ONE)

        dict2 = merge_two_dicts(dict1, dict2)

        # Should not work, dict1 should be unchanged
        with self.assertRaises(KeyError):
            dict1["foo"] == 1  # pylint: disable=pointless-statement

        self.assertTrue(dict1["qaz"] == "a")

        # Should work - dict2 is the merge target
        self.assertTrue(dict2["qaz"] == "a")
        self.assertTrue(dict2["foo"] == 1)
Exemple #2
0
    def _augment_with_base_schema(schema_dict: dict):
        # The below is incredibly gross code smell - ideally, there'd
        # be a much better way to merge the base schema with the derivative ones
        # and that allowed for multiple layers of inheritance. Oh well.
        # TODO: Support multiple layer of inheritence

        base_name = None
        base_version = None

        # If the yaml has a base_type
        if ("mlspec_base_type" in schema_dict
                and schema_dict["mlspec_base_type"]["meta"] is not None):
            base_type = schema_dict["mlspec_base_type"]
        else:
            # There is no base type, so just return the full schema.
            return schema_dict

        if "mlspec_schema_version" in schema_dict:
            base_version = schema_dict["mlspec_schema_version"]
        else:
            raise KeyError(
                "There is no mlschema version for this spec, so cannot look up the base schema."
            )  # noqa
        try:
            base_schema = marshmallow.class_registry.get_class(
                build_schema_name_for_schema(
                    mlspec_schema_version=base_version,
                    mlspec_schema_type=base_type))

        except RegistryError:
            raise RegistryError(
                f"""Could not find the base schema in the class registry. Values provided:
        base_name = '{base_name}'
        base_type = '{base_type}'
        schema_version = '{base_version}'""")

        base_dict = base_schema().fields

        schema_dict = merge_two_dicts(schema_dict, base_dict)

        return schema_dict
Exemple #3
0
 def wrap_submission_with_mlschema_info(self, this_dict):
     return merge_two_dicts(self.submission_schema_info,
                            convert_yaml_to_dict(this_dict))