shep

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

test_state.py (10369B)


      1 # standard imports
      2 import unittest
      3 import logging
      4 
      5 # local imports
      6 from shep import State
      7 from shep.error import (
      8         StateExists,
      9         StateInvalid,
     10         StateItemNotFound,
     11         )
     12 
     13 logging.basicConfig(level=logging.DEBUG)
     14 logg = logging.getLogger()
     15 
     16 
     17 class MockCallback:
     18 
     19     def __init__(self):
     20         self.items = {}
     21         self.items_from = {}
     22 
     23 
     24     def add(self, k, v_from, v_to):
     25         if self.items.get(k) == None:
     26             self.items[k] = []
     27             self.items_from[k] = []
     28         self.items[k].append(v_to)
     29         self.items_from[k].append(v_from)
     30 
     31 
     32 class TestState(unittest.TestCase):
     33 
     34     def test_key_check(self):
     35         states = State(3)
     36         states.add('foo')
     37 
     38         for k in [
     39                 'f0o',
     40                 'f oo',
     41             ]:
     42             with self.assertRaises(ValueError):
     43                 states.add(k)
     44 
     45 
     46     def test_get(self):
     47         states = State(3)
     48         states.add('foo')
     49         states.add('bar')
     50         states.add('baz')
     51         self.assertEqual(states.BAZ, 4)
     52 
     53 
     54     def test_limit(self):
     55         states = State(3)
     56         states.add('foo')
     57         states.add('bar')
     58         states.add('baz')
     59         with self.assertRaises(OverflowError):
     60             states.add('gaz')
     61 
     62 
     63     def test_dup(self):
     64         states = State(2)
     65         states.add('foo')
     66         with self.assertRaises(StateExists):
     67             states.add('foo')
     68 
     69 
     70     def test_alias(self):
     71         states = State(2)
     72         states.add('foo')
     73         states.add('bar')
     74         states.alias('baz', states.FOO | states.BAR)
     75         self.assertEqual(states.BAZ, 3)
     76 
     77 
     78     def test_alias_limit(self):
     79         states = State(2)
     80         states.add('foo')
     81         states.add('bar')
     82         states.alias('baz', states.FOO | states.BAR)
     83 
     84 
     85     def test_alias_nopure(self):
     86         states = State(3)
     87         with self.assertRaises(ValueError):
     88             states.alias('foo', 1)
     89         states.add('foo')
     90         states.add('bar')
     91         states.alias('baz', states.FOO, states.BAR)
     92         self.assertEqual(states.BAZ, 3)
     93 
     94 
     95     def test_alias_multi(self):
     96         states = State(3)
     97 
     98 
     99     def test_alias_cover(self):
    100         states = State(3)
    101         states.add('foo')
    102         states.add('bar')
    103         with self.assertRaises(StateInvalid):
    104             states.alias('baz', 5)
    105 
    106 
    107     def test_alias_invalid(self):
    108         states = State(3)
    109         states.add('foo')
    110         states.add('bar')
    111         states.put('abcd')
    112         states.set('abcd', states.FOO)
    113         with self.assertRaises(StateInvalid):
    114             states.set('abcd', states.BAR)
    115 
    116 
    117     def test_alias_invalid_ignore(self):
    118         states = State(3, check_alias=False)
    119         states.add('foo')
    120         states.add('bar')
    121         states.add('baz')
    122         states.put('abcd')
    123         states.set('abcd', states.FOO)
    124         states.set('abcd', states.BAZ)
    125         v = states.state('abcd')
    126         s = states.name(v)
    127         self.assertEqual(s, '_FOO__BAZ')
    128 
    129 
    130     def test_peek(self):
    131         states = State(2)
    132         states.add('foo')
    133         states.add('bar')
    134 
    135         states.put('abcd')
    136         self.assertEqual(states.peek('abcd'), states.FOO)
    137 
    138         states.move('abcd', states.FOO)
    139         self.assertEqual(states.peek('abcd'), states.BAR)
    140 
    141         states.move('abcd', states.BAR)
    142 
    143         with self.assertRaises(StateInvalid):
    144             states.peek('abcd')
    145 
    146 
    147     def test_from_name(self):
    148         states = State(3)
    149         states.add('foo')
    150         self.assertEqual(states.from_name('foo'), states.FOO)
    151 
    152 
    153     def test_change(self):
    154         states = State(3)
    155         states.add('foo')
    156         states.add('bar')
    157         states.add('baz')
    158         states.alias('inky', states.FOO | states.BAR)
    159         states.alias('pinky', states.FOO | states.BAZ)
    160         states.put('abcd')
    161         states.next('abcd')
    162         states.set('abcd', states.BAR)
    163         states.change('abcd', states.BAZ, states.BAR)
    164         self.assertEqual(states.state('abcd'), states.PINKY)
    165 
    166 
    167     def test_change_onezero(self):
    168         states = State(3)
    169         states.add('foo')
    170         states.add('bar')
    171         states.add('baz')
    172         states.alias('inky', states.FOO | states.BAR)
    173         states.alias('pinky', states.FOO | states.BAZ)
    174         states.put('abcd')
    175         states.next('abcd')
    176         states.change('abcd', states.BAR, 0)
    177         self.assertEqual(states.state('abcd'), states.INKY)
    178         states.change('abcd', 0, states.BAR)
    179         self.assertEqual(states.state('abcd'), states.FOO)
    180 
    181 
    182     def test_change_dates(self):
    183         states = State(3)
    184         states.add('foo')
    185         states.put('abcd')
    186         states.put('bcde')
    187 
    188         a = states.modified('abcd')
    189         b = states.modified('bcde')
    190         self.assertGreater(b, a)
    191 
    192         states.set('abcd', states.FOO)
    193         a = states.modified('abcd')
    194         b = states.modified('bcde')
    195         self.assertGreater(a, b)
    196 
    197 
    198     def test_event_callback(self):
    199         cb = MockCallback()
    200         states = State(3, event_callback=cb.add)
    201         states.add('foo')
    202         states.add('bar')
    203         states.add('baz')
    204         states.alias('xyzzy', states.FOO | states.BAR)
    205         states.put('abcd')
    206         states.set('abcd', states.FOO)
    207         states.set('abcd', states.BAR)
    208         states.change('abcd', states.BAZ, states.XYZZY)
    209         events = cb.items['abcd']
    210         self.assertEqual(len(events), 4)
    211         self.assertEqual(states.from_name(events[0]), states.NEW)
    212         self.assertEqual(states.from_name(events[1]), states.FOO)
    213         self.assertEqual(states.from_name(events[2]), states.XYZZY)
    214         self.assertEqual(states.from_name(events[3]), states.BAZ)
    215 
    216 
    217     def test_dynamic(self):
    218         states = State(0)
    219         states.add('foo')
    220         states.add('bar')
    221         states.alias('baz', states.FOO | states.BAR)
    222 
    223 
    224     def test_mask(self):
    225         states = State(3)
    226         states.add('foo')
    227         states.add('bar')
    228         states.add('baz')
    229         states.alias('all', states.FOO | states.BAR | states.BAZ)
    230         mask = states.mask('xyzzy', states.FOO | states.BAZ)
    231         self.assertEqual(mask, states.BAR)
    232 
    233 
    234     def test_mask_dynamic(self):
    235         states = State(0)
    236         states.add('foo')
    237         states.add('bar')
    238         states.add('baz')
    239         states.alias('all', states.FOO | states.BAR | states.BAZ)
    240         mask = states.mask('xyzzy', states.FOO | states.BAZ)
    241         self.assertEqual(mask, states.BAR)
    242 
    243 
    244     def test_mask_zero(self):
    245         states = State(0)
    246         states.add('foo')
    247         states.add('bar')
    248         states.add('baz')
    249         states.alias('all', states.FOO | states.BAR | states.BAZ)
    250         mask = states.mask('xyzzy')
    251         self.assertEqual(mask, states.ALL)
    252 
    253 
    254     def test_remove(self):
    255         states = State(1)
    256         states.add('foo')
    257 
    258         states.put('xyzzy', contents='plugh')
    259         v = states.get('xyzzy')
    260         self.assertEqual(v, 'plugh')
    261 
    262         states.next('xyzzy')
    263 
    264         v = states.state('xyzzy')
    265         self.assertEqual(states.FOO, v)
    266 
    267         states.purge('xyzzy')
    268        
    269         with self.assertRaises(StateItemNotFound):
    270             states.state('xyzzy')
    271 
    272 
    273     def test_elements(self):
    274         states = State(2)
    275         states.add('foo')
    276         states.add('bar')
    277         states.alias('baz', states.FOO, states.BAR)
    278 
    279         v = states.elements(states.BAZ)
    280         self.assertIn('FOO', v)
    281         self.assertIn('BAR', v)
    282         self.assertIsInstance(v, str)
    283 
    284         v = states.elements(states.BAZ, numeric=True)
    285         self.assertIn(states.FOO, v)
    286         self.assertIn(states.BAR, v)
    287 
    288         v = states.elements(states.BAZ, as_string=False)
    289         self.assertIn('FOO', v)
    290         self.assertIn('BAR', v)
    291         self.assertNotIsInstance(v, str)
    292         self.assertIsInstance(v, list)
    293 
    294 
    295     def test_count(self):
    296         states = State(3)
    297         states.add('foo')
    298         states.add('bar')
    299         self.assertEqual(states.count(), 2)
    300         states.add('baz')
    301         self.assertEqual(states.count(), 3)
    302 
    303 
    304     def test_pure(self):
    305         states = State(2)
    306         states.add('foo')
    307         states.add('bar')
    308         states.alias('baz', states.FOO, states.BAR)
    309 
    310         v = states.is_pure(states.BAZ)
    311         self.assertFalse(v)
    312 
    313         v = states.is_pure(states.FOO)
    314         self.assertTrue(v)
    315 
    316 
    317     def test_default(self):
    318         states = State(2, default_state='FOO')
    319         with self.assertRaises(StateItemNotFound):
    320             states.state('NEW')
    321         r = getattr(states, 'FOO')
    322         self.assertEqual(r, 0)
    323         states.state('FOO')
    324         states.put('bar')
    325         r = states.list(states.FOO)
    326         self.assertEqual(len(r), 1)
    327 
    328 
    329     def test_unset(self):
    330         states = State(2)
    331         states.add('one')
    332         states.add('two')
    333         states.alias('three', states.ONE, states.TWO)
    334         states.put('foo', state=states.ONE)
    335         states.set('foo', states.TWO)
    336         r = states.list(states.ONE)
    337         self.assertEqual(len(r), 1)
    338         r = states.list(states.TWO)
    339         self.assertEqual(len(r), 1)
    340         r = states.unset('foo', states.ONE)
    341         r = states.list(states.ONE)
    342         self.assertEqual(len(r), 0)
    343         r = states.list(states.TWO)
    344         self.assertEqual(len(r), 1)
    345 
    346 
    347     def test_move(self):
    348         states = State(1)
    349         states.add('one')
    350         states.put('foo')
    351         r = states.list(states.NEW)
    352         self.assertEqual(len(r), 1)
    353         states.move('foo', states.ONE)
    354         r = states.list(states.NEW)
    355         self.assertEqual(len(r), 0)
    356 
    357 
    358 
    359     def test_generate_missing(self):
    360         states = State(3)
    361         with self.assertRaises(StateInvalid):
    362             states.from_elements("_FOO__BAR")
    363         states.from_elements("_FOO__BAR", create_missing=True)
    364 
    365 
    366     def test_set_same(self):
    367         states = State(4, check_alias=False)
    368         states.add('one')
    369         states.add('two')
    370         states.add('three')
    371         states.put('foo')
    372         states.next('foo')
    373         self.assertEqual(states.state('foo'), states.ONE)
    374         states.set('foo', states.TWO)
    375         self.assertEqual(states.state('foo'), states.ONE | states.TWO)
    376         self.assertEqual(states.state('foo'), states._ONE__TWO)
    377 
    378         states.alias('onetwo', states.ONE, states.TWO)
    379         states.set('foo', states.TWO)
    380         self.assertEqual(states.state('foo'), states.ONETWO)
    381         self.assertEqual(states.state('foo'), states._ONE__TWO)
    382 
    383 
    384 if __name__ == '__main__':
    385     unittest.main()