Skip to content

Commit

Permalink
Make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
matejak committed Sep 23, 2024
1 parent 729a2f4 commit dd66d94
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 49 deletions.
11 changes: 9 additions & 2 deletions estimage/local_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ def request_namespace(self, namespace):
ns = namespace
return self.ns_requests.add(ns)

def save(self, io):
def save(self, io_cls):
io = io_cls()
io.save_dict(self.storage)
io.set_ns(self.ns_to_set)

def load(self, io):
@classmethod
def erase(self, io_cls):
io = io_cls()
io.erase()

def load(self, io_cls):
io = io_cls()
data = dict()
data[tuple()] = io.load_keys_to_dict(self.requests)
for nsreq in self.ns_requests:
Expand Down
12 changes: 10 additions & 2 deletions estimage/persistence/storage/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ def load_keys_to_dict(self, keys):
def load_ns_to_dict(self, nsname):
return {nsname: GLOBAL_STORAGE[nsname]}

def erase(self):
global GLOBAL_STORAGE
GLOBAL_STORAGE = dict()
GLOBAL_STORAGE[tuple()] = dict()


class LocalMemoryIO:
def __init__(self):
self.storage = dict()
self.storage[tuple()] = dict()
self.erase()

def save_dict(self, data):
self.storage.update(data)
Expand All @@ -34,3 +38,7 @@ def load_keys_to_dict(self, keys):

def load_ns_to_dict(self, nsname):
return {nsname: self.storage[nsname]}

def erase(self):
self.storage = dict()
self.storage[tuple()] = dict()
4 changes: 2 additions & 2 deletions estimage/plugins/demo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def load_data():
storage = local_storage.Storage()
storage.request_namespace(STORAGE_NS)
storage.request_namespace(STORAGE_NS + ("progress",))
storage.load(io_cls())
storage.load(io_cls)
ret = storage.get_namespace(STORAGE_NS)
vel = storage.get_namespace(STORAGE_NS + ("progress",))
ret["progress_by_id"] = {key: float(val) for key, val in vel.items()}
Expand Down Expand Up @@ -114,7 +114,7 @@ def write_data(main, progress):
storage = local_storage.Storage()
storage.set_namespace(STORAGE_NS, main)
storage.set_namespace(STORAGE_NS + ("progress",), progress)
storage.save(io_cls())
storage.save(io_cls)


def save_data(what):
Expand Down
91 changes: 48 additions & 43 deletions tests/test_local_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,25 @@
from tests.test_inidata import temp_filename


def test_stores():
storage = tm.Storage()
storage.feed_key("key", "value")
storage.feed_key("key", "val")
assert storage.get_key("key") == "val"
@pytest.fixture
def storage_with_key_value():
storage_in = tm.Storage()
storage_in.feed_key("key", "value")
return storage_in


def test_raises_exception_on_nonexistence():
storage = tm.Storage()
with pytest.raises(KeyError):
assert storage.get_key("yek")
def test_stores(storage_with_key_value):
storage_with_key_value.feed_key("key", "val")
assert storage_with_key_value.get_key("key") == "val"


def test_is_isolated():
storage = tm.Storage()
storage.feed_key("key", "value")
def test_raises_exception_on_nonexistence():
storage = tm.Storage()
with pytest.raises(KeyError):
assert storage.get_key("key")
assert storage.get_key("yek")


def test_is_isolated():
storage = tm.Storage()
storage.feed_key("key", "value")
def test_is_isolated(storage_with_key_value):
storage = tm.Storage()
with pytest.raises(KeyError):
assert storage.get_key("key")
Expand All @@ -38,80 +33,90 @@ def test_is_isolated():
@pytest.fixture(params=("memory", "ini"))
def storage_io(request, temp_filename):
choices = dict(
memory=tm_per.memory.LocalMemoryIO,
memory=tm_per.memory.GlobalMemoryIO,
ini=tm_per.ini.IniStorageIO,
)
io = choices[request.param]
io.CONFIG_FILENAME = temp_filename
tm.Storage.erase(io)
yield io


def test_wrong_load(storage_io):
storage_out = tm.Storage()
storage_out.load(storage_io())
storage_out.load(storage_io)
storage_out.request_key("ke-y")
with pytest.raises(KeyError, match="ke-y"):
storage_out.load(storage_io())
storage_out.load(storage_io)


def test_persistence(storage_io):
storage_in = tm.Storage()
storage_in.feed_key("key", "value")
storage_in.save(storage_io())
def test_persistence(storage_with_key_value, storage_io):
storage_with_key_value.save(storage_io)

storage_out = tm.Storage()
storage_out.request_key("key")
storage_out.load(storage_io())
storage_out.load(storage_io)
assert storage_out.get_key("key") == "value"


def test_erase(storage_with_key_value, storage_io):
storage_with_key_value.save(storage_io)

storage_out = tm.Storage()
storage_out.erase(storage_io)
storage_out.request_key("key")
with pytest.raises(KeyError, match="key"):
storage_out.load(storage_io)


def test_ns_key_exists(storage_io):
storage_in = tm.Storage()
storage_in.feed_key("key", "value", namespace=("ns",))
storage_in.feed_key("kez", "valuf", namespace=("first", "second"))
storage_in.save(storage_io())
storage_in.save(storage_io)

storage_out = tm.Storage()
storage_out.request_namespace(("ns",))
storage_out.request_namespace(("first", "second"))
storage_out.load(storage_io())
storage_out.load(storage_io)
assert storage_out.get_namespace(("ns",))["key"] == "value"
assert storage_out.get_namespace(("first", "second"))["kez"] == "valuf"


def test_nonexistent_ns_empty_noerror(storage_io):
storage_in = tm.Storage()
storage_in.feed_key("key", "value", namespace=("ns",))
storage_in.save(storage_io())
storage_in.save(storage_io)

storage_out = tm.Storage()
storage_out.request_namespace(("one", "one"))
storage_out.request_namespace(("ns", "one"))
storage_out.load(storage_io())
storage_out.load(storage_io)
assert not storage_out.get_namespace(("one", "one"))
assert not storage_out.get_namespace(("ns", "one"))


def test_nonns_key_doesnt_exist(storage_io):
storage_in = tm.Storage()
storage_in.feed_key("key", "value", namespace=("ns",))
storage_in.save(storage_io())
storage_in.save(storage_io)

storage_out = tm.Storage()
storage_out.request_key("key")
with pytest.raises(KeyError, match="key"):
storage_out.load(storage_io())
storage_out.load(storage_io)


def test_ns_with_data(storage_io):
storage_in = tm.Storage()
storage_in.feed_key("one", "1", namespace=("something", "numbers",))
storage_in.feed_key("two", "2", namespace=("something", "numbers",))
storage_in.save(storage_io())
storage_in.save(storage_io)

storage_out = tm.Storage()
storage_out.request_namespace(("numbers",))
storage_out.request_namespace(("something", "numbers",))
storage_out.load(storage_io())
storage_out.load(storage_io)
assert not storage_out.get_namespace(("numbers",))
numbers = storage_out.get_namespace(("something", "numbers",))
assert len(numbers) == 2
Expand All @@ -120,33 +125,33 @@ def test_ns_with_data(storage_io):
numbers["nothing"] = dict()


def test_define_ns(storage_io):
@pytest.fixture
def storage_with_numbers_namespace(storage_io):
storage_in = tm.Storage()
data = dict(One="1", two="2")
storage_in.set_namespace(("numbers",), data)
storage_in.save(storage_io())
storage_in.save(storage_io)
return storage_in


def test_define_ns(storage_with_numbers_namespace, storage_io):
storage_out = tm.Storage()
storage_out.request_namespace(("numbers",))
storage_out.load(storage_io())
storage_out.load(storage_io)
numbers = storage_out.get_namespace(("numbers",))
assert len(numbers) == 2
assert numbers["One"] == "1"
assert numbers["two"] == "2"


def test_set_ns(storage_io):
storage_in = tm.Storage()
data = dict(one="1", two="3")
storage_in.set_namespace(("numbers",), data)
storage_in.save(storage_io())
def test_set_ns(storage_with_numbers_namespace, storage_io):
data = dict(two="2")
storage_in.set_namespace(("numbers",), data)
storage_in.save(storage_io())
storage_with_numbers_namespace.set_namespace(("numbers",), data)
storage_with_numbers_namespace.save(storage_io)

storage_out = tm.Storage()
storage_out.request_namespace(("numbers",))
storage_out.load(storage_io())
storage_out.load(storage_io)
numbers = storage_out.get_namespace(("numbers",))
assert len(numbers) == 1
assert numbers["two"] == "2"

0 comments on commit dd66d94

Please sign in to comment.