shep

Multi-state key stores using bit masks for python3
git clone git://git.defalsify.org/shep.git
Info | Log | Files | Refs | LICENSE

test_item.py (3307B)


      1 # standard imports
      2 import unittest
      3 
      4 # local imports
      5 from shep import State
      6 from shep.error import (
      7         StateExists,
      8         StateItemExists,
      9         StateInvalid,
     10         StateItemNotFound,
     11         )
     12 
     13 
     14 class TestStateItems(unittest.TestCase):
     15         
     16     def setUp(self):
     17         self.states = State(4)
     18         self.states.add('foo') 
     19         self.states.add('bar') 
     20         self.states.add('baz') 
     21         self.states.alias('xyzzy', self.states.BAZ | self.states.BAR) 
     22         self.states.alias('plugh', self.states.FOO | self.states.BAR) 
     23 
     24 
     25     def test_put(self):
     26         item = b'foo'
     27 
     28         # put in initial (no) state
     29         self.states.put(item)
     30 
     31         with self.assertRaises(StateItemExists):
     32             self.states.put(item)
     33 
     34         with self.assertRaises(StateItemExists):
     35             self.states.put(item, self.states.BAZ)
     36 
     37 
     38     def test_item_state(self):
     39         item = b'foo'
     40         self.states.put(item, state=self.states.XYZZY)
     41         self.assertEqual(self.states.state(item), self.states.XYZZY)
     42 
     43 
     44     def test_item_move(self):
     45         item = b'foo'
     46         self.states.put(item, state=self.states.FOO)
     47         self.states.move(item, self.states.BAR)
     48         self.assertEqual(self.states.state(item), self.states.BAR)
     49 
     50 
     51     def test_item_move_from_alias(self):
     52         item = b'foo'
     53         self.states.put(item, state=self.states.FOO)
     54         self.states.move(item, self.states.XYZZY)
     55         self.assertEqual(self.states.state(item), self.states.XYZZY)
     56         self.states.move(item, self.states.BAR)
     57         self.assertEqual(self.states.state(item), self.states.BAR)
     58 
     59 
     60     def test_item_move_from_new(self):
     61         item = b'foo'
     62         self.states.put(item)
     63         self.assertEqual(self.states.state(item), self.states.NEW)
     64         self.states.move(item, self.states.XYZZY)
     65         self.assertEqual(self.states.state(item), self.states.XYZZY)
     66 
     67 
     68     def test_item_get(self):
     69         item = b'foo'
     70         self.states.put(item, state=self.states.BAZ, contents='bar')
     71         self.assertEqual(self.states.state(item), self.states.BAZ)
     72         v = self.states.get(item)
     73         self.assertEqual(v, 'bar')
     74 
     75 
     76     def test_item_set(self):
     77         item = b'foo'
     78         self.states.put(item, state=self.states.FOO)
     79         self.states.set(item, self.states.BAR)
     80         self.assertEqual(self.states.state(item), self.states.PLUGH)
     81 
     82 
     83     def test_item_set_invalid(self):
     84         item = b'foo'
     85         self.states.put(item, state=self.states.FOO)
     86         with self.assertRaises(StateInvalid):
     87             self.states.set(item, self.states.BAZ)
     88 
     89         item = b'bar'
     90         self.states.put(item, state=self.states.BAR)
     91         with self.assertRaises(ValueError):
     92             self.states.set(item, self.states.XYZZY)
     93 
     94 
     95     def test_item_set_invalid(self):
     96         item = b'foo'
     97         self.states.put(item, state=self.states.XYZZY)
     98         self.states.unset(item, self.states.BAZ)
     99         self.assertEqual(self.states.state(item), self.states.BAR)
    100 
    101         item = b'bar'
    102         self.states.put(item, self.states.XYZZY)
    103         with self.assertRaises(ValueError):
    104             self.states.unset(item, self.states.PLUGH)
    105 
    106         with self.assertRaises(ValueError):
    107             self.states.unset(item, self.states.FOO) # bit not set
    108 
    109 
    110 if __name__ == '__main__':
    111     unittest.main()