Beispiel #1
0
    def test_inline_schema_resolves():
        expected_schema = {
            "type": ["null", "object"],
            "properties": {
                "str": {
                    "type": "string"
                },
                "int": {
                    "type": "integer"
                },
                "obj": {
                    "type": ["null", "object"],
                    "properties": {
                        "k1": {
                            "type": "string"
                        }
                    },
                },
            },
        }

        create_schema("simple_schema", expected_schema)
        resolver = ResourceSchemaLoader(MODULE_NAME)
        actual_schema = resolver.get_schema("simple_schema")
        assert actual_schema == expected_schema
Beispiel #2
0
 def fields(self) -> List[str]:
     """List of fields that we want to query, for now just all properties from stream's schema"""
     if self._fields:
         return self._fields
     schema = ResourceSchemaLoader(package_name_from_class(
         self.__class__)).get_schema("ads_insights")
     return list(schema.get("properties", {}).keys())
Beispiel #3
0
    def _check_custom_insights_entries(self, insights: List[Mapping[str, Any]]):

        default_fields = list(
            ResourceSchemaLoader(package_name_from_class(self.__class__)).get_schema("ads_insights").get("properties", {}).keys()
        )
        default_breakdowns = list(
            ResourceSchemaLoader(package_name_from_class(self.__class__)).get_schema("ads_insights_breakdowns").get("properties", {}).keys()
        )
        default_actions_breakdowns = [e for e in default_breakdowns if "action_" in e]

        for insight in insights:
            if insight.get("fields"):
                value_checked, value = self._check_values(default_fields, insight.get("fields"))
                if not value_checked:
                    message = f"{value} is not a valid field name"
                    raise Exception("Config validation error: " + message) from None
            if insight.get("breakdowns"):
                value_checked, value = self._check_values(default_breakdowns, insight.get("breakdowns"))
                if not value_checked:
                    message = f"{value} is not a valid breakdown name"
                    raise Exception("Config validation error: " + message) from None
            if insight.get("action_breakdowns"):
                value_checked, value = self._check_values(default_actions_breakdowns, insight.get("action_breakdowns"))
                if not value_checked:
                    message = f"{value} is not a valid action_breakdown name"
                    raise Exception("Config validation error: " + message) from None

        return True
Beispiel #4
0
    def test_shared_schemas_resolves():
        expected_schema = {
            "type": ["null", "object"],
            "properties": {
                "str": {
                    "type": "string"
                },
                "int": {
                    "type": "integer"
                },
                "obj": {
                    "$ref": "#/definitions/shared_schema_"
                },
            },
            "definitions": {
                "shared_schema_": {
                    "type": ["null", "object"],
                    "properties": {
                        "k1": {
                            "type": "string"
                        }
                    }
                }
            },
        }

        partial_schema = {
            "type": ["null", "object"],
            "properties": {
                "str": {
                    "type": "string"
                },
                "int": {
                    "type": "integer"
                },
                "obj": {
                    "$ref": "shared_schema.json"
                }
            },
        }

        referenced_schema = {
            "type": ["null", "object"],
            "properties": {
                "k1": {
                    "type": "string"
                }
            }
        }

        create_schema("complex_schema", partial_schema)
        create_schema("shared/shared_schema", referenced_schema)

        resolver = ResourceSchemaLoader(MODULE_NAME)

        actual_schema = resolver.get_schema("complex_schema")
        assert actual_schema == expected_schema
Beispiel #5
0
 def get_json_schema(self) -> Mapping[str, Any]:
     """Add fields from breakdowns to the stream schema
     :return: A dict of the JSON schema representing this stream.
     """
     loader = ResourceSchemaLoader(package_name_from_class(self.__class__))
     schema = loader.get_schema("ads_insights")
     if self._fields:
         schema["properties"] = {k: v for k, v in schema["properties"].items() if k in self._fields}
     if self.breakdowns:
         breakdowns_properties = loader.get_schema("ads_insights_breakdowns")["properties"]
         schema["properties"].update({prop: breakdowns_properties[prop] for prop in self.breakdowns})
     return schema
Beispiel #6
0
 def get_json_schema(self) -> Mapping[str, Any]:
     """Add fields from breakdowns to the stream schema
     :return: A dict of the JSON schema representing this stream.
     """
     schema = ResourceSchemaLoader(package_name_from_class(self.__class__)).get_schema("ads_insights")
     schema["properties"].update(self._schema_for_breakdowns())
     return schema
Beispiel #7
0
    def get_json_schema(self) -> Mapping[str, Any]:
        """
        :return: A dict of the JSON schema representing this stream.

        The default implementation of this method looks for a JSONSchema file with the same name as this stream's "name" property.
        Override as needed.
        """
        # TODO show an example of using pydantic to define the JSON schema, or reading an OpenAPI spec
        return ResourceSchemaLoader(package_name_from_class(self.__class__)).get_schema(self.name)
Beispiel #8
0
 def get_json_schema(self) -> Mapping[str, Any]:
     return ResourceSchemaLoader(package_name_from_class(
         self.__class__)).get_schema(self.report_schema_name)
Beispiel #9
0
 def get_json_schema(self) -> Mapping[str, Any]:
     """All reports have same schema"""
     return ResourceSchemaLoader(
         package_name_from_class(AdvertiserIds)).get_schema(
             self.schema_name)
Beispiel #10
0
    def test_shared_schemas_resolves_nested():
        expected_schema = {
            "type": ["null", "object"],
            "properties": {
                "str": {
                    "type": "string"
                },
                "int": {
                    "type": "integer"
                },
                "one_of": {
                    "oneOf": [
                        {
                            "type": "string"
                        },
                        {
                            "type": ["null", "object"],
                            "properties": {
                                "k1": {
                                    "type": "string"
                                }
                            },
                        },
                    ]
                },
                "obj": {
                    "type": ["null", "object"],
                    "properties": {
                        "k1": {
                            "type": "string"
                        }
                    },
                },
            },
        }
        partial_schema = {
            "type": ["null", "object"],
            "properties": {
                "str": {
                    "type": "string"
                },
                "int": {
                    "type": "integer"
                },
                "one_of": {
                    "oneOf": [
                        {
                            "type": "string"
                        },
                        {
                            "$ref": "shared_schema.json#/definitions/type_one"
                        },
                    ]
                },
                "obj": {
                    "$ref": "shared_schema.json#/definitions/type_one"
                },
            },
        }

        referenced_schema = {
            "definitions": {
                "type_one": {
                    "$ref": "shared_schema.json#/definitions/type_nested"
                },
                "type_nested": {
                    "type": ["null", "object"],
                    "properties": {
                        "k1": {
                            "type": "string"
                        }
                    },
                },
            }
        }

        create_schema("complex_schema", partial_schema)
        create_schema("shared/shared_schema", referenced_schema)

        resolver = ResourceSchemaLoader(MODULE_NAME)

        actual_schema = resolver.get_schema("complex_schema")
        assert actual_schema == expected_schema
        # Make sure generated schema is JSON serializable
        assert json.dumps(actual_schema)
        assert jsonref.JsonRef.replace_refs(actual_schema)