From 46a8fd8e84d66f767a8863596c92fbdec0e49e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Ja=C5=82owiecki?= Date: Mon, 18 Mar 2024 15:17:25 +0100 Subject: [PATCH] Add pydantic-based tests and improve test naming (#3) --- src/hqar/__init__.py | 5 +++-- tests/hqar/test_schema_validation.py | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/hqar/__init__.py b/src/hqar/__init__.py index 07a3a24..7cb4332 100644 --- a/src/hqar/__init__.py +++ b/src/hqar/__init__.py @@ -11,9 +11,10 @@ """ from typing import Any -from ._schema_v1 import generate_schema_v1 +from ._schema_v1 import SchemaV1, generate_schema_v1 SCHEMA_GENERATORS = {"v1": generate_schema_v1} +MODELS = {"v1": SchemaV1} LATEST_SCHEMA_VERSION = "v1" @@ -35,4 +36,4 @@ def generate_program_schema(version: str = LATEST_SCHEMA_VERSION) -> dict[str, A raise ValueError(f"Unknown schema version {version}") -__all__ = ["generate_program_schema"] +__all__ = ["generate_program_schema", "SchemaV1"] diff --git a/tests/hqar/test_schema_validation.py b/tests/hqar/test_schema_validation.py index 55282a5..d1df996 100644 --- a/tests/hqar/test_schema_validation.py +++ b/tests/hqar/test_schema_validation.py @@ -11,11 +11,12 @@ """ from pathlib import Path +import pydantic import pytest import yaml # type: ignore[import-untyped] from jsonschema import ValidationError, validate -from hqar import generate_program_schema +from hqar import SchemaV1, generate_program_schema def validate_with_v1(data): @@ -45,7 +46,7 @@ def load_valid_examples(): @pytest.mark.parametrize("input, error_path, error_message", load_invalid_examples()) -def test_invalid_schema_fail_to_validate(input, error_path, error_message): +def test_invalid_program_fails_to_validate_with_schema_v1(input, error_path, error_message): with pytest.raises(ValidationError) as err_info: validate_with_v1(input) @@ -54,5 +55,16 @@ def test_invalid_schema_fail_to_validate(input, error_path, error_message): @pytest.mark.parametrize("input", load_valid_examples()) -def test_valid_schema_fail_to_validate(input): +def test_valid_program_fails_to_validate_with_schema_v1(input): validate_with_v1(input) + + +@pytest.mark.parametrize("input", [input for input, *_ in load_invalid_examples()]) +def test_invalid_program_fails_to_validate_with_pydantic_model_v1(input): + with pytest.raises(pydantic.ValidationError): + SchemaV1.model_validate(input) + + +@pytest.mark.parametrize("input", load_valid_examples()) +def test_valid_program_succesfully_validate_with_pydantic_model_v1(input): + SchemaV1.model_validate(input)