コード例 #1
0
ファイル: tests.py プロジェクト: EdwinvO/django_keyval
 def setUp(self):
     self.kv1 = KeyVal('kv1')
     self.kv2 = KeyVal.connect('kv2')
     self.values1 = (12, 'ab', 1.2, (1,2), [1,2], {'a': 1.2})
     self.values2 = (123, 'abc', 12.3, (1,2,3), [1,2,3],
             {'a': 1, 'b': 2.3})
     self.keys = ['key{}'.format(i) for i, unused in enumerate(
             self.values1)]
     self.nokeys = ['nokey{}'.format(i) for i, unused in enumerate(
             self.values1)]
コード例 #2
0
ファイル: keyval.py プロジェクト: EdwinvO/django_keyval
 def handle(self, **options):
     command = options.get('command')
     if not command:
         raise SystemExit('Error: you must specify a command')
     store_name = options.get('store_name')
     if not store_name:
         raise SystemExit('Error: you must specify a store name')
     key = options.get('key')
     if not key:
         raise SystemExit('Error: you must specify a key')
     kv = KeyVal(store_name)
     if command == 'set':
         value = options.get('value')
         if not value:
             raise SystemExit('Error: you must specify a value')
         created = kv.set(key, value)
         if created:
             print('New key {} set to {}.'.format(key, value))
         else:
             print('Existing key {} overwritten to {}.'.format(key, value))
     elif command == 'get':
         try:
             value = kv.get_default(key, None)
         except NameError:
             raise SystemExit('Error: store_name {} does not exist'.format(
                     store_name))
         if value is None:
             print('Warning: Key {} does not exist in {}.'.format(key, 
                     store_name))
         else:
             print('Value for key {} is {}'.format(key, value))
     elif command == 'del':
         try:
             deleted = kv.delete(key, ignore_keyerror=True)
         except NameError:
             raise SystemExit('Error: store_name {} does not exist'.format(
                     store_name))
         if deleted:
             print('Key {} deleted'.format(key))
         else:
             print('Warning: Key {} does not exist in {}.'.format(key, 
                     store_name))
     else:
         raise SystemExit('Error: command {} is invalid'.format(command))
コード例 #3
0
ファイル: tests.py プロジェクト: EdwinvO/django_keyval
    def test_disconnect(self):
        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertTrue(self.kv1.set(key, value))
        # generate new kv's in kv2
        for key, value in zip(self.keys, self.values2):
            self.assertTrue(self.kv2.set(key, value))

        self.assertIsNone(self.kv1.disconnect())

        # any operation on kv1 should raise an error, but kv2 should be ok
        for key, value in zip(self.keys, self.values2):
            #exists
            self.assertRaises(NameError, self.kv1.exists, key)
            self.assertTrue(self.kv2.exists(key))
            #get
            self.assertRaises(NameError, self.kv1.get, key)
            self.assertEqual(self.kv2.get(key), value)
            #set
            self.assertRaises(NameError, self.kv1.set, key, value)
            self.assertFalse(self.kv2.set(key, value))
            #pop
            self.assertRaises(NameError, self.kv1.pop, key)
            self.assertEqual(self.kv2.pop(key), value)
            # further tests on kv2 are pointless
            #delete
            self.assertRaises(NameError, self.kv1.delete, key)
            #pop_default
            self.assertRaises(NameError, self.kv1.pop_default, key, 0)
            #get_default
            self.assertRaises(NameError, self.kv1.get_default, key, 0)
            #flush
            self.assertRaises(NameError, self.kv1.flush)

        # check if values in kv1 are still there
        new_kv1 = KeyVal('kv1')
        for key, value in zip(self.keys, self.values1):
            self.assertEqual(new_kv1.get(key), value)

        #flush_all should work!!
        self.assertIsNone(self.kv1.flush_all())
コード例 #4
0
ファイル: tests.py プロジェクト: EdwinvO/django_keyval
class KeyValTestCase(test.TestCase):
    '''
    #TODO: put values in database fixture instead or relying on kv1.set

    Until a database fixture is used, KeyVal.set is used in all tests.
    KeyVal.exists is not explicitly tested, because it is used in most tests
    anyway.
    '''


    def setUp(self):
        self.kv1 = KeyVal('kv1')
        self.kv2 = KeyVal.connect('kv2')
        self.values1 = (12, 'ab', 1.2, (1,2), [1,2], {'a': 1.2})
        self.values2 = (123, 'abc', 12.3, (1,2,3), [1,2,3],
                {'a': 1, 'b': 2.3})
        self.keys = ['key{}'.format(i) for i, unused in enumerate(
                self.values1)]
        self.nokeys = ['nokey{}'.format(i) for i, unused in enumerate(
                self.values1)]


    def test_set_get(self):
        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values2):
            self.assertTrue(self.kv1.set(key, value))
        # overwrite kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertFalse(self.kv1.set(key, value))

        # get (nonexistent) values in kv1
        for key in self.nokeys:
            self.assertRaises(KeyError, self.kv1.get, key)
        # get (nonexistent) values in *empty* kv2
        for key in self.keys:
            self.assertRaises(KeyError, self.kv2.get, key)

        # generate new kv's in kv2
        for key, value in zip(self.keys, self.values2):
            self.assertTrue(self.kv2.set(key, value))

        # check values in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertEqual(self.kv1.get(key), value)
        # check values in kv2
        for key, value in zip(self.keys, self.values2):
            self.assertEqual(self.kv2.get(key), value)


    def test_delete(self):
        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertTrue(self.kv1.set(key, value))

        # delete (nonexistent) kvs
        for key in self.nokeys:
            self.assertRaises(KeyError, self.kv1.delete, key)
            self.assertFalse(self.kv1.delete(key, ignore_keyerror=True))

        # delete kv1 keys:
        for key in self.keys:
            self.assertTrue(self.kv1.delete(key))

        # check if they are gone:
        for key in self.keys:
            self.assertFalse(self.kv1.exists(key))

        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertTrue(self.kv1.set(key, value))

        # delete kv1 keys:
        for key in self.keys:
            self.assertTrue(self.kv1.delete(key, ignore_keyerror=True))

        # check if they are gone:
        for key in self.keys:
            self.assertFalse(self.kv1.exists(key))


    def test_pop(self):
        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertTrue(self.kv1.set(key, value))

        # pop (nonexistent) kvs
        for key in self.nokeys:
            self.assertRaises(KeyError, self.kv1.pop, key)

        # pop kv1 keys:
        for key, value in zip(self.keys, self.values1):
            self.assertEqual(self.kv1.pop(key), value)

        # check if they are gone:
        for key in self.keys:
            self.assertFalse(self.kv1.exists(key))


    def test_pop_default(self):
        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertTrue(self.kv1.set(key, value))

        # pop (nonexistent) kvs
        for key in self.nokeys:
            self.assertEqual(self.kv1.pop_default(key, 5), 5)

        # pop kv1 keys:
        for key, value in zip(self.keys, self.values1):
            self.assertEqual(self.kv1.pop_default(key, 5), value)

        # check if they are gone:
        for key in self.keys:
            self.assertFalse(self.kv1.exists(key))


    def test_get_default(self):
        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertTrue(self.kv1.set(key, value))

        # get (nonexistent) kvs
        for key in self.nokeys:
            self.assertEqual(self.kv1.get_default(key, 5), 5)

        # pop kv1 keys:
        for key, value in zip(self.keys, self.values1):
            self.assertEqual(self.kv1.get_default(key, 5), value)


    def test_flush(self):
        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertTrue(self.kv1.set(key, value))
        # generate new kv's in kv2
        for key, value in zip(self.keys, self.values2):
            self.assertTrue(self.kv2.set(key, value))

        # flush kv1:
        self.assertIsNone(self.kv1.flush())

        # kv1 should be empty
        for key in self.keys:
            self.assertFalse(self.kv1.exists(key))

        # kv2 should be full
        for key in self.keys:
            self.assertTrue(self.kv2.exists(key))


    def test_flush_all(self):
        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertTrue(self.kv1.set(key, value))
        # generate new kv's in kv2
        for key, value in zip(self.keys, self.values2):
            self.assertTrue(self.kv2.set(key, value))

        # the following is equivalent to KeyVal.flush_all()
        self.assertIsNone(self.kv1.flush_all())

        # kv1 should be empty
        for key in self.keys:
            self.assertFalse(self.kv1.exists(key))

        # kv2 should be empty
        for key in self.keys:
            self.assertFalse(self.kv2.exists(key))

    def test_disconnect(self):
        # generate new kv's in kv1
        for key, value in zip(self.keys, self.values1):
            self.assertTrue(self.kv1.set(key, value))
        # generate new kv's in kv2
        for key, value in zip(self.keys, self.values2):
            self.assertTrue(self.kv2.set(key, value))

        self.assertIsNone(self.kv1.disconnect())

        # any operation on kv1 should raise an error, but kv2 should be ok
        for key, value in zip(self.keys, self.values2):
            #exists
            self.assertRaises(NameError, self.kv1.exists, key)
            self.assertTrue(self.kv2.exists(key))
            #get
            self.assertRaises(NameError, self.kv1.get, key)
            self.assertEqual(self.kv2.get(key), value)
            #set
            self.assertRaises(NameError, self.kv1.set, key, value)
            self.assertFalse(self.kv2.set(key, value))
            #pop
            self.assertRaises(NameError, self.kv1.pop, key)
            self.assertEqual(self.kv2.pop(key), value)
            # further tests on kv2 are pointless
            #delete
            self.assertRaises(NameError, self.kv1.delete, key)
            #pop_default
            self.assertRaises(NameError, self.kv1.pop_default, key, 0)
            #get_default
            self.assertRaises(NameError, self.kv1.get_default, key, 0)
            #flush
            self.assertRaises(NameError, self.kv1.flush)

        # check if values in kv1 are still there
        new_kv1 = KeyVal('kv1')
        for key, value in zip(self.keys, self.values1):
            self.assertEqual(new_kv1.get(key), value)

        #flush_all should work!!
        self.assertIsNone(self.kv1.flush_all())