Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand unit test coverage #1725

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions pupil_src/tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -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
Empty file.
62 changes: 62 additions & 0 deletions pupil_src/tests/video_overlay/test_constraints.py
Original file line number Diff line number Diff line change
@@ -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