def test_query_cache(self): # TODO Stop this test from flapping. get_mock.reset_mock() model = CachingPynamoModel({}) CachingPynamoModel.cache = TTLCache(maxsize=128, ttl=0.001) result = model.query('1234') self.assertIsInstance(result, QueryResult) self.assertEqual(len(result.items), 4) item = model.get(result.items[0].hash_id, result.items[0].range_id) self.assertEqual(item.kind, 'abcd') # Should have pulled from cache get_mock.assert_not_called() self.assertEqual(len(model.cache), 4) sleep(.05) self.assertEqual(len(model.cache), 0) item = model.get(result.items[0].hash_id, result.items[0].range_id) self.assertEqual(item, get_mock_result) get_mock.assert_called_once()
def test_get_cache(self): get_mock.reset_mock() model = CachingPynamoModel({}) CachingPynamoModel.cache = TTLCache(maxsize=128, ttl=0.001) item = model.get(get_mock_result.hash_id, get_mock_result.range_id) self.assertEqual(item.kind, 'abcd') get_mock.assert_called_once() item = model.get(get_mock_result.hash_id, get_mock_result.range_id) self.assertEqual(item, get_mock_result) # Should have pulled from cache get_mock.assert_called_once() sleep(.005) model.get(get_mock_result.hash_id, get_mock_result.range_id) self.assertEqual(get_mock.call_count, 2)
def test_delete_cache(self): get_mock.reset_mock() model = CachingPynamoModel({}) CachingPynamoModel.cache = TTLCache(maxsize=128, ttl=0.001) key = [random_key(), random_key()] model._get_keys = MagicMock(return_value=key) model.save() item = model.get(*key) self.assertEqual(item, model) get_mock.assert_not_called() model.delete() item = model.get(*key) self.assertEqual(item, get_mock_result) get_mock.assert_called_once()
def test_update_cache(self): get_mock.reset_mock() model = CachingPynamoModel({}) CachingPynamoModel.cache = TTLCache(maxsize=128, ttl=0.001) key = [random_key(), random_key()] model._get_keys = MagicMock(return_value=key) model.update(actions=[]) item = model.get(*key) self.assertEqual(item, model) get_mock.assert_not_called()