Skip to content
Snippets Groups Projects
Commit d8e6ae1b authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'dev' into f-fix-windows-unit-tests

parents 1c3710e2 2dba6f55
No related branches found
No related tags found
2 merge requests!128MNT: Added a warning when column metadata is not configured, and a better...,!124Fix unit tests on Windows
Pipeline #58710 passed
...@@ -478,6 +478,12 @@ class XLSXConverter: ...@@ -478,6 +478,12 @@ class XLSXConverter:
if isinstance(value, datetime.date) and ( if isinstance(value, datetime.date) and (
{'type': 'string', 'format': 'date'} in subschema["anyOf"]): {'type': 'string', 'format': 'date'} in subschema["anyOf"]):
return value return value
# booleans might be retrieved as an integer or formula
if subschema.get('type') == 'boolean':
if value == 0 or isinstance(value, str) and '=false()' == value.lower():
value = False
if value == 1 or isinstance(value, str) and '=true()' == value.lower():
value = True
jsonschema.validate(value, subschema) jsonschema.validate(value, subschema)
# Finally: convert to target type # Finally: convert to target type
......
{
"Training": [
{
"date": "2023-01-01",
"url": "www.indiscale.com",
"coach": [
{
"family_name": "Sky",
"given_name": "Max",
"Organisation": "ECB"
},
{
"family_name": "Sky",
"given_name": "Min",
"Organisation": "ECB"
}
],
"supervisor": {
"family_name": "Steve",
"given_name": "Stevie",
"Organisation": "IMF"
},
"duration": 1.0,
"participants": 1,
"subjects": ["Math", "Physics"],
"remote": false
},
{
"date": "2023-01-02",
"url": "www.indiscale.com",
"supervisor": {
"family_name": "Steve",
"given_name": "Stevie",
"Organisation": "IMF"
},
"duration": 1.0,
"participants": 1,
"subjects": ["Math", "Physics"],
"remote": true
}
],
"Person": [{
"family_name": "Steve",
"given_name": "Stevie",
"Organisation": "IMF"
}]
}
File added
No preview for this file type
...@@ -26,6 +26,7 @@ import os ...@@ -26,6 +26,7 @@ import os
import re import re
from types import SimpleNamespace from types import SimpleNamespace
from typing import Optional
import jsonschema import jsonschema
import pytest import pytest
...@@ -41,7 +42,7 @@ def rfp(*pathcomponents): ...@@ -41,7 +42,7 @@ def rfp(*pathcomponents):
def convert_and_compare(xlsx_file: str, schema_file: str, known_good_file: str, def convert_and_compare(xlsx_file: str, schema_file: str, known_good_file: str,
known_good_data: dict = None, strict: bool = False, known_good_data: Optional[dict] = None, strict: bool = False,
validate: bool = True) -> dict: validate: bool = True) -> dict:
"""Convert an XLSX file and compare to a known result. """Convert an XLSX file and compare to a known result.
...@@ -77,6 +78,9 @@ def test_conversions(): ...@@ -77,6 +78,9 @@ def test_conversions():
schema_file=rfp("data/multiple_choice_schema.json"), schema_file=rfp("data/multiple_choice_schema.json"),
known_good_file=rfp("data/multiple_choice_data.json"), known_good_file=rfp("data/multiple_choice_data.json"),
strict=True) strict=True)
convert_and_compare(xlsx_file=rfp("data/simple_data_booleans.xlsx"),
schema_file=rfp("data/simple_schema.json"),
known_good_file=rfp("data/simple_data_booleans.json"))
with open(rfp("data/simple_data.json"), encoding="utf-8") as myfile: with open(rfp("data/simple_data.json"), encoding="utf-8") as myfile:
expected_datetime = json.load(myfile) expected_datetime = json.load(myfile)
...@@ -126,29 +130,40 @@ def test_error_table(): ...@@ -126,29 +130,40 @@ def test_error_table():
assert "'There is no entry in the schema" in str(caught.value) assert "'There is no entry in the schema" in str(caught.value)
assert "'Not an enum' is not one of [" in str(caught.value) assert "'Not an enum' is not one of [" in str(caught.value)
# Correct Locations # Correct Locations
matches = set()
for line in str(caught.value).split('\n'): for line in str(caught.value).split('\n'):
if "'Not a num' is not of type 'number'" in line: if "'Not a num' is not of type 'number'" in line:
assert "J7" in line assert "J7" in line
matches.add("J7")
if "'Yes a number?' is not of type 'number'" in line: if "'Yes a number?' is not of type 'number'" in line:
assert "J8" in line assert "J8" in line
matches.add("J8")
if "1.5 is not of type 'integer'" in line: if "1.5 is not of type 'integer'" in line:
assert "K7" in line assert "K7" in line
matches.add("K7")
if "1.2345 is not of type 'integer'" in line: if "1.2345 is not of type 'integer'" in line:
assert "K8" in line assert "K8" in line
matches.add("K8")
if "'There is no entry in the schema" in line: if "'There is no entry in the schema" in line:
assert "Column M" in line assert "Column M" in line
matches.add("Col M")
if "'Not an enum' is not one of [" in line: if "'Not an enum' is not one of [" in line:
assert "G8" in line assert "G8" in line
matches.add("K8")
# The next two tests could potentially be removed in the future, once we evaluate formulas.
if "'=NOT(FALSE())' is not of type 'boolean'" in line:
assert "L9" in line
matches.add("L9")
if "'=NOT(TRUE())' is not of type 'boolean'" in line:
assert "L10" in line
matches.add("L10")
assert matches == {"J7", "J8", "K7", "K8", "Col M", "K8", "L9", "L10"}
# No additional errors # No additional errors
assert str(caught.value).count("Malformed metadata: Cannot parse paths in worksheet") == 1 assert str(caught.value).count("Malformed metadata: Cannot parse paths in worksheet") == 1
assert str(caught.value).count("There is no entry in the schema") == 1 assert str(caught.value).count("There is no entry in the schema") == 1
assert str(caught.value).count("is not one of") == 1 assert str(caught.value).count("is not one of") == 1
# FIXME ToDo: Remove when boolean is fixed / when everything works as assert str(caught.value).count("is not of type") == 6
# expected, set correct number.
if "is not of type 'boolean'" in str(caught.value):
assert str(caught.value).count("is not of type") == 6
else:
assert str(caught.value).count("is not of type") == 4
# Check correct error message for completely unknown path # Check correct error message for completely unknown path
with pytest.raises(jsonschema.ValidationError) as caught: with pytest.raises(jsonschema.ValidationError) as caught:
convert.to_dict(xlsx=rfp("data/simple_data_broken_paths.xlsx"), convert.to_dict(xlsx=rfp("data/simple_data_broken_paths.xlsx"),
......
...@@ -58,7 +58,8 @@ Raise an assertion exception if they are not equal.""" ...@@ -58,7 +58,8 @@ Raise an assertion exception if they are not equal."""
"the other.") "the other.")
return return
assert isinstance(json1, list) and isinstance(json2, list), f"Is not a list, path: {path}" assert isinstance(json1, list) and isinstance(json2, list), f"Is not a list, path: {path}"
assert len(json1) == len(json2), f"Lists must have equal length, path: {path}" assert len(json1) == len(json2), (f"Lists must have equal length, path: {path}\n"
f"{json1}\n ---\n{json2}")
for idx, (el1, el2) in enumerate(zip(json1, json2)): for idx, (el1, el2) in enumerate(zip(json1, json2)):
this_path = path + [idx] this_path = path + [idx]
if isinstance(el1, dict): if isinstance(el1, dict):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment