Example #1
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 #2
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 #3
0
    def test_add_cache_empty(self, mock_time):
        cache = Cache(5, 60.0)

        cache.add("k1", "data")
        mock_time.assert_called_once()

        self.assertEqual(cache._items,
                         {"k1": ["data", mock_time.return_value]})
Example #4
0
    def test_size(self):
        cache = Cache(500, 60.0)
        for i in range(100):
            cache.add(i, i)

        self.assertEqual(len(cache), 100)