Skip to content

Commit

Permalink
Moved duplicated_property_types to utils and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed Feb 6, 2025
1 parent c6f078d commit aa69f61
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 36 deletions.
2 changes: 1 addition & 1 deletion bam_masterdata/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from openpyxl import Workbook
from rdflib import Graph

from bam_masterdata.cli.duplicated_property_types import duplicated_property_types
from bam_masterdata.cli.entities_to_excel import entities_to_excel
from bam_masterdata.cli.entities_to_json import entities_to_json
from bam_masterdata.cli.entities_to_rdf import entities_to_rdf
from bam_masterdata.cli.fill_masterdata import MasterdataCodeGenerator
from bam_masterdata.logger import logger
from bam_masterdata.utils import (
delete_and_create_dir,
duplicated_property_types,
import_module,
listdir_py_modules,
)
Expand Down
34 changes: 0 additions & 34 deletions bam_masterdata/cli/duplicated_property_types.py

This file was deleted.

1 change: 1 addition & 0 deletions bam_masterdata/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .utils import (
code_to_class_name,
delete_and_create_dir,
duplicated_property_types,
import_module,
listdir_py_modules,
load_validation_rules,
Expand Down
43 changes: 43 additions & 0 deletions bam_masterdata/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import glob
import importlib.util
import inspect
import json
import os
import re
import shutil
from itertools import chain
from typing import TYPE_CHECKING, Any, Optional
Expand Down Expand Up @@ -160,3 +162,44 @@ def load_validation_rules(
except json.JSONDecodeError as e:
logger.error(f"Error parsing validation rules JSON: {e}")
raise ValueError(f"Error parsing validation rules JSON: {e}")


from pathlib import Path


def duplicated_property_types(module_path: str, logger: "BoundLoggerLazyProxy") -> dict:
"""
Find the duplicated property types in a module specified by `module_path` and returns a dictionary
containing the duplicated property types class names as keys and the lines where they matched as values.
Args:
module_path (str): The path to the module containing the property types.
logger (BoundLoggerLazyProxy): The logger to log messages.
Returns:
dict: A dictionary containing the duplicated property types class names as keys and the
lines where they matched as values.
"""
duplicated_props: dict = {}
module = import_module(module_path=module_path)
source_code = inspect.getsource(module)
for name, _ in inspect.getmembers(module):
if name.startswith("_") or name == "PropertyTypeDef":
continue

pattern = rf"^\s*{name} *= *PropertyTypeDef"

# Find all matching line numbers
matches = [
i + 1 # Convert to 1-based index
for i, line in enumerate(source_code.splitlines())
if re.match(pattern, line)
]
if len(matches) > 1:
duplicated_props[name] = matches
if duplicated_props:
logger.critical(
f"Found {len(duplicated_props)} duplicated property types. These are stored in a dictionary "
f"where the keys are the names of the variables in property_types.py and the values are the lines in the module: {duplicated_props}"
)
return duplicated_props
23 changes: 23 additions & 0 deletions tests/data/utils/example_prop_types_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from bam_masterdata.metadata.definitions import PropertyTypeDef

PropA = PropertyTypeDef(
code="PROPA",
description="""repeated property""",
data_type="VARCHAR",
property_label="A1",
)


PropB = PropertyTypeDef(
code="PROPB",
description="""non-repeated property""",
data_type="VARCHAR",
property_label="B",
)

PropA = PropertyTypeDef(
code="PROPA",
description="""repeated property""",
data_type="VARCHAR",
property_label="A2",
)
16 changes: 16 additions & 0 deletions tests/data/utils/example_prop_types_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from bam_masterdata.metadata.definitions import PropertyTypeDef

PropA = PropertyTypeDef(
code="PROPA",
description="""non-repeated property""",
data_type="VARCHAR",
property_label="A",
)


PropB = PropertyTypeDef(
code="PROPB",
description="""non-repeated property""",
data_type="VARCHAR",
property_label="B",
)
24 changes: 23 additions & 1 deletion tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from bam_masterdata.utils import (
code_to_class_name,
delete_and_create_dir,
duplicated_property_types,
import_module,
listdir_py_modules,
load_validation_rules,
Expand Down Expand Up @@ -62,7 +63,12 @@ def test_delete_and_create_dir(
"warning",
),
# No Python files found in the directory
("./tests/data", [], "No Python files found in the directory.", "info"),
(
"./tests/data/empty",
[],
"No Python files found in the directory.",
"info",
),
# Python files found in the directory
(
"./tests/utils",
Expand Down Expand Up @@ -270,3 +276,19 @@ def test_load_validation_rules(
assert result == expected_output
assert cleared_log_storage[-1]["event"] == expected_log
assert cleared_log_storage[-1]["level"] == "info"


@pytest.mark.parametrize(
"path, result",
[
# PropA appears twice
("tests/data/utils/example_prop_types_1.py", {"PropA": [3, 18]}),
# None duplicated
("tests/data/utils/example_prop_types_2.py", {}),
],
)
def test_duplicated_property_types(cleared_log_storage: list, path: str, result: dict):
assert result == duplicated_property_types(path, logger)
if result:
assert cleared_log_storage[0]["level"] == "critical"
assert "Found 1 duplicated property types" in cleared_log_storage[0]["event"]

0 comments on commit aa69f61

Please sign in to comment.