generated from Hochfrequenz/python_template_repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |