示例#1
0
    def test_clear(self):
        table_before_clear = dict(a=1, b=2, c=3)

        s = MemoryKeyValueStore()
        s._table = table_before_clear
        s.clear()
        nose.tools.assert_equal(s._table, {})
    def test_remove_many_missing_key(self):
        """
        Test that we cannot remove keys not present in table and that table
        is not modified on error.
        """
        expected_table = {
            0: 0,
            1: 1,
            2: 2,
        }

        s = MemoryKeyValueStore()
        s._table = {
            0: 0,
            1: 1,
            2: 2,
        }

        self.assertRaisesRegex(KeyError, 'a', s.remove_many, ['a'])
        self.assertDictEqual(s._table, expected_table)

        # Even if one of the keys is value, the table should not be modified if
        # one of the keys is invalid.
        self.assertRaisesRegex(KeyError, '6', s.remove_many, [1, 6])
        self.assertDictEqual(s._table, expected_table)

        PY2_SET_KEY_ERROR_RE = r"set\(\[(?:7|8), (?:7|8)\]\)"
        PY3_SET_KEY_ERROR_RE = "{(?:7|8), (?:7|8)}"
        self.assertRaisesRegex(
            KeyError,
            # Should show a "set" that contains 7 and 8, regardless of order.
            '(?:{}|{})'.format(PY2_SET_KEY_ERROR_RE, PY3_SET_KEY_ERROR_RE),
            s.remove_many,
            [7, 8])
    def test_clear(self):
        """ Test normal clear functionality. """
        table_before_clear = dict(a=1, b=2, c=3)

        s = MemoryKeyValueStore()
        s._table = table_before_clear
        s.clear()
        self.assertEqual(s._table, {})
 def test_count(self):
     """
     Test that count returns appropriately based on table state.
     """
     s = MemoryKeyValueStore()
     assert s.count() == 0
     s._table = {0: 0, 1: 1, 'a': True, None: False}
     assert s.count() == 4
示例#5
0
    def test_clear(self):
        """ Test normal clear functionality. """
        table_before_clear = dict(a=1, b=2, c=3)

        s = MemoryKeyValueStore()
        s._table = table_before_clear
        s.clear()
        self.assertEqual(s._table, {})
 def test_remove_missing_key(self):
     """
     Test that we cannot remove a key not in the store.
     """
     s = MemoryKeyValueStore()
     s._table = {0: 1, 'a': 'b'}
     self.assertRaises(KeyError, s.remove, 'some-key')
     # table should remain unchanged.
     self.assertDictEqual(s._table, {0: 1, 'a': 'b'})
示例#7
0
    def test_clear_readonly(self):
        table_before_clear = dict(a=1, b=2, c=3)

        s = MemoryKeyValueStore()
        s._table = table_before_clear
        s.is_read_only = mock.MagicMock(return_value=True)

        nose.tools.assert_raises(ReadOnlyError, s.clear)
        nose.tools.assert_equal(s._table, table_before_clear)
    def test_remove(self):
        """ Test normal removal. """
        s = MemoryKeyValueStore()
        s._table = {
            0: 1,
            'a': 'b',
        }

        s.remove(0)
        self.assertDictEqual(s._table, {'a': 'b'})
    def test_clear_readonly(self):
        """ Test trying to clear on a read-only store. """
        table_before_clear = dict(a=1, b=2, c=3)

        s = MemoryKeyValueStore()
        s._table = table_before_clear
        s.is_read_only = mock.MagicMock(return_value=True)

        self.assertRaises(ReadOnlyError, s.clear)
        self.assertEqual(s._table, table_before_clear)
示例#10
0
 def test_keys_with_table(self):
     s = MemoryKeyValueStore()
     s._table = {
         'a': 'b',
         'c': 1,
         'asdfghsdfg': None,
         'r3adf3a#+': [4, 5, 6, '7'],
     }
     nose.tools.assert_set_equal(set(s.keys()),
                                 {'a', 'c', 'asdfghsdfg', 'r3adf3a#+'})
示例#11
0
    def test_remove(self):
        """ Test normal removal. """
        s = MemoryKeyValueStore()
        s._table = {
            0: 1,
            'a': 'b',
        }

        s.remove(0)
        self.assertDictEqual(s._table, {'a': 'b'})
示例#12
0
    def test_remove_from_index_shared_hashes_partial(self):
        """
        Test that only some hashes are removed from the hash index, but not
        others when those hashes still refer to other descriptors.
        """
        # Simulate initial state with some descriptor hashed to one value and
        # other descriptors hashed to another.

        # Vectors of length 1 for easy dummy hashing prediction.
        descriptors = [
            DescriptorMemoryElement('t', 0).set_vector([0]),
            DescriptorMemoryElement('t', 1).set_vector([1]),
            DescriptorMemoryElement('t', 2).set_vector([2]),
            DescriptorMemoryElement('t', 3).set_vector([3]),
            DescriptorMemoryElement('t', 4).set_vector([4]),
        ]

        # Dummy hash function to do the simulated thing
        hash_func = DummyHashFunctor()
        hash_func.get_hash = mock.Mock(
            # Vectors of even sum hash to 0, odd to 1.
            side_effect=lambda vec: [vec.sum() % 2]
        )

        d_set = MemoryDescriptorIndex()
        d_set._table = {
            0: descriptors[0],
            1: descriptors[1],
            2: descriptors[2],
            3: descriptors[3],
            4: descriptors[4],
        }

        hash2uid_kvs = MemoryKeyValueStore()
        hash2uid_kvs._table = {
            0: {0, 2, 4},
            1: {1, 3},
        }

        idx = LSHNearestNeighborIndex(hash_func, d_set, hash2uid_kvs)
        idx.hash_index = mock.Mock(spec=HashIndex)

        idx.remove_from_index([1, 2, 3])
        # Check that only one hash vector was passed to hash_index's removal
        # method (deque of hash-code vectors).
        idx.hash_index.remove_from_index.assert_called_once_with(
            collections.deque([
                [1],
            ])
        )
        self.assertDictEqual(d_set._table, {
            0: descriptors[0],
            4: descriptors[4],
        })
        self.assertDictEqual(hash2uid_kvs._table, {0: {0, 4}})
示例#13
0
    def test_remove_from_index_shared_hashes_partial(self):
        """
        Test that only some hashes are removed from the hash index, but not
        others when those hashes still refer to other descriptors.
        """
        # Simulate initial state with some descriptor hashed to one value and
        # other descriptors hashed to another.

        # Vectors of length 1 for easy dummy hashing prediction.
        descriptors = [
            DescriptorMemoryElement('t', 0).set_vector([0]),
            DescriptorMemoryElement('t', 1).set_vector([1]),
            DescriptorMemoryElement('t', 2).set_vector([2]),
            DescriptorMemoryElement('t', 3).set_vector([3]),
            DescriptorMemoryElement('t', 4).set_vector([4]),
        ]

        # Dummy hash function to do the simulated thing
        hash_func = DummyHashFunctor()
        hash_func.get_hash = mock.Mock(
            # Vectors of even sum hash to 0, odd to 1.
            side_effect=lambda vec: [vec.sum() % 2]
        )

        d_set = MemoryDescriptorIndex()
        d_set._table = {
            0: descriptors[0],
            1: descriptors[1],
            2: descriptors[2],
            3: descriptors[3],
            4: descriptors[4],
        }

        hash2uid_kvs = MemoryKeyValueStore()
        hash2uid_kvs._table = {
            0: {0, 2, 4},
            1: {1, 3},
        }

        idx = LSHNearestNeighborIndex(hash_func, d_set, hash2uid_kvs)
        idx.hash_index = mock.Mock(spec=HashIndex)

        idx.remove_from_index([1, 2, 3])
        # Check that only one hash vector was passed to hash_index's removal
        # method (deque of hash-code vectors).
        idx.hash_index.remove_from_index.assert_called_once_with(
            collections.deque([
                [1],
            ])
        )
        self.assertDictEqual(d_set._table, {
            0: descriptors[0],
            4: descriptors[4],
        })
        self.assertDictEqual(hash2uid_kvs._table, {0: {0, 4}})
示例#14
0
 def test_has_valid_key(self):
     s = MemoryKeyValueStore()
     s._table = {
         'a': 0,
         'b': 1,
         0: 2,
     }
     nose.tools.assert_true(s.has('a'))
     nose.tools.assert_true(s.has('b'))
     nose.tools.assert_true(s.has(0))
     nose.tools.assert_false(s.has('c'))
示例#15
0
 def test_has_key(self):
     """ Test that has-key returns true for entered keys. """
     s = MemoryKeyValueStore()
     s._table = {
         'a': 0,
         'b': 1,
         0: 2,
     }
     self.assertTrue(s.has('a'))
     self.assertTrue(s.has('b'))
     self.assertTrue(s.has(0))
     self.assertFalse(s.has('c'))
 def test_has_key(self):
     """ Test that has-key returns true for entered keys. """
     s = MemoryKeyValueStore()
     s._table = {
         'a': 0,
         'b': 1,
         0: 2,
     }
     self.assertTrue(s.has('a'))
     self.assertTrue(s.has('b'))
     self.assertTrue(s.has(0))
     self.assertFalse(s.has('c'))
示例#17
0
    def test_remove_many(self):
        """
        Test expected remove_many functionality.
        """
        s = MemoryKeyValueStore()
        s._table = {
            0: 0,
            1: 1,
            2: 2,
        }

        s.remove_many([0, 1])
        self.assertDictEqual(s._table, {2: 2})
示例#18
0
 def test_count(self):
     """
     Test that count returns appropriately based on table state.
     """
     s = MemoryKeyValueStore()
     assert s.count() == 0
     s._table = {
         0: 0,
         1: 1,
         'a': True,
         None: False
     }
     assert s.count() == 4
示例#19
0
    def test_clear_readonly(self):
        """ Test trying to clear on a read-only store. """
        table_before_clear = dict(a=1, b=2, c=3)

        s = MemoryKeyValueStore()
        s._table = table_before_clear
        s.is_read_only = mock.MagicMock(return_value=True)

        self.assertRaises(
            ReadOnlyError,
            s.clear
        )
        self.assertEqual(s._table, table_before_clear)
 def test_keys_with_table(self):
     """
     Test that keys returned reflect the table state.
     """
     s = MemoryKeyValueStore()
     s._table = {
         'a': 'b',
         'c': 1,
         'asdfghsdfg': None,
         'r3adf3a#+': [4, 5, 6, '7'],
     }
     self.assertSetEqual(set(s.keys()),
                         {'a', 'c', 'asdfghsdfg', 'r3adf3a#+'})
    def test_remove_many(self):
        """
        Test expected remove_many functionality.
        """
        s = MemoryKeyValueStore()
        s._table = {
            0: 0,
            1: 1,
            2: 2,
        }

        s.remove_many([0, 1])
        self.assertDictEqual(s._table, {2: 2})
示例#22
0
 def test_remove_missing_key(self):
     """
     Test that we cannot remove a key not in the store.
     """
     s = MemoryKeyValueStore()
     s._table = {
         0: 1,
         'a': 'b'
     }
     self.assertRaises(
         KeyError,
         s.remove, 'some-key'
     )
     # table should remain unchanged.
     self.assertDictEqual(s._table, {0: 1, 'a': 'b'})
示例#23
0
 def test_keys_with_table(self):
     """
     Test that keys returned reflect the table state.
     """
     s = MemoryKeyValueStore()
     s._table = {
         'a': 'b',
         'c': 1,
         'asdfghsdfg': None,
         'r3adf3a#+': [4, 5, 6, '7'],
     }
     self.assertSetEqual(
         set(s.keys()),
         {'a', 'c', 'asdfghsdfg', 'r3adf3a#+'}
     )
示例#24
0
    def test_remove_many_missing_key(self):
        """
        Test that we cannot remove keys not present in table and that table
        is not modified on error.
        """
        expected_table = {
            0: 0,
            1: 1,
            2: 2,
        }

        s = MemoryKeyValueStore()
        s._table = {
            0: 0,
            1: 1,
            2: 2,
        }

        self.assertRaisesRegexp(
            KeyError, 'a',
            s.remove_many, ['a']
        )
        self.assertDictEqual(s._table, expected_table)

        # Even if one of the keys is value, the table should not be modified if
        # one of the keys is invalid.
        self.assertRaisesRegexp(
            KeyError, '6',
            s.remove_many, [1, 6]
        )
        self.assertDictEqual(s._table, expected_table)

        PY2_SET_KEY_ERROR_RE = "set\(\[(?:7|8), (?:7|8)\]\)"
        PY3_SET_KEY_ERROR_RE = "{(?:7|8), (?:7|8)}"
        self.assertRaisesRegexp(
            KeyError,
            # Should show a "set" that contains 7 and 8, regardless of order.
            '(?:{}|{})'.format(PY2_SET_KEY_ERROR_RE, PY3_SET_KEY_ERROR_RE),
            s.remove_many, [7, 8]
        )
示例#25
0
 def test_count(self):
     s = MemoryKeyValueStore()
     assert s.count() == 0
     s._table = {0: 0, 1: 1, 'a': True, None: False}
     assert s.count() == 4