Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 32 additions & 37 deletions unittests/test_duplicates.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import shutil
import sys
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Generator

import pytest

sys.path.insert(
0, str(Path(__file__).resolve().parents[1] / "src")
) # it is needed just one time to be able to import the module
from bo4egenerator import duplicates

from bo4egenerator import duplicates # pylint: disable=wrong-import-position
# sys.path.insert(
# 0, str(Path(__file__).resolve().parents[1] / "src")
# ) # it is needed just one time to be able to import the module
# You don't need this. Follow the instructions from the python-template repository.
# You have to mark the src directory as a source root (blue) in PyCharm + the unittests directory as a test root (green)
Comment on lines +9 to +13
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grafik



class TestRemoveDuplicateDefinitions:
Expand All @@ -18,48 +19,42 @@ class TestRemoveDuplicateDefinitions:
"""

@pytest.fixture
def temp_dir(self) -> str: # type: ignore
"""Create a temporary directory for the test."""
with TemporaryDirectory() as temp_dir:
yield temp_dir

@pytest.fixture
def output_dir(self, temp_dir: str) -> str: # type: ignore
def output_dir(self, tmp_path: Path) -> Generator[Path, None, None]:
"""Copy the generated classes to the temporary directory."""
project_root = Path.cwd() / "unittests" / "test-data"
test_data_root = Path(__file__).parent / "test-data"
# Path to the source directory containing the generated C# files with quicktype
source_dir = project_root / "generated-classes"
source_dir = test_data_root / "generated-classes"
# Path to the temporary directory where the processed files will be saved
output_dir = Path(temp_dir) / "dotnet-classes"
output_dir = tmp_path / "dotnet-classes"
output_dir.mkdir(exist_ok=True)
shutil.copytree(source_dir, output_dir, dirs_exist_ok=True)
yield output_dir

def test_remove_duplicate_definitions_with_setup_and_teardown(self) -> None:
def test_remove_duplicate_definitions_with_setup_and_teardown(self, output_dir: Path) -> None:
"""
Test case for removing duplicate class and enum definitions from C# files using setup and teardown.
"""
with TemporaryDirectory() as output_dir:
# Set up the test environment
project_root = Path.cwd() / "unittests" / "test-data"
source_dir = project_root / "generated-classes"
shutil.copytree(source_dir, output_dir, dirs_exist_ok=True)
# Set up the test environment
test_data_root = Path(__file__).parent / "test-data"
source_dir = test_data_root / "generated-classes"
shutil.copytree(source_dir, output_dir, dirs_exist_ok=True)

# Run the process_directory function on the test directory
duplicates.process_directory(Path(output_dir))
# Run the process_directory function on the test directory
duplicates.process_directory(Path(output_dir))

# Verify that duplicate class and enum definitions are removed
bo_file_path = output_dir / Path("bo") / Path("Angebot.cs")
with open(bo_file_path, "r", encoding="utf-8") as file:
bo_content = file.read()
# Verify that duplicate class and enum definitions are removed
bo_file_path = output_dir / Path("bo") / Path("Angebot.cs")
with open(bo_file_path, "r", encoding="utf-8") as file:
bo_content = file.read()

com_file_path = output_dir / Path("com") / Path("Adresse.cs")
with open(com_file_path, "r", encoding="utf-8") as file:
com_content = file.read()
com_file_path = output_dir / Path("com") / Path("Adresse.cs")
with open(com_file_path, "r", encoding="utf-8") as file:
com_content = file.read()

# Check that "public enum Typ" is not in Angebot.cs
assert "public enum Typ" not in bo_content, "`public enum Typ` should have been removed from Angebot.cs"
# Check that "public enum Typ" is not in Angebot.cs
assert "public enum Typ" not in bo_content, "`public enum Typ` should have been removed from Angebot.cs"

# Check that "public enum Landescode" is not in Adresse.cs
assert (
"public enum Landescode" not in com_content
), "`public enum Landescode` should have been removed from Adresse.cs"
# Check that "public enum Landescode" is not in Adresse.cs
assert (
"public enum Landescode" not in com_content
), "`public enum Landescode` should have been removed from Adresse.cs"
10 changes: 5 additions & 5 deletions unittests/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pathlib import Path

from bo4egenerator import generator # pylint: disable=wrong-import-position
from bo4egenerator import generator


class TestGenerator:
Expand All @@ -20,12 +20,12 @@ def test_generate_csharp_classes(self) -> None:
The Angebot schema file is used as an example for the test.
It also checks if the generated `Angebot.cs` file exists in the output directory.
"""
project_root = Path(__file__).parent / "test-data"
schemas_dir = project_root / "schemas"
output_dir = project_root / "generated-classes"
test_data_root = Path(__file__).parent / "test-data"
schemas_dir = test_data_root / "schemas"
output_dir = test_data_root / "generated-classes"
quicktype_executable = "quicktype" # Assuming it's in PATH on Linux (GH Actions)

generator.generate_csharp_classes(project_root, schemas_dir, output_dir, quicktype_executable)
generator.generate_csharp_classes(test_data_root, schemas_dir, output_dir, quicktype_executable)

angebot_file = Path(output_dir / "bo" / "Angebot.cs")
assert angebot_file.exists()