Skip to content
Snippets Groups Projects
Commit d753df87 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

TST(yaml-model-parser): unit test for data model comparison

parent dc5554d0
No related branches found
No related tags found
2 merge requests!128MNT: Added a warning when column metadata is not configured, and a better...,!115add datatype, unit and description to properties that are part of Records
Pipeline #57809 failed
...@@ -30,8 +30,6 @@ from caosadvancedtools.models.parser import (TwiceDefinedException, ...@@ -30,8 +30,6 @@ from caosadvancedtools.models.parser import (TwiceDefinedException,
from linkahead.apiutils import compare_entities from linkahead.apiutils import compare_entities
from pytest import mark, raises from pytest import mark, raises
from unittests.mock import Mock
def to_file(string): def to_file(string):
f = NamedTemporaryFile(mode="w", delete=False) f = NamedTemporaryFile(mode="w", delete=False)
...@@ -676,12 +674,14 @@ test_reference: ...@@ -676,12 +674,14 @@ test_reference:
# comparison with a version taken from a LinkAhead instance will have these attributes. # comparison with a version taken from a LinkAhead instance will have these attributes.
# Furthermore, RT2 will be set as the datatype **in object version** in the yaml definition, while # Furthermore, RT2 will be set as the datatype **in object version** in the yaml definition, while
# it is an ID in case of the version from the LinkAhead instance. # it is an ID in case of the version from the LinkAhead instance.
# p_foo = db.Property(name="foo", datatype="INTEGER", description="bla bla", unit="m")
# rt2 = db.RecordType(name="RT2")
# rt1 = db.RecordType(name="RT1")
# rt1.add_property(p_foo)
# rt1.add_property(rt2)
p_foo = db.Property(name="foo", datatype="INTEGER", description="bla bla", unit="m") # existing_entities = [
rt2 = db.RecordType(name="RT2") # p_foo, rt2, rt1]
rt1 = db.RecordType(name="RT1")
rt1.add_property(p_foo)
rt1.add_property(rt2)
server_response = """ server_response = """
<Entities> <Entities>
...@@ -710,26 +710,39 @@ test_reference: ...@@ -710,26 +710,39 @@ test_reference:
c2 = compare_entities(model["RT1"], entities[1]) c2 = compare_entities(model["RT1"], entities[1])
c3 = compare_entities(model["RT2"], entities[2]) c3 = compare_entities(model["RT2"], entities[2])
c4 = compare_entities(model["test_reference"], entities[3]) c4 = compare_entities(model["test_reference"], entities[3])
for cs in (c1, c2, c3, c4): for cs in (c1, c2, c3, c4):
assert "id" not in cs[0] assert "id" in cs[0]
assert cs[0]["id"] is None
assert "id" in cs[1] assert "id" in cs[1]
assert cs[1]["id"] is not None
mq = Mock() # The server response would be the same as the xml above:
def get_existing_entities(ent_cont):
def mq_init(self, query): return entities
self.query = query
def mq_execute(self, unique=True): class MockQuery:
pass def __init__(self, q):
self.q = q
mq.__init__ = mq_init def execute(self, unique=True):
mq.execute.side_effect = mq_execute id = int(self.q.split("=")[1])
for existing_ent in entities:
if existing_ent.id == id:
return existing_ent
return None
caosadvancedtools.models.parser.db.Query = mq model.get_existing_entities = get_existing_entities
caosadvancedtools.models.parser.db.Query = MockQuery
caosadvancedtools.models.parser.db.Container.update = Mock()
caosadvancedtools.models.parser.db.Container.insert = Mock()
model.sync_data_model(True, True) model.sync_data_model(True, True)
assert caosadvancedtools.models.parser.db.Container.update.called
stdout, stderr = capfd.readouterr() assert not caosadvancedtools.models.parser.db.Container.insert.called
output, err = capfd.readouterr()
print(output)
assert False
# TODO: test that there were no changes required # TODO: test that there were no changes required
...@@ -766,9 +779,11 @@ RT: ...@@ -766,9 +779,11 @@ RT:
model.get_existing_entities = get_existing_entities model.get_existing_entities = get_existing_entities
caosadvancedtools.models.parser.db.Query = MockQuery caosadvancedtools.models.parser.db.Query = MockQuery
caosadvancedtools.models.parser.db.Container.update = Mock() caosadvancedtools.models.parser.db.Container.update = Mock()
caosadvancedtools.models.parser.db.Container.insert = Mock()
model.sync_data_model(True, True) model.sync_data_model(True, True)
assert caosadvancedtools.models.parser.db.Container.update.called assert caosadvancedtools.models.parser.db.Container.update.called
assert not caosadvancedtools.models.parser.db.Container.insert.called
output, err = capfd.readouterr() output, err = capfd.readouterr()
print(output) print(output)
assert "version from the yaml file: TEXT" in output assert "version from the yaml file: TEXT" in output
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment