Add contents to keys
This commit is contained in:
parent
aff0da4d84
commit
86a7620373
@ -15,34 +15,42 @@ class PersistedState(State):
|
||||
self.__stores[k] = self.__store_factory(k)
|
||||
|
||||
|
||||
def put(self, item, state=None):
|
||||
def put(self, key, contents=None, state=None):
|
||||
k = self.name(state)
|
||||
self.__ensure_store(k)
|
||||
self.__stores[k].add(item)
|
||||
self.__stores[k].add(key, contents)
|
||||
|
||||
super(PersistedState, self).put(item, state=state)
|
||||
super(PersistedState, self).put(key, state=state, contents=contents)
|
||||
|
||||
|
||||
def move(self, item, to_state):
|
||||
def move(self, key, to_state):
|
||||
k_to = self.name(to_state)
|
||||
|
||||
from_state = self.state(item)
|
||||
from_state = self.state(key)
|
||||
k_from = self.name(from_state)
|
||||
|
||||
self.__ensure_store(k_to)
|
||||
self.__ensure_store(k_from)
|
||||
|
||||
self.__stores[k_to].add(item)
|
||||
self.__stores[k_from].remove(item)
|
||||
contents = self.__stores[k_from].get(key)
|
||||
self.__stores[k_to].add(key, contents)
|
||||
self.__stores[k_from].remove(key)
|
||||
|
||||
super(PersistedState, self).move(item, to_state)
|
||||
super(PersistedState, self).move(key, to_state)
|
||||
|
||||
|
||||
def purge(self, item):
|
||||
state = self.state(item)
|
||||
def purge(self, key):
|
||||
state = self.state(key)
|
||||
k = self.name(state)
|
||||
|
||||
self.__ensure_store(k)
|
||||
|
||||
self.__stores[k].remove(item)
|
||||
super(PersistedState, self).purge(item)
|
||||
self.__stores[k].remove(key)
|
||||
super(PersistedState, self).purge(key)
|
||||
|
||||
|
||||
def get(self, key):
|
||||
state = self.state(key)
|
||||
k = self.name(state)
|
||||
|
||||
self.__stores[k].get(k)
|
||||
|
@ -204,3 +204,7 @@ class State:
|
||||
if state == None:
|
||||
raise StateItemNotFound(key)
|
||||
return state
|
||||
|
||||
|
||||
def get(self, key):
|
||||
return self.__contents[key]
|
||||
|
@ -9,7 +9,9 @@ class SimpleFileStore:
|
||||
os.makedirs(self.path, exist_ok=True)
|
||||
|
||||
|
||||
def add(self, v, contents=''):
|
||||
def add(self, v, contents):
|
||||
if contents == None:
|
||||
contents = ''
|
||||
fp = os.path.join(self.path, v)
|
||||
try:
|
||||
os.stat(fp)
|
||||
|
@ -24,19 +24,19 @@ class TestStateReport(unittest.TestCase):
|
||||
|
||||
|
||||
def test_add(self):
|
||||
self.states.put('abcd', self.states.FOO)
|
||||
self.states.put('abcd', state=self.states.FOO, contents='baz')
|
||||
fp = os.path.join(self.d, 'FOO', 'abcd')
|
||||
f = open(fp, 'r')
|
||||
v = f.read()
|
||||
f.close()
|
||||
self.assertEqual(len(v), 0)
|
||||
self.assertEqual(v, 'baz')
|
||||
|
||||
|
||||
def test_dup(self):
|
||||
self.states.put('abcd', self.states.FOO)
|
||||
self.states.put('abcd', state=self.states.FOO)
|
||||
with self.assertRaises(FileExistsError):
|
||||
self.states.put('abcd', self.states.FOO)
|
||||
|
||||
self.states.put('abcd', state=self.states.FOO)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
@ -37,20 +37,20 @@ class TestStateItems(unittest.TestCase):
|
||||
|
||||
def test_item_state(self):
|
||||
item = b'foo'
|
||||
self.states.put(item, self.states.XYZZY)
|
||||
self.states.put(item, state=self.states.XYZZY)
|
||||
self.assertEqual(self.states.state(item), self.states.XYZZY)
|
||||
|
||||
|
||||
def test_item_move(self):
|
||||
item = b'foo'
|
||||
self.states.put(item, self.states.FOO)
|
||||
self.states.put(item, state=self.states.FOO)
|
||||
self.states.move(item, self.states.BAR)
|
||||
self.assertEqual(self.states.state(item), self.states.BAR)
|
||||
|
||||
|
||||
def test_item_move_from_alias(self):
|
||||
item = b'foo'
|
||||
self.states.put(item, self.states.FOO)
|
||||
self.states.put(item, state=self.states.FOO)
|
||||
self.states.move(item, self.states.XYZZY)
|
||||
self.assertEqual(self.states.state(item), self.states.XYZZY)
|
||||
self.states.move(item, self.states.BAR)
|
||||
@ -67,12 +67,20 @@ class TestStateItems(unittest.TestCase):
|
||||
|
||||
def test_item_purge(self):
|
||||
item = b'foo'
|
||||
self.states.put(item, self.states.BAZ)
|
||||
self.states.put(item, state=self.states.BAZ)
|
||||
self.assertEqual(self.states.state(item), self.states.BAZ)
|
||||
self.states.purge(item)
|
||||
with self.assertRaises(StateItemNotFound):
|
||||
self.states.state(item)
|
||||
|
||||
|
||||
def test_item_get(self):
|
||||
item = b'foo'
|
||||
self.states.put(item, self.states.BAZ, contents='bar')
|
||||
self.assertEqual(self.states.state(item), self.states.BAZ)
|
||||
v = self.states.get(item)
|
||||
self.assertEqual(v, 'bar')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
@ -1,5 +1,6 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
import logging
|
||||
|
||||
# local imports
|
||||
from shep.persist import PersistedState
|
||||
@ -10,6 +11,9 @@ from shep.error import (
|
||||
StateItemNotFound,
|
||||
)
|
||||
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class MockStore:
|
||||
|
||||
def __init__(self):
|
||||
@ -17,14 +21,18 @@ class MockStore:
|
||||
self.for_state = 0
|
||||
|
||||
|
||||
def add(self, k):
|
||||
self.v[k] = True
|
||||
def add(self, k, contents):
|
||||
self.v[k] = contents
|
||||
|
||||
|
||||
def remove(self, k):
|
||||
del self.v[k]
|
||||
|
||||
|
||||
def get(self, k):
|
||||
return self.v[k]
|
||||
|
||||
|
||||
class TestStateItems(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@ -44,7 +52,7 @@ class TestStateItems(unittest.TestCase):
|
||||
|
||||
def test_persist_new(self):
|
||||
item = b'foo'
|
||||
self.states.put(item)
|
||||
self.states.put(item, True)
|
||||
self.assertTrue(self.mockstore.v.get(item))
|
||||
|
||||
|
||||
@ -57,7 +65,7 @@ class TestStateItems(unittest.TestCase):
|
||||
|
||||
def test_persist_move(self):
|
||||
item = b'foo'
|
||||
self.states.put(item, self.states.FOO)
|
||||
self.states.put(item, self.states.FOO, True)
|
||||
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
|
||||
@ -66,7 +74,7 @@ class TestStateItems(unittest.TestCase):
|
||||
|
||||
def test_persist_purge(self):
|
||||
item = b'foo'
|
||||
self.states.put(item, self.states.FOO)
|
||||
self.states.put(item, self.states.FOO, True)
|
||||
self.states.purge(item)
|
||||
self.assertEqual(self.mockstore.for_state, self.states.name(self.states.FOO))
|
||||
self.assertIsNone(self.mockstore.v.get(item))
|
||||
|
Loading…
Reference in New Issue
Block a user