diff --git a/unittests/test__schema.py b/unittests/test__schema.py new file mode 100644 index 0000000..567e69c --- /dev/null +++ b/unittests/test__schema.py @@ -0,0 +1,8 @@ +from bo4e_generator.schema import camel_to_snake + + +class TestSchema: + def test_camel_to_snake(self) -> None: + camelcasestring = "LastgangKompakt" + snakecasestring = "lastgang_kompakt" + assert snakecasestring == camel_to_snake(camelcasestring) diff --git a/unittests/test_main.py b/unittests/test_main.py index f49ec68..3d507c7 100644 --- a/unittests/test_main.py +++ b/unittests/test_main.py @@ -41,8 +41,9 @@ def test_main(self): assert __version__ == "0.6.1rc13" def test_resolve_paths(self) -> None: - relative_path_input = Path(INPUT_DIR) + relative_path_input = INPUT_DIR absolute_path_input = relative_path_input.resolve() - relative_path_output = Path(OUTPUT_DIR) + relative_path_output = OUTPUT_DIR absolute_path_output = relative_path_output.resolve() assert (absolute_path_input, absolute_path_output) == resolve_paths(INPUT_DIR, OUTPUT_DIR) + assert (absolute_path_input, absolute_path_output) == resolve_paths(absolute_path_input, absolute_path_output) diff --git a/unittests/test_parser.py b/unittests/test_parser.py new file mode 100644 index 0000000..0db90c3 --- /dev/null +++ b/unittests/test_parser.py @@ -0,0 +1,29 @@ +from pathlib import Path + +from datamodel_code_generator import DataModelType, PythonVersion +from datamodel_code_generator.model import get_data_model_types + +from bo4e_generator.parser import OutputType, get_bo4e_data_model_types, remove_future_import +from bo4e_generator.schema import get_namespace + +INPUT_DIR = Path("unittests/test_data/bo4e_schemas") + + +class TestParser: + def test_get_bo4e_data_model_types(self) -> None: + namespace = get_namespace(INPUT_DIR) + data_model_types = get_bo4e_data_model_types( + DataModelType.PydanticV2BaseModel, target_python_version=PythonVersion.PY_311, namespace=namespace + ) + data_model_types_compare = get_data_model_types( + DataModelType.PydanticV2BaseModel, target_python_version=PythonVersion.PY_311 + ) + assert (data_model_types.root_model) == data_model_types_compare.root_model + assert (data_model_types.field_model) == data_model_types_compare.field_model + assert ( + data_model_types.dump_resolve_reference_action + ) == data_model_types_compare.dump_resolve_reference_action + assert (data_model_types.known_third_party) == data_model_types_compare.known_third_party + + def test_remove_future_import(self) -> None: + assert ("") == remove_future_import("from __future__ import annotations\n\n", OutputType.PYDANTIC_V2) diff --git a/unittests/test_sqlparser.py b/unittests/test_sqlparser.py new file mode 100644 index 0000000..d2d7ef9 --- /dev/null +++ b/unittests/test_sqlparser.py @@ -0,0 +1,45 @@ +from collections import defaultdict +from pathlib import Path +from typing import Any, DefaultDict + +from bo4e_generator.schema import get_namespace +from bo4e_generator.sqlparser import create_sql_field, return_ref, write_many_many_links + +INPUT_DIR = Path("unittests/test_data/bo4e_schemas") +CLASSNAME = "Angebot" +FIELDNAME = "_typ" + + +class TestSQLParser: + def test_return_ref(self): + namespace = get_namespace(INPUT_DIR) + field_from_json = namespace[CLASSNAME].schema_parsed["properties"][FIELDNAME] + reference_name = return_ref(field_from_json, "$ref") + assert reference_name == "Typ" + + def test_create_sql_field(self): + namespace = get_namespace(INPUT_DIR) + add_relation: DefaultDict[str, dict[str, Any]] = defaultdict(dict) + relation_imports: DefaultDict[str, dict[str, str]] = defaultdict(dict) + add_relation_comp: DefaultDict[str, dict[str, Any]] = defaultdict(dict) + relation_imports_comp: DefaultDict[str, dict[str, Any]] = defaultdict(dict) + add_relation, relation_imports = create_sql_field( + FIELDNAME, CLASSNAME, namespace, add_relation, relation_imports + ) + add_relation_comp.update({"Angebot": {"typ": "Typ| None= Field(Typ.ANGEBOT)"}}) + relation_imports_comp.update({"AngebotADD": {"Typ": "enum.typ"}}) + assert add_relation == add_relation_comp + assert relation_imports == relation_imports_comp + + def test_write_many_many_links(self): + links: dict[str, str] = {} + namespace = get_namespace(INPUT_DIR) + add_relation: DefaultDict[str, dict[str, Any]] = defaultdict(dict) + relation_imports: DefaultDict[str, dict[str, str]] = defaultdict(dict) + add_relation, relation_imports = create_sql_field( + "externeReferenzen", CLASSNAME, namespace, add_relation, relation_imports + ) + links = add_relation["MANY"] + file_contents = write_many_many_links(links) + keywords = ["AngebotExterneReferenzLink", "angebot_id", "externereferenz_id"] + assert all(substring in file_contents for substring in keywords)