Ejemplo n.º 1
0
    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')])
Ejemplo n.º 2
0
 def test_get_set_unset(self):
     kv = Storage(':memory:')
     kv.hook_scope('test')
     kv.set('hello', 'saucy')
     kv.set('hello', 'world')
     self.assertEqual(kv.get('hello'), 'world')
     kv.flush()
     kv.unset('hello')
     self.assertEqual(kv.get('hello'), None)
     kv.flush(False)
     self.assertEqual(kv.get('hello'), 'world')
Ejemplo n.º 3
0
    def test_init_kv_multiple(self):
        with tempfile.NamedTemporaryFile() as fh:
            kv = Storage(fh.name)
            with kv.hook_scope('xyz'):
                kv.set('x', 1)
            kv.close()

            kv = Storage(fh.name)
            with kv.hook_scope('abc'):
                self.assertEqual(kv.get('x'), 1)
            kv.close()
Ejemplo n.º 4
0
 def test_record(self):
     kv = Storage(':memory:')
     kv.set('config', {'x': 1, 'b': False})
     config = kv.get('config', record=True)
     self.assertEqual(config.b, False)
     self.assertEqual(config.x, 1)
     self.assertEqual(kv.set('config.x', 1), 1)
     try:
         config.z
     except AttributeError:
         pass
     else:
         self.fail('attribute error should fire on nonexistant')