Пример #1
0
def test_validate_models(config, model_file_name):
    with open(Path(os.path.dirname(__file__)) / model_file_name) as f:
        model = json.load(f)

    with open(config.model_schema_path) as f:
        model_schema = yaml.load(f, Loader=yaml.FullLoader)

    validation.validate_object_against_schema(
        input_object=model,
        schema_object=model_schema,
        raise_on_error=True,
        strict=False,
    )
Пример #2
0
def test_validate_default_expectations(config):

    with open(config.expectations_path, "r") as f:
        default_expectations = json.load(f)

    with open(config.expectations_schema_path, "r") as handle:
        expectations_schema = yaml.load(handle, Loader=yaml.FullLoader)

    validation.validate_object_against_schema(
        input_object=default_expectations,
        schema_object=expectations_schema,
        raise_on_error=True,
        strict=True,
    )
Пример #3
0
    def load_model(self):
        """
        Load the data model, then inject constants into the model.
        :return: data model with injected constants
        """
        with open(self.model_file_path, "r") as model_file:
            model = json.loads(model_file.read(),
                               object_pairs_hook=OrderedDict)

        with open(self.config.model_schema_path, "r") as model_schema_file:
            schema = yaml.load(model_schema_file, Loader=yaml.FullLoader)

        status = validation.validate_object_against_schema(
            input_object=model,
            schema_object=schema,
            strict=False,
        )

        if status.ok:
            exp_status = self.validate_expectations(model)

            if exp_status.ok:
                for endpoint in Fuzzer.get_endpoints(model["endpoints"],
                                                     self.uri):
                    exp_status = self.validate_expectations(endpoint)
                    if not exp_status.ok:
                        break
            if not exp_status.ok:
                self.config.root_logger.error(
                    "Model %s failed to validate against schema %s: %s",
                    self.model_file_path,
                    self.config.expectations_schema_path,
                    exp_status.errors,
                )
                try:
                    return self.model_obj
                except AttributeError:
                    raise validation.ValidationError(
                        f"Model {self.model_file_path} failed to validate against schema {self.config.model_schema_path}: {status.errors}"
                    )
            return model
        else:
            try:
                self.config.root_logger.error(
                    "Model %s failed to validate against schema %s: %s",
                    self.model_file_path,
                    self.config.model_schema_path,
                    status.errors,
                )
                return self.model_obj
            except AttributeError:
                raise validation.ValidationError(
                    f"Model {self.model_file_path} failed to validate against schema {self.config.model_schema_path}: {status.errors}"
                )
Пример #4
0
def test_validate_object_against_schema(
    schema_object, input_object, strict, raise_on_error, should_pass
):
    if raise_on_error and not should_pass:
        with pytest.raises(ValidationError):
            validation.validate_object_against_schema(
                input_object=input_object,
                schema_object=schema_object,
                raise_on_error=raise_on_error,
                strict=strict,
            )
    else:
        status = validation.validate_object_against_schema(
            input_object=input_object,
            schema_object=schema_object,
            raise_on_error=raise_on_error,
            strict=strict,
        )

        assert status.ok if should_pass else not status.ok
        assert status.errors if not should_pass else not status.errors
Пример #5
0
def test_validate_expectations(config, model_file_name):
    with open(Path(os.path.dirname(__file__)) / model_file_name) as f:
        model = json.load(f)

    with open(config.expectations_schema_path) as f:
        expectations_schema = yaml.load(f, Loader=yaml.FullLoader)

    if model.get("expectations"):
        validation.validate_object_against_schema(
            input_object=model,
            schema_object=expectations_schema,
            raise_on_error=True,
            strict=False,
        )

    for endpoint in model.get("endpoints"):
        if endpoint.get("expectations"):
            validation.validate_object_against_schema(
                input_object=endpoint,
                schema_object=expectations_schema,
                raise_on_error=True,
                strict=False,
            )
Пример #6
0
    def validate_expectations(self,
                              model: dict = {},
                              raise_on_error: bool = False):
        with open(self.config.expectations_schema_path, "r") as schema_file:
            schema = yaml.load(schema_file, Loader=yaml.FullLoader)

        if model.get("expectations"):
            return validation.validate_object_against_schema(
                input_object=model,
                schema_object=schema,
                strict=False,
                raise_on_error=raise_on_error,
            )

        return validation.Status(error_object={})