Skip to content

Commit

Permalink
✅Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-sheese committed Jan 16, 2024
1 parent 4f01c80 commit c7461d1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
8 changes: 8 additions & 0 deletions unittests/test__schema.py
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)
5 changes: 3 additions & 2 deletions unittests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 29 additions & 0 deletions unittests/test_parser.py
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)
45 changes: 45 additions & 0 deletions unittests/test_sqlparser.py
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)

0 comments on commit c7461d1

Please sign in to comment.