Example #1
0
    def test_get_oldest(self):
        cache = Cache(5, 60.0)

        cache._items = {
            "k1": ["data", 1.0],
            "k2": ["data", 1.5],
            "k3": ["data", 0.5]
        }
        self.assertEqual(cache.get_oldest(), "k3")

        cache._items = {"k1": ["data", 0.1], "k2": ["data", 1.0]}
        self.assertEqual(cache.get_oldest(), "k1")
Example #2
0
    def test_valid_timestamp_false(self, mock_time):
        cache = Cache(5, 60.0)

        cache._items = {"k1": ["v1", 20.0]}
        mock_time.return_value = 100.0
        self.assertFalse(cache._valid_timestamp("k1"))
        mock_time.assert_called_once()
Example #3
0
    def test_add_cache_full(self, mock_time, mock_get_oldest):
        cache = Cache(5, 60.0)

        cache._items = {
            "k1": ["v1", 0.6],
            "k2": ["v2", 0.5],
            "k3": ["v3", 0.1],
            "k4": ["v4", 1.5],
            "k5": ["v5", 2.1]
        }

        mock_get_oldest.return_value = "k3"

        cache.add("k6", "v6")
        mock_get_oldest.assert_called_once()
        mock_time.assert_called_once()

        self.assertEqual(
            cache._items, {
                "k1": ["v1", 0.6],
                "k2": ["v2", 0.5],
                "k6": ["v6", mock_time.return_value],
                "k4": ["v4", 1.5],
                "k5": ["v5", 2.1]
            })
Example #4
0
    def test_store_failure(self, mock_write_dict):
        cache = Cache(5, 60.0)

        cache._items = {"k1": ["v1", 0.5], "k2": ["v2", 0.9]}
        mock_write_dict.return_value = False

        self.assertFalse(cache.store("somefile"))
        mock_write_dict.assert_called_once_with("somefile", cache._items)
Example #5
0
    def test_get_item_in_cache_invalid_timestamp(self, mock_valid_timestamp):
        cache = Cache(5, 60.0)

        cache._items = {"k1": ["v1", 0.1]}
        mock_valid_timestamp.return_value = False

        self.assertIsNone(cache.get("k1"))
        mock_valid_timestamp.assert_called_once_with("k1")
Example #6
0
    def test_get_item_in_cache_valid_timestamp(self, mock_valid_timestamp):
        cache = Cache(5, 60.0)

        cache._items = {"k1": ["v1", 0.1]}
        mock_valid_timestamp.return_value = True

        self.assertEqual(cache.get("k1"), "v1")
        mock_valid_timestamp.assert_called_once_with("k1")
Example #7
0
    def test_add_item_already_in_cache(self, mock_time):
        cache = Cache(5, 60.0)

        cache._items = {"k1": ["v1", 0.1]}

        cache.add("k1", "v10")
        mock_time.assert_called_once()
        self.assertEqual(cache._items, {"k1": ["v10", mock_time.return_value]})
Example #8
0
    def test_has_space_false(self):
        cache = Cache(5, 60.0)
        cache._items = {
            "k1": ["v1", 0.1],
            "k2": ["v2", 0.5],
            "k3": ["v3", 1.2],
            "k4": ["v4", 5.8],
            "k5": ["v5", 6.4]
        }

        self.assertFalse(cache.has_space())
Example #9
0
    def test_valid_timestamp_true(self, mock_time):
        cache = Cache(5, 60.0)
        cache._items = {"k1": ["v1", 20.0]}

        mock_time.return_value = 50.0
        self.assertTrue(cache._valid_timestamp("k1"))
        mock_time.assert_called_once()

        mock_time.reset_mock()

        mock_time.return_value = 80.0
        self.assertTrue(cache._valid_timestamp("k1"))
        mock_time.assert_called_once()
Example #10
0
    def test_remove_old_empty_cache(self, mock_valid_timestamp):
        cache = Cache(5, 60.0)

        items = {
            "k1": ["v1", 0.1],
            "k2": ["v2", 0.5],
            "k3": ["v3", 1.8],
            "k4": ["v4", 55.5]
        }
        cache._items = items

        mock_valid_timestamp.return_value = True

        self.assertEqual(cache.remove_old(), 0)
        self.assertEqual(cache._items, items)
        mock_valid_timestamp.assert_has_calls(
            [mock.call(key) for key in items])
Example #11
0
    def test_remove_old(self, mock_valid_timestamp):
        cache = Cache(5, 60.0)

        items = {
            "k1": ["v1", 0.1],
            "k2": ["v2", 0.5],
            "k3": ["v3", 1.8],
            "k4": ["v4", 55.5]
        }
        cache._items = items

        mock_valid_timestamp.side_effect = lambda x: True if x == "k4" else False

        self.assertEqual(cache.remove_old(), 3)
        self.assertEqual(cache._items, {"k4": ["v4", 55.5]})
        mock_valid_timestamp.assert_has_calls(
            [mock.call(key) for key in items])
Example #12
0
 def test_has_key_true(self):
     cache = Cache(5, 60.0)
     cache._items = {"k1": ["data", 0.1]}
     self.assertTrue(cache.has("k1"))
Example #13
0
 def test_repr(self):
     cache = Cache(5, 60.0)
     cache._items = {"k1": ["v1", 0.5]}
     self.assertEqual(cache.__repr__(), "Cache([(u'k1', [u'v1', 0.5])])")
Example #14
0
 def test_get_items(self):
     cache = Cache(5, 60.0)
     cache._items = {"k1": ["v1", 0.5], "k2": ["v2", 0.8]}
     self.assertEqual(sorted(cache.items()), [("k1", ["v1", 0.5]),
                                              ("k2", ["v2", 0.8])])