Skip to content

Commit

Permalink
Added testing for entities_to_json and entities_to_excel
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed Dec 19, 2024
1 parent 51dc101 commit 16bbc4c
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 15 deletions.
23 changes: 23 additions & 0 deletions bam_masterdata/cli/entities_to_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ def entities_to_excel(
"""
def_members = inspect.getmembers(definitions_module, inspect.isclass)
module = import_module(module_path=module_path)

# Special case of `PropertyTypeDef` in `property_types.py`
if 'property_types.py' in module_path:
for name, obj in inspect.getmembers(module):
if name.startswith('_') or name == 'PropertyTypeDef':
continue

# Entity title
worksheet.append([obj.excel_name])

# Entity header definitions and values
worksheet.append(obj.excel_headers)
row = []
for f_set in obj.model_fields.keys():
if f_set == 'data_type':
val = obj.data_type.value
else:
val = getattr(obj, f_set)
row.append(val)
worksheet.append(row)
return None

# All other datamodel modules
for _, obj in inspect.getmembers(module, inspect.isclass):
# Ensure the class has the `to_json` method
if not hasattr(obj, 'defs') or not callable(getattr(obj, 'to_json')):
Expand Down
33 changes: 18 additions & 15 deletions bam_masterdata/cli/entities_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ def entities_to_json(
export_dir, os.path.basename(module_path).replace('.py', '')
)
delete_and_create_dir(directory_path=module_export_dir, logger=logger)

# Special case of `PropertyTypeDef` in `property_types.py`
if 'property_types.py' in module_path:
for name, obj in inspect.getmembers(module):
if name.startswith('_') or name == 'PropertyTypeDef':
continue
try:
json_data = json.dumps(obj.model_dump(), indent=2)
output_file = os.path.join(module_export_dir, f'{obj.code}.json')
with open(output_file, 'w', encoding='utf-8') as f:
f.write(json_data)

click.echo(f'Saved JSON for class {name} to {output_file}')
except Exception as err:
click.echo(f'Failed to process class {name} in {module_path}: {err}')
return None

# All other datamodel modules
for name, obj in inspect.getmembers(module, inspect.isclass):
# Ensure the class has the `to_json` method
if not hasattr(obj, 'defs') or not callable(getattr(obj, 'to_json')):
Expand All @@ -47,18 +65,3 @@ def entities_to_json(
click.echo(f'Saved JSON for class {name} to {output_file}')
except Exception as err:
click.echo(f'Failed to process class {name} in {module_path}: {err}')

# Special case of `PropertyTypeDef` in `property_types.py`
if 'property_types.py' in module_path:
for name, obj in inspect.getmembers(module):
if name.startswith('_') or name == 'PropertyTypeDef':
continue
try:
json_data = json.dumps(obj.model_dump(), indent=2)
output_file = os.path.join(module_export_dir, f'{obj.code}.json')
with open(output_file, 'w', encoding='utf-8') as f:
f.write(json_data)

click.echo(f'Saved JSON for class {name} to {output_file}')
except Exception as err:
click.echo(f'Failed to process class {name} in {module_path}: {err}')
83 changes: 83 additions & 0 deletions tests/cli/test_entities_to_excel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os

import pytest
from openpyxl import Workbook

from bam_masterdata.cli.entities_to_excel import entities_to_excel
from bam_masterdata.utils import import_module


@pytest.mark.parametrize(
'module_name, entity_name, entity_headers',
[
(
'collection_types',
'EXPERIMENT_TYPE',
['Version', 'Code', 'Description', 'Validation script'],
),
# ('dataset_types', 'DATASET_TYPE', ['Version', 'Code', 'Description', 'Validation script']), # ! this module does not have classes yet
(
'object_types',
'SAMPLE_TYPE',
[
'Version',
'Code',
'Description',
'Validation script',
'Generated code prefix',
'Auto generated codes',
],
),
(
'property_types',
'PROPERTY_TYPE',
[
'Version',
'Code',
'Description',
'Property label',
'Data type',
'Vocabulary code',
'Metadata',
'Dynamic script',
],
),
(
'vocabulary_types',
'VOCABULARY_TYPE',
['Version', 'Code', 'Description', 'Url template'],
),
],
)
def test_entities_to_excel(
module_name: str, entity_name: str, entity_headers: list[str]
):
"""Test the `entities_to_excel` function."""
definitions_module = import_module(
module_path='./bam_masterdata/metadata/definitions.py'
)

# Get the Python modules to process the datamodel
module_path = os.path.join('./bam_masterdata/datamodel', f'{module_name}.py')
wb = Workbook()
ws = wb.active
ws.title = (
os.path.basename(module_path).capitalize().replace('.py', '').replace('_', ' ')
)

entities_to_excel(
worksheet=ws,
module_path=module_path,
definitions_module=definitions_module,
)

assert len(wb.worksheets) == 1
# Header for type of entity
assert ws.cell(row=1, column=1).value == entity_name
# Headers for definitions
col = []
for c in range(1, 101):
if not ws.cell(row=2, column=c).value:
break
col.append(ws.cell(row=2, column=c).value)
assert col == entity_headers
42 changes: 42 additions & 0 deletions tests/cli/test_entities_to_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
import os
import shutil

import pytest

from bam_masterdata.cli.entities_to_json import entities_to_json
from bam_masterdata.logger import logger


@pytest.mark.parametrize(
'module_name',
[
('collection_types'),
# ('dataset_types', False), # ! this module does not have classes yet
('object_types'),
('property_types'),
('vocabulary_types'),
],
)
def test_entities_to_json(module_name: str):
"""Test the `entities_to_json` function."""
export_dir = './tests/data/tmp/'
module_path = os.path.join('./bam_masterdata/datamodel', f'{module_name}.py')

entities_to_json(module_path=module_path, export_dir=export_dir, logger=logger)

module_export_dir = os.path.join(export_dir, module_name)
assert os.path.exists(export_dir)
assert len(os.listdir(module_export_dir)) > 0
assert ['.json' in f for f in os.listdir(module_export_dir)]

for file in os.listdir(module_export_dir):
with open(os.path.join(module_export_dir, file)) as f:
data = json.load(f)
# making sure the data stored in json files is correct
if module_name == 'property_types':
assert data['code'] == file.replace('.json', '')
else:
assert data['defs']['code'] == file.replace('.json', '')

shutil.rmtree(export_dir) # ! careful with this line

1 comment on commit 16bbc4c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Coverage

Coverage Report
FileStmtsMissCoverMissing
bam_masterdata
   logger.py80100% 
bam_masterdata/cli
   cli.py362222 39%
   entities_to_excel.py5333 94%
   entities_to_json.py3655 86%
bam_masterdata/datamodel
   collection_types.py70100% 
   dataset_types.py00100% 
   object_types.py110100% 
   property_types.py20100% 
   vocabulary_types.py140100% 
bam_masterdata/metadata
   definitions.py780100% 
   entities.py4511 98%
bam_masterdata/utils
   utils.py3366 82%
TOTAL3233789% 

Tests Skipped Failures Errors Time
58 1 💤 0 ❌ 0 🔥 1.246s ⏱️

Please sign in to comment.