-
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.
Added testing for entities_to_json and entities_to_excel
- Loading branch information
1 parent
51dc101
commit 16bbc4c
Showing
4 changed files
with
166 additions
and
15 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
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,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 |
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,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 |
16bbc4c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report