Skip to content
Snippets Groups Projects
Commit f62037c1 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

FIX: unittests

parent 96e5eb76
No related branches found
No related tags found
4 merge requests!100WIP: Filling XLSX: Seems to be working.,!94ENH: add framework for converting json schema into table templates,!93Filling XLSX: Everything except multiple choice.,!92ENH: xlsx template generator
Pipeline #47924 failed
......@@ -89,7 +89,7 @@ class TableTemplateGenerator(ABC):
-------
dict
The structure of the dict, lets call it ``sheets`` is as follows:
``sheets[sheetname][colname]= (COL_TYPE, description [path])``
``sheets[sheetname][colname]= (COL_TYPE, description, [path])``
I.e. the top level dict contains sheet names as keys and dicts as values.
The inner dict has column names as keys and tuples as values. The tuples have the
column type as the first element, the description of the corresponding property as
......@@ -223,7 +223,7 @@ class XLSXTemplateGenerator(TableTemplateGenerator):
definition dict
You need to pass the dict of a single sheet to this function.
"""
return max([len(path) for _, _, path in sheetdef.values()])
return max([len(path) for _, _, path in sheetdef.values()])
@staticmethod
def _get_ordered_cols(sheetdef: dict) -> list:
......
......@@ -104,7 +104,7 @@ def test_generate_sheets_from_schema():
assert "Training" in sdef
tdef = sdef['Training']
assert 'name' in tdef
assert tdef['name'] == (ColumnType.SCALAR, ["Training", 'name'])
assert tdef['name'] == (ColumnType.SCALAR, "The name of the Record to be created", ["Training", 'name'])
# example case
with open(rfp("model_schema.json")) as sfi:
......@@ -115,29 +115,30 @@ def test_generate_sheets_from_schema():
foreign_keys={'Training': (['date', 'url'], {})})
assert "Training" in sdef
tdef = sdef['Training']
assert tdef['date'] == (ColumnType.SCALAR, ["Training", 'date'])
assert tdef['url'] == (ColumnType.SCALAR, ["Training", 'url'])
assert tdef['supervisor.family_name'] == (ColumnType.SCALAR, ["Training", 'supervisor',
'family_name'])
assert tdef['supervisor.given_name'] == (ColumnType.SCALAR, ["Training", 'supervisor',
'given_name'])
assert tdef['supervisor.Organisation.name'] == (ColumnType.SCALAR, ["Training", 'supervisor',
'Organisation', 'name'])
assert tdef['supervisor.Organisation.Country'] == (ColumnType.SCALAR, ["Training", 'supervisor',
'Organisation', 'Country'])
assert tdef['duration'] == (ColumnType.SCALAR, ["Training", 'duration'])
assert tdef['participants'] == (ColumnType.SCALAR, ["Training", 'participants'])
assert tdef['subjects'] == (ColumnType.LIST, ["Training", 'subjects'])
assert tdef['remote'] == (ColumnType.SCALAR, ["Training", 'remote'])
assert tdef['date'] == (ColumnType.SCALAR, 'date', ["Training", 'date'])
assert tdef['url'] == (ColumnType.SCALAR, 'url', ["Training", 'url'])
assert tdef['supervisor.family_name'] == (ColumnType.SCALAR, None, ["Training", 'supervisor',
'family_name'])
assert tdef['supervisor.given_name'] == (ColumnType.SCALAR, None, ["Training", 'supervisor',
'given_name'])
assert tdef['supervisor.Organisation.name'] == (ColumnType.SCALAR, 'The name of the Record to be created', ["Training", 'supervisor',
'Organisation', 'name'])
assert tdef['supervisor.Organisation.Country'] == (ColumnType.SCALAR, None, ["Training", 'supervisor',
'Organisation', 'Country'])
assert tdef['duration'] == (ColumnType.SCALAR, None, ["Training", 'duration'])
assert tdef['participants'] == (ColumnType.SCALAR, None, ["Training", 'participants'])
assert tdef['subjects'] == (ColumnType.LIST, None, ["Training", 'subjects'])
assert tdef['remote'] == (ColumnType.SCALAR, None, ["Training", 'remote'])
cdef = sdef['Training.coach']
assert cdef['coach.family_name'] == (ColumnType.SCALAR, ["Training", 'coach', 'family_name'])
assert cdef['coach.given_name'] == (ColumnType.SCALAR, ["Training", 'coach', 'given_name'])
assert cdef['coach.Organisation.name'] == (ColumnType.SCALAR, ["Training", 'coach',
'Organisation', 'name'])
assert cdef['coach.Organisation.Country'] == (ColumnType.SCALAR, ["Training", 'coach',
'Organisation', 'Country'])
assert cdef['date'] == (ColumnType.FOREIGN, ["Training", 'date'])
assert cdef['url'] == (ColumnType.FOREIGN, ["Training", 'url'])
assert cdef['coach.family_name'] == (ColumnType.SCALAR, None, ["Training", 'coach', 'family_name'])
assert cdef['coach.given_name'] == (ColumnType.SCALAR, None, ["Training", 'coach', 'given_name'])
assert cdef['coach.Organisation.name'] == (ColumnType.SCALAR, 'The name of the Record to be created', ["Training", 'coach',
'Organisation', 'name'])
assert cdef['coach.Organisation.Country'] == (ColumnType.SCALAR, None, ["Training", 'coach',
'Organisation', 'Country'])
print(cdef)
assert cdef['Training.date'] == (ColumnType.FOREIGN, "see sheet 'Training'", ["Training", 'date'])
assert cdef['Training.url'] == (ColumnType.FOREIGN, "see sheet 'Training'", ["Training", 'url'])
def test_get_foreign_keys():
......@@ -149,7 +150,7 @@ def test_get_foreign_keys():
assert ['a'] == generator._get_foreign_keys(fkd, ['Training'])
fkd = {"Training": {'hallo'}}
with pytest.raises(ValueError, match=r"A foreign key definition is missing for path:\n\[\]\n{'Training': \{'hallo'\}\}"):
with pytest.raises(ValueError, match=r"A foreign key definition is missing for path:\n\['Training'\]\n{'Training': \{'hallo'\}\}"):
generator._get_foreign_keys(fkd, ['Training'])
fkd = {"Training": (['a'], {'b': ['c']})}
......@@ -160,8 +161,8 @@ def test_get_foreign_keys():
def test_get_max_path_length():
assert 4 == XLSXTemplateGenerator._get_max_path_length({'a': (1, [1, 2, 3]),
'b': (2, [1, 2, 3, 4])})
assert 4 == XLSXTemplateGenerator._get_max_path_length({'a': (1, 'desc', [1, 2, 3]),
'b': (2, 'desc', [1, 2, 3, 4])})
def test_template_generator():
......@@ -189,16 +190,13 @@ def test_template_generator():
assert ws.column_dimensions['A'].hidden is True
rp = '/home/henrik/CaosDB/management/external/dimr/eingabemaske/django/laforms/settings/DevelSchema.json'
with open(rp) as sfi:
schema = json.load(sfi)
generator.generate(schema=schema,
foreign_keys={},
filepath=path)
os.system(f'libreoffice {path}')
# rp = '/home/henrik/CaosDB/management/external/dimr/eingabemaske/django/laforms/settings/DevelSchema.json'
# with open(rp) as sfi:
# schema = json.load(sfi)
# generator.generate(schema=schema,
# foreign_keys={},
# filepath=path)
# os.system(f'libreoffice {path}')
# TODO test colisions of sheet or colnames
# TODO test escaping of values
......
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