shep/tests/test_item.py

87 lines
2.4 KiB
Python
Raw Normal View History

2022-01-31 12:23:51 +01:00
# standard imports
import unittest
# local imports
from shep import State
from shep.error import (
StateExists,
StateItemExists,
StateInvalid,
StateItemNotFound,
)
class TestStateItems(unittest.TestCase):
def setUp(self):
self.states = State(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_put(self):
item = b'foo'
# put in initial (no) state
self.states.put(item)
with self.assertRaises(StateItemExists):
self.states.put(item)
with self.assertRaises(StateItemExists):
self.states.put(item, self.states.BAZ)
def test_item_state(self):
item = b'foo'
2022-02-01 08:01:57 +01:00
self.states.put(item, state=self.states.XYZZY)
2022-01-31 12:23:51 +01:00
self.assertEqual(self.states.state(item), self.states.XYZZY)
def test_item_move(self):
item = b'foo'
2022-02-01 08:01:57 +01:00
self.states.put(item, state=self.states.FOO)
2022-01-31 12:23:51 +01:00
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'
2022-02-01 08:01:57 +01:00
self.states.put(item, state=self.states.FOO)
2022-01-31 12:23:51 +01:00
self.states.move(item, self.states.XYZZY)
self.assertEqual(self.states.state(item), self.states.XYZZY)
self.states.move(item, self.states.BAR)
self.assertEqual(self.states.state(item), self.states.BAR)
def test_item_move_from_new(self):
item = b'foo'
self.states.put(item)
self.assertEqual(self.states.state(item), self.states.NEW)
self.states.move(item, self.states.XYZZY)
self.assertEqual(self.states.state(item), self.states.XYZZY)
def test_item_purge(self):
item = b'foo'
2022-02-01 08:01:57 +01:00
self.states.put(item, state=self.states.BAZ)
2022-01-31 12:23:51 +01:00
self.assertEqual(self.states.state(item), self.states.BAZ)
self.states.purge(item)
with self.assertRaises(StateItemNotFound):
self.states.state(item)
2022-02-01 08:01:57 +01:00
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')
2022-01-31 12:23:51 +01:00
if __name__ == '__main__':
unittest.main()