Skip to content

Commit

Permalink
chore: extend tests for mapping template loader
Browse files Browse the repository at this point in the history
  • Loading branch information
yshalenyk committed Jun 28, 2024
1 parent 45a9044 commit 9f17b1a
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 19 deletions.
15 changes: 7 additions & 8 deletions nightingale/mapping/v1/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,12 @@ def normmalize_mapping_column(self, mappings):
return mappings

def read_mapping_sheet(self, sheet):
headers = [cell.value for cell in sheet[3]]
# Iterate over the rows, starting from the third row
in_extensions = False
current_extension = ""
mappings = []

for row in sheet.iter_rows(min_row=4, values_only=True):
if len(row) != len(headers):
logger.debug(f"Row {row} does not have the same number of columns as the headers")
continue
column_type = row[0]
path = row[2]
title = row[3]
Expand Down Expand Up @@ -113,6 +109,9 @@ def read_data_elements_sheet(self, sheet):
}
return elements

def get_data_elements(self):
return self.data_elements

def read_schema_sheet(self):
sheet = self.get_schema_sheet()
if sheet is None:
Expand Down Expand Up @@ -155,14 +154,14 @@ def enforce_mapping_structure(self, mappings):
+ sections["implementation"]
)

def get_mapping(self):
def get_mappings(self):
return self.mappings

def get_mapping_for(self, path):
result = []
if not path.startswith("/"):
path = "/" + path
for mapping in self.mappings:
for mapping in self.get_mappings():
if mapping["path"] == path:
result.append(mapping)
return result
Expand All @@ -172,7 +171,7 @@ def get_mapping_for(self, path):

def get_paths_for_mapping(self, key, force_publish=False):
result = []
for mapping in self.mappings:
for mapping in self.get_mappings():
if mapping["mapping"] == key:
element = self.get_element_by_mapping(key)
if element["publish"] or force_publish:
Expand All @@ -189,7 +188,7 @@ def get_arrays(self):
result.append(path)
return result

def get_scheme(self):
def get_schema(self):
return self.schema

def get_ocid_mapping(self):
Expand Down
148 changes: 137 additions & 11 deletions tests/test_mapping_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,28 @@
import openpyxl
import pytest

from nightingale.mapping.v1 import Mapping # Replace 'your_module' with the actual name of your module
from nightingale.mapping.v1 import Mapping
from nightingale.mapping.v1.config import get_longest_array_path


def test_sheets():
return {
"OCDS Schema": [
(None, "array_path", "title1", "description1", "array", "range1", "values1", "links1", None, None),
(None, "path1", "title2", "description2", "string", "range2", "values2", "links2", None, None),
(None, "", "not set", "not set", "string", "range2", "values2", "links2", None, None),
(
None,
"array_path",
"object_inside_array",
"object_inside_array",
"object",
"",
"values2",
"links2",
None,
None,
),
],
"2. Data Elements": [
(
Expand Down Expand Up @@ -41,10 +55,40 @@ def test_sheets():
None,
None,
),
(
"mapping3",
"source3",
"table3",
"",
"no",
"not set",
"not set",
"type2",
None,
None,
None,
None,
),
],
"ocds": [
("field", "id", "path", "title", "description", "mapping", None, None, None, None),
("field", "id", "path2", "title2", "description2", "mapping2", None, None, None, None),
("required_field", "id", "path2", "title2", "description2", "mapping2", None, None, None, None),
("field", "id", "path_not_mapped", "title_not_mapped", "description", "", None, None, None, None),
("span", "text", "text", "text", "description", "", None, None, None, None),
("section", "text", "Extensions are additions", "text", "description", "", None, None, None, None),
("extension", "extension_name", "extension:description", "", "", "", None, None, None, None),
(
"additional_field",
"extension_field",
"extension_field",
"extension_field_title",
"description",
"extension_mapping",
None,
None,
None,
None,
),
],
}

Expand Down Expand Up @@ -150,7 +194,7 @@ def test_read_schema_sheet(mock_load_workbook, mock_workbook, mock_config):
mock_load_workbook.return_value = mock_workbook

mapping = Mapping(mock_config)
schema = mapping.read_schema_sheet()
schema = mapping.get_schema()

expected_schema = {
"/array_path": {
Expand All @@ -171,6 +215,8 @@ def test_read_schema_sheet(mock_load_workbook, mock_workbook, mock_config):
},
}
assert schema == expected_schema
assert mapping.get_arrays() == ["/array_path"]
assert mapping.get_containing_array_path("/array_path/id") == "/array_path"


@patch("openpyxl.load_workbook")
Expand Down Expand Up @@ -226,9 +272,19 @@ def test_read_mappings(mock_load_workbook, mock_workbook, mock_config):
"mapping": "mapping2",
"is_extensions": False,
"extension": "",
"is_required": False,
"is_required": True,
"is_additional": False,
},
{
"description": "description",
"extension": "extension",
"is_additional": True,
"is_extensions": True,
"is_required": False,
"mapping": "extension_mapping",
"path": "/extension_field",
"title": "extension_field_title",
},
{
"path": "/path",
"title": "title",
Expand All @@ -246,9 +302,19 @@ def test_read_mappings(mock_load_workbook, mock_workbook, mock_config):
"mapping": "mapping2",
"is_extensions": False,
"extension": "",
"is_required": False,
"is_required": True,
"is_additional": False,
},
{
"description": "description",
"extension": "extension",
"is_additional": True,
"is_extensions": True,
"is_required": False,
"mapping": "extension_mapping",
"path": "/extension_field",
"title": "extension_field_title",
},
{
"path": "/path",
"title": "title",
Expand All @@ -266,9 +332,19 @@ def test_read_mappings(mock_load_workbook, mock_workbook, mock_config):
"mapping": "mapping2",
"is_extensions": False,
"extension": "",
"is_required": False,
"is_required": True,
"is_additional": False,
},
{
"description": "description",
"extension": "extension",
"is_additional": True,
"is_extensions": True,
"is_required": False,
"mapping": "extension_mapping",
"path": "/extension_field",
"title": "extension_field_title",
},
{
"path": "/path",
"title": "title",
Expand All @@ -286,9 +362,19 @@ def test_read_mappings(mock_load_workbook, mock_workbook, mock_config):
"mapping": "mapping2",
"is_extensions": False,
"extension": "",
"is_required": False,
"is_required": True,
"is_additional": False,
},
{
"description": "description",
"extension": "extension",
"is_additional": True,
"is_extensions": True,
"is_required": False,
"mapping": "extension_mapping",
"path": "/extension_field",
"title": "extension_field_title",
},
{
"path": "/path",
"title": "title",
Expand All @@ -306,9 +392,19 @@ def test_read_mappings(mock_load_workbook, mock_workbook, mock_config):
"mapping": "mapping2",
"is_extensions": False,
"extension": "",
"is_required": False,
"is_required": True,
"is_additional": False,
},
{
"description": "description",
"extension": "extension",
"is_additional": True,
"is_extensions": True,
"is_required": False,
"mapping": "extension_mapping",
"path": "/extension_field",
"title": "extension_field_title",
},
{
"path": "/path",
"title": "title",
Expand All @@ -326,11 +422,35 @@ def test_read_mappings(mock_load_workbook, mock_workbook, mock_config):
"mapping": "mapping2",
"is_extensions": False,
"extension": "",
"is_required": False,
"is_required": True,
"is_additional": False,
},
{
"description": "description",
"extension": "extension",
"is_additional": True,
"is_extensions": True,
"is_required": False,
"mapping": "extension_mapping",
"path": "/extension_field",
"title": "extension_field_title",
},
]
mapping.get_mappings()
assert mapping.get_mappings() == expected_mappings
assert mapping.get_mapping_for("path2") == [
{
"path": "/path2",
"title": "title2",
"description": "description2",
"mapping": "mapping2",
"is_extensions": False,
"extension": "",
"is_required": True,
"is_additional": False,
}
for _ in range(6)
]
assert mapping.mappings == expected_mappings


@patch("openpyxl.load_workbook")
Expand Down Expand Up @@ -462,10 +582,16 @@ def test_is_array_path(mock_load_workbook, mock_workbook, mock_config):

mapping = Mapping(mock_config)
mapping.schema = mapping.read_schema_sheet()

assert mapping.is_array_path("/array_path") is True
assert mapping.is_array_path("/path2") is False


def test_get_longest_array_path():
arrays = ["/root/level1/level2", "/root/level1", "/root/level1/level2/level3"]
path = "/root/level1/level2/element"
result = get_longest_array_path(arrays, path)
assert result == "/root/level1/level2"


if __name__ == "__main__":
pytest.main()

0 comments on commit 9f17b1a

Please sign in to comment.