From 80ba84e3d986c36d70127424f220fd7c5bc29207 Mon Sep 17 00:00:00 2001 From: Roman Roibu Date: Fri, 8 Nov 2019 15:46:44 +0100 Subject: [PATCH 1/2] Add storage module tests --- pupil_src/tests/test_storage.py | 104 ++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 pupil_src/tests/test_storage.py diff --git a/pupil_src/tests/test_storage.py b/pupil_src/tests/test_storage.py new file mode 100644 index 0000000000..bed5745cc6 --- /dev/null +++ b/pupil_src/tests/test_storage.py @@ -0,0 +1,104 @@ +import os +import pytest + +from storage import StorageItem, Storage, SingleFileStorage +from observable import Observable + + +def test_storage_item_public_api(): + uid1 = StorageItem.create_new_unique_id() + uid2 = StorageItem.create_new_unique_id() + + assert isinstance(uid1, str), "StorageItem.create_new_unique_id must return an instance of str" + assert len(uid1) > 0, "StorageItem.create_new_unique_id must return a non-empty string" + assert uid1 != uid2, "StorageItem.create_new_unique_id must return a unique string" + + uid1 = StorageItem.create_unique_id_from_string("foo") + uid2 = StorageItem.create_unique_id_from_string("bar") + uid3 = StorageItem.create_unique_id_from_string("foo") + + assert isinstance(uid1, str), "StorageItem.create_unique_id_from_string must return an instance of str" + assert len(uid1) > 0, "StorageItem.create_unique_id_from_string must return a non-empty string" + assert uid1 != uid2, "StorageItem.create_unique_id_from_string must return a unique string in different input" + assert uid1 == uid3, "StorageItem.create_unique_id_from_string must return the same string on same input" + + +def test_single_file_storage_public_api(tmpdir): + plugin = DummyPlugin() + storage = DummySingleFileStorage(rec_dir=tmpdir, plugin=plugin) + + item1 = DummyStorageItem(foo=999, bar=["abc", 3]) + item2 = DummyStorageItem(foo=None, bar=item1.as_tuple) + + storage.add(item1) + storage.add(item2) + assert len(storage.items) == 2, "Storage must contain the added items" + + file_path = os.path.join(tmpdir, "offline_data", storage._storage_file_name) + + assert not os.path.exists(file_path), "The storage file must not exist in the temporary directory" + storage.save_to_disk() + assert os.path.exists(file_path), "The storage file must exists after saving to disk" + + # Reset storage + storage = DummySingleFileStorage(rec_dir=tmpdir, plugin=plugin) + assert len(storage.items) == 0, "Storage must be empty on initialization" + + storage._load_from_disk() + assert len(storage.items) == 2, "Storage must load from disk all the saved items" + + deserialized1, deserialized2 = storage.items + deserialized2.bar = DummyStorageItem.from_tuple(deserialized2.bar) + + assert deserialized1.foo == deserialized2.bar.foo == 999, "Deserialization must yield the same values as the original" + assert deserialized1.bar == deserialized2.bar.bar == ["abc", 3], "Deserialization must yield the same values as the original" + assert deserialized2.foo == None, "Deserialization must yield the same values as the original" + + +################################################## + + +class DummyPlugin(Observable): + def cleanup(self): + pass + + +class DummyStorageItem(StorageItem): + version = 123 + + def __init__(self, foo, bar): + self.foo = foo + self.bar = bar + + @staticmethod + def from_tuple(tuple_): + foo, bar = tuple_ + return DummyStorageItem(foo=foo, bar=bar) + + @property + def as_tuple(self): + return (self.foo, self.bar) + + +class DummySingleFileStorage(SingleFileStorage): + @property + def _storage_file_name(self): + return "dummy_items.xyz" + + def add(self, item): + self.items.append(item) + + def delete(self, item): + self.items.remove(item) + + @property + def items(self): + try: + return self.__item_storage + except AttributeError: + self.__item_storage = [] + return self.__item_storage + + @property + def _item_class(self): + return DummyStorageItem From 30ff173e7e597d01a1a9e82e1daadcb82957a733 Mon Sep 17 00:00:00 2001 From: Roman Roibu Date: Fri, 8 Nov 2019 17:17:42 +0100 Subject: [PATCH 2/2] Add tests for video_overlay.utils.constraints --- pupil_src/tests/video_overlay/__init__.py | 0 .../tests/video_overlay/test_constraints.py | 62 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 pupil_src/tests/video_overlay/__init__.py create mode 100644 pupil_src/tests/video_overlay/test_constraints.py diff --git a/pupil_src/tests/video_overlay/__init__.py b/pupil_src/tests/video_overlay/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/pupil_src/tests/video_overlay/test_constraints.py b/pupil_src/tests/video_overlay/test_constraints.py new file mode 100644 index 0000000000..ea315516a8 --- /dev/null +++ b/pupil_src/tests/video_overlay/test_constraints.py @@ -0,0 +1,62 @@ +import pytest + +from video_overlay.utils.constraints import ( + NoConstraint, + InclusiveConstraint, + BooleanConstraint, + ConstraintedValue, + ConstraintedPosition, +) + + +def test_no_constraint(): + constraint = NoConstraint() + assert constraint.apply_to(123.4) == 123.4, "NoConstraint shouldn't constrain the input" + + +def test_inclusive_constraint(): + constraint = InclusiveConstraint() + assert constraint.apply_to(-123.4) == -123.4, "InclusiveConstraint with no arguments shouldn't constrain the input" + assert constraint.apply_to(+123.4) == +123.4, "InclusiveConstraint with no arguments shouldn't constrain the input" + + constraint = InclusiveConstraint(low=-100.0) + assert constraint.apply_to(-123.4) == -100.0, "InclusiveConstraint should constrain the input on the lower bound" + assert constraint.apply_to(+123.4) == +123.4, "InclusiveConstraint shouldn't constrain the input on the upper bound" + + constraint = InclusiveConstraint(high=+100.0) + assert constraint.apply_to(-123.4) == -123.4, "InclusiveConstraint shouldn't constrain the input on the lower bound" + assert constraint.apply_to(+123.4) == +100.0, "InclusiveConstraint should constrain the input on the upper bound" + + constraint = InclusiveConstraint(low=-100.0, high=+100.0) + assert constraint.apply_to(-123.4) == -100.0, "InclusiveConstraint should constrain the input on the lower bound" + assert constraint.apply_to(+123.4) == +100.0, "InclusiveConstraint should constrain the input on the upper bound" + + +def test_boolean_constraint(): + constraint = BooleanConstraint() + assert constraint.apply_to(0.0) == False, "" + assert constraint.apply_to(1.0) == True, "" + + +def test_constrainted_value(): + val = ConstraintedValue(5) + assert val.value == 5 + + val.value = -123.4 + assert val.value == -123.4 + + val.value = +123.4 + assert val.value == +123.4 + + val.constraint = InclusiveConstraint(low=-100.0, high=+100.0) + val.value = 5 + assert val.value == 5 + + val.value = -123.4 + assert val.value == -100.0 + + val.value = +123.4 + assert val.value == +100.0 + + del val.constraint + assert val.value == +123.4