diff --git a/mlair/helpers/datastore.py b/mlair/helpers/datastore.py index b4615216000d887f16e6ed30d97215a261e12c6d..d6c977c717c5ef869fdba517fb36fcd55cfe3961 100644 --- a/mlair/helpers/datastore.py +++ b/mlair/helpers/datastore.py @@ -65,7 +65,7 @@ class CorrectScope: return self.wrapper(*args, **kwargs) def __get__(self, instance, cls): - """Create bound method object and supply self argument to the decorated method.""" + """Create bound method object and supply self argument to the decorated method. <Python Cookbook, p.347>""" return types.MethodType(self, instance) @staticmethod @@ -101,6 +101,7 @@ class CorrectScope: class TrackParameter: + """Hint: Tracking is not working for static methods.""" def __init__(self, func): """Construct decorator.""" @@ -114,7 +115,7 @@ class TrackParameter: return self.__wrapped__(*args, **kwargs) def __get__(self, instance, cls): - """Create bound method object and supply self argument to the decorated method.""" + """Create bound method object and supply self argument to the decorated method. <Python Cookbook, p.347>""" return types.MethodType(self, instance) def track(self, tracker_obj, *args): @@ -312,7 +313,7 @@ class DataStoreByVariable(AbstractDataStore): if name not in self._store.keys(): self._store[name] = {} self._store[name][scope] = obj - if log: + if log: # pragma: no cover logging.debug(f"set: {name}({scope})={obj}") @CorrectScope @@ -463,7 +464,7 @@ class DataStoreByScope(AbstractDataStore): if scope not in self._store.keys(): self._store[scope] = {} self._store[scope][name] = obj - if log: + if log: # pragma: no cover logging.debug(f"set: {name}({scope})={obj}") @CorrectScope diff --git a/test/test_datastore.py b/test/test_helpers/test_datastore.py similarity index 87% rename from test/test_datastore.py rename to test/test_helpers/test_datastore.py index 662c90bf04e11b8b4ff9647506c1981c8883f30b..1eecc576e60e5dc43b97a6e8254f8a2fea29728a 100644 --- a/test/test_datastore.py +++ b/test/test_helpers/test_datastore.py @@ -3,7 +3,8 @@ __date__ = '2019-11-22' import pytest -from mlair.helpers.datastore import AbstractDataStore, DataStoreByVariable, DataStoreByScope, CorrectScope +from mlair.helpers.datastore import AbstractDataStore, DataStoreByVariable, DataStoreByScope +from mlair.helpers.datastore import CorrectScope, TrackParameter from mlair.helpers.datastore import NameNotFoundInDataStore, NameNotFoundInScope, EmptyScope @@ -339,3 +340,52 @@ class TestCorrectScope: assert self.function1(21) == (21, "general", 44) assert self.function1(55, "sub", 34) == (55, "general.sub", 34) assert self.function1("string", b=99, scope="tester") == ("string", "general.tester", 99) + + +class TestTracking: + class Tracker: + def __init__(self): + self.tracker = [{}] + + @TrackParameter + def function2(self, arg1, arg2, arg3): + return + + @staticmethod + def function1(): + return + + def test_init(self): + track = self.Tracker() + track.function2(1, "2", "scopy") + assert track.tracker == [{1: [{"method": "function2", "scope": "scopy"}]}] + + def test_track_first_entry(self): + track = object.__new__(TrackParameter) + track.__wrapped__ = self.function1 + tracker_obj = self.Tracker() + assert len(tracker_obj.tracker[-1].keys()) == 0 + track.track(tracker_obj, "eins", 2) + assert len(tracker_obj.tracker[-1].keys()) == 1 + assert tracker_obj.tracker == [{"eins": [{"method": "function1", "scope": 2}]}] + track.track(tracker_obj, "zwei", 20) + assert len(tracker_obj.tracker[-1].keys()) == 2 + assert tracker_obj.tracker == [{"eins": [{"method": "function1", "scope": 2}], + "zwei": [{"method": "function1", "scope": 20}]}] + + def test_track_second_entry(self): + track = object.__new__(TrackParameter) + track.__wrapped__ = self.function1 + tracker_obj = self.Tracker() + assert len(tracker_obj.tracker[-1].keys()) == 0 + track.track(tracker_obj, "eins", 2) + track.track(tracker_obj, "eins", 23) + assert len(tracker_obj.tracker[-1].keys()) == 1 + assert tracker_obj.tracker == [{"eins": [{"method": "function1", "scope": 2}, + {"method": "function1", "scope": 23}]}] + + def test_decrypt_args(self): + track = object.__new__(TrackParameter) + assert track._decrypt_args(23) == (23,) + assert track._decrypt_args("test", 33, 4) == ("test", 33, 4) + assert track._decrypt_args("eins", 2) == ("eins", None, 2)