Skip to content
Snippets Groups Projects
Commit a7cb6bae authored by lukas leufen's avatar lukas leufen
Browse files

Merge branch 'lukas_issue240_test_new-tests-for-datastore' into 'develop'

Resolve "new tests for datastore"

See merge request toar/mlair!214
parents 0ac52f88 5a8f107a
No related branches found
No related tags found
3 merge requests!226Develop,!225Resolve "release v1.2.0",!214Resolve "new tests for datastore"
Pipeline #55559 passed
...@@ -65,7 +65,7 @@ class CorrectScope: ...@@ -65,7 +65,7 @@ class CorrectScope:
return self.wrapper(*args, **kwargs) return self.wrapper(*args, **kwargs)
def __get__(self, instance, cls): 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) return types.MethodType(self, instance)
@staticmethod @staticmethod
...@@ -101,6 +101,7 @@ class CorrectScope: ...@@ -101,6 +101,7 @@ class CorrectScope:
class TrackParameter: class TrackParameter:
"""Hint: Tracking is not working for static methods."""
def __init__(self, func): def __init__(self, func):
"""Construct decorator.""" """Construct decorator."""
...@@ -114,7 +115,7 @@ class TrackParameter: ...@@ -114,7 +115,7 @@ class TrackParameter:
return self.__wrapped__(*args, **kwargs) return self.__wrapped__(*args, **kwargs)
def __get__(self, instance, cls): 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) return types.MethodType(self, instance)
def track(self, tracker_obj, *args): def track(self, tracker_obj, *args):
...@@ -312,7 +313,7 @@ class DataStoreByVariable(AbstractDataStore): ...@@ -312,7 +313,7 @@ class DataStoreByVariable(AbstractDataStore):
if name not in self._store.keys(): if name not in self._store.keys():
self._store[name] = {} self._store[name] = {}
self._store[name][scope] = obj self._store[name][scope] = obj
if log: if log: # pragma: no cover
logging.debug(f"set: {name}({scope})={obj}") logging.debug(f"set: {name}({scope})={obj}")
@CorrectScope @CorrectScope
...@@ -463,7 +464,7 @@ class DataStoreByScope(AbstractDataStore): ...@@ -463,7 +464,7 @@ class DataStoreByScope(AbstractDataStore):
if scope not in self._store.keys(): if scope not in self._store.keys():
self._store[scope] = {} self._store[scope] = {}
self._store[scope][name] = obj self._store[scope][name] = obj
if log: if log: # pragma: no cover
logging.debug(f"set: {name}({scope})={obj}") logging.debug(f"set: {name}({scope})={obj}")
@CorrectScope @CorrectScope
......
...@@ -3,7 +3,8 @@ __date__ = '2019-11-22' ...@@ -3,7 +3,8 @@ __date__ = '2019-11-22'
import pytest 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 from mlair.helpers.datastore import NameNotFoundInDataStore, NameNotFoundInScope, EmptyScope
...@@ -339,3 +340,52 @@ class TestCorrectScope: ...@@ -339,3 +340,52 @@ class TestCorrectScope:
assert self.function1(21) == (21, "general", 44) assert self.function1(21) == (21, "general", 44)
assert self.function1(55, "sub", 34) == (55, "general.sub", 34) assert self.function1(55, "sub", 34) == (55, "general.sub", 34)
assert self.function1("string", b=99, scope="tester") == ("string", "general.tester", 99) 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment