shep/tests/test_store.py

90 lines
2.2 KiB
Python
Raw Normal View History

2022-01-31 13:10:04 +01:00
# standard imports
import unittest
2022-02-01 08:01:57 +01:00
import logging
2022-01-31 13:10:04 +01:00
# local imports
from shep.persist import PersistedState
from shep.error import (
StateExists,
StateItemExists,
StateInvalid,
StateItemNotFound,
)
2022-02-01 08:01:57 +01:00
logg = logging.getLogger()
2022-01-31 13:10:04 +01:00
class MockStore:
def __init__(self):
self.v = {}
self.for_state = 0
def put(self, k, contents=None):
2022-02-01 08:01:57 +01:00
self.v[k] = contents
2022-01-31 13:10:04 +01:00
def remove(self, k):
del self.v[k]
2022-02-01 08:01:57 +01:00
def get(self, k):
return self.v[k]
2022-03-29 11:24:57 +02:00
def list(self):
return list(self.v.keys())
2022-01-31 13:10:04 +01:00
class TestStateItems(unittest.TestCase):
def setUp(self):
self.mockstore = MockStore()
def mockstore_factory(v):
self.mockstore.for_state = v
return self.mockstore
self.states = PersistedState(mockstore_factory, 4)
self.states.add('foo')
self.states.add('bar')
self.states.add('baz')
self.states.alias('xyzzy', self.states.BAZ | self.states.BAR)
self.states.alias('plugh', self.states.FOO | self.states.BAR)
def test_persist_new(self):
item = b'foo'
2022-02-01 08:01:57 +01:00
self.states.put(item, True)
2022-01-31 13:10:04 +01:00
self.assertTrue(self.mockstore.v.get(item))
def test_persist_move(self):
item = b'foo'
self.states.put(item, self.states.FOO)
self.states.move(item, self.states.XYZZY)
self.assertEqual(self.mockstore.for_state, self.states.name(self.states.XYZZY))
def test_persist_move(self):
item = b'foo'
2022-02-01 08:01:57 +01:00
self.states.put(item, self.states.FOO, True)
2022-01-31 13:10:04 +01:00
self.states.move(item, self.states.XYZZY)
self.assertEqual(self.mockstore.for_state, self.states.name(self.states.XYZZY))
# TODO: cant check the add because remove happens after remove, need better mock
self.assertIsNone(self.mockstore.v.get(item))
def test_persist_move_new(self):
item = b'foo'
self.states.put(item)
self.states.move(item, self.states.BAZ)
self.assertEqual(self.mockstore.for_state, self.states.name(self.states.BAZ))
self.assertIsNone(self.mockstore.v.get(item))
2022-03-17 20:16:33 +01:00
2022-01-31 13:10:04 +01:00
if __name__ == '__main__':
unittest.main()