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

TST: change tests for remove_unnecessary_updates

parent bd89bf74
No related branches found
No related tags found
2 merge requests!53Release 0.1,!4TST: change tests for remove_unnecessary_updates
...@@ -344,15 +344,23 @@ class Crawler(object): ...@@ -344,15 +344,23 @@ class Crawler(object):
if isinstance(val, db.Entity): if isinstance(val, db.Entity):
el.value[index] = val.id el.value[index] = val.id
def remove_unnecessary_updates(self, updateList: list[db.Record]): @staticmethod
def remove_unnecessary_updates(updateList: list[db.Record],
identified_records: list[db.Record]):
""" """
checks whether all relevant attributes (especially Property values) are equal checks whether all relevant attributes (especially Property values) are equal
Returns (in future)
-------
update list without unecessary updates
""" """
if len(updateList) != len(identified_records):
raise RuntimeError("The lists of updates and of identified records need to be of the "
"same length!")
# TODO this can now easily be changed to a function without side effect
for i in reversed(range(len(updateList))): for i in reversed(range(len(updateList))):
record = updateList[i] comp = compare_entities(updateList[i], identified_records[i])
identifiable = self.identifiableAdapter.retrieve_identifiable(record)
comp = compare_entities(record, identifiable)
identical = True identical = True
for j in range(2): for j in range(2):
# TODO: should be implemented elsewhere (?) # TODO: should be implemented elsewhere (?)
...@@ -364,6 +372,8 @@ class Crawler(object): ...@@ -364,6 +372,8 @@ class Crawler(object):
break break
for key in comp[0]["properties"]: for key in comp[0]["properties"]:
for attribute in ("datatype", "importance", "unit"): for attribute in ("datatype", "importance", "unit"):
# only make an update for those attributes if there is a value difference and
# the value in the updateList is not None
if (attribute in comp[0]["properties"][key] and if (attribute in comp[0]["properties"][key] and
comp[0]["properties"][key][attribute] is not None and comp[0]["properties"][key][attribute] is not None and
comp[0]["properties"][key][attribute] != comp[0]["properties"][key][attribute] !=
...@@ -405,7 +415,9 @@ class Crawler(object): ...@@ -405,7 +415,9 @@ class Crawler(object):
for el in to_be_updated: for el in to_be_updated:
self.replace_entities_by_ids(el) self.replace_entities_by_ids(el)
self.remove_unnecessary_updates(to_be_updated) identified_records = [self.identifiableAdapter.retrieve_identifiable(record) for record
in to_be_updated]
self.remove_unnecessary_updates(to_be_updated, identified_records)
# TODO # TODO
# self.execute_inserts_in_list(to_be_inserted) # self.execute_inserts_in_list(to_be_inserted)
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
import caosdb as db import caosdb as db
from abc import abstractmethod from abc import abstractmethod
from .utils import get_value, has_parent from .utils import get_value, has_parent
from caosdb.common.datatype import is_reference
class IdentifiableAdapter(object): class IdentifiableAdapter(object):
......
...@@ -295,29 +295,41 @@ def test_crawler_update_list(crawler, ident): ...@@ -295,29 +295,41 @@ def test_crawler_update_list(crawler, ident):
assert len(updl) == 0 assert len(updl) == 0
def test_identifiable_update(crawler, ident): def test_remove_unnecessary_updates():
# change one value in updateList and then run the synchronization: # test trvial case
meas = [r for r in crawler.updateList if r.parents[0].name == "Measurement"][0] upl = [db.Record().add_parent("A")]
meas.get_property("responsible").value = [] irs = [db.Record().add_parent("A")]
insl, updl = crawler.synchronize() Crawler.remove_unnecessary_updates(upl, irs)
assert len(updl) == 1 assert len(upl) == 0
# test property difference case
def test_identifiable_update2(crawler, ident): # TODO this should work right?
# change one unit in updateList and then run the synchronization: #upl = [db.Record().add_parent("A").add_property("a", 3)]
meas = [r for r in crawler.updateList if r.parents[0].name == "Measurement"][0] # irs = [db.Record().add_parent("A")] # ID should be s
meas.get_property("description").unit = "cm" #Crawler.remove_unnecessary_updates(upl, irs)
insl, updl = crawler.synchronize() #assert len(upl) == 1
assert len(updl) == 1
# test value difference case
upl = [db.Record().add_parent("A").add_property("a", 5)]
def test_identifiable_update3(crawler, ident): irs = [db.Record().add_parent("A").add_property("a")]
# change values of multiple records in updateList and then run the synchronization: Crawler.remove_unnecessary_updates(upl, irs)
meas = [r for r in crawler.updateList if r.parents[0].name == "Measurement"] assert len(upl) == 1
meas[0].get_property("responsible").value = [] upl = [db.Record().add_parent("A").add_property("a", 5)]
meas[3].get_property("responsible").value = [] irs = [db.Record().add_parent("A").add_property("a", 5)]
insl, updl = crawler.synchronize() Crawler.remove_unnecessary_updates(upl, irs)
assert len(updl) == 2 assert len(upl) == 0
# test unit difference case
upl = [db.Record().add_parent("A").add_property("a", unit='cm')]
irs = [db.Record().add_parent("A").add_property("a")]
Crawler.remove_unnecessary_updates(upl, irs)
assert len(upl) == 1
# test None difference case
upl = [db.Record().add_parent("A").add_property("a")]
irs = [db.Record().add_parent("A").add_property("a", 5)]
Crawler.remove_unnecessary_updates(upl, irs)
assert len(upl) == 1
def test_identifiable_adapter(): def test_identifiable_adapter():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment