def test_hook_scope(self): kv = Storage(':memory:') try: with kv.hook_scope('install') as rev: self.assertEqual(rev, 1) kv.set('a', 1) raise RuntimeError('x') except RuntimeError: self.assertEqual(kv.get('a'), None) with kv.hook_scope('config-changed') as rev: self.assertEqual(rev, 1) kv.set('a', 1) self.assertEqual(kv.get('a'), 1) kv.revision = None with kv.hook_scope('start') as rev: self.assertEqual(rev, 2) kv.set('a', False) kv.set('a', True) self.assertEqual(kv.get('a'), True) # History doesn't decode values by default history = [h[:-1] for h in kv.gethistory('a')] self.assertEqual( history, [(1, 'a', '1', 'config-changed'), (2, 'a', 'true', 'start')]) history = [h[:-1] for h in kv.gethistory('a', deserialize=True)] self.assertEqual( history, [(1, 'a', 1, 'config-changed'), (2, 'a', True, 'start')])
def test_unset(self): kv = Storage(':memory:') with kv.hook_scope('install'): kv.set('a', True) with kv.hook_scope('start'): kv.set('a', False) with kv.hook_scope('config-changed'): kv.unset('a') history = [h[:-1] for h in kv.gethistory('a')] self.assertEqual(history, [(1, 'a', 'true', 'install'), (2, 'a', 'false', 'start'), (3, 'a', '"DELETED"', "config-changed")])
def test_delta_no_previous_and_history(self): kv = Storage(':memory:') with kv.hook_scope('install'): data = {'a': 0, 'c': False} delta = kv.delta(data, 'settings.') self.assertEqual(delta, {'a': (None, False), 'c': (None, False)}) kv.update(data, 'settings.') with kv.hook_scope('config'): data = {'a': 1, 'c': True} delta = kv.delta(data, 'settings.') self.assertEqual(delta, {'a': (0, 1), 'c': (False, True)}) kv.update(data, 'settings.') # strip the time history = [h[:-1] for h in kv.gethistory('settings.a')] self.assertEqual(history, [(1, 'settings.a', '0', 'install'), (2, 'settings.a', '1', 'config')])