MCPcopy Create free account
hub / github.com/requests-cache/requests-cache / TestSQLiteCache

Class TestSQLiteCache

tests/integration/test_sqlite.py:330–453  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

328
329
330class TestSQLiteCache(BaseCacheTest):
331 backend_class = SQLiteCache
332 init_kwargs = {'use_temp': True}
333
334 @classmethod
335 def teardown_class(cls):
336 try:
337 os.unlink(CACHE_NAME)
338 except Exception:
339 pass
340
341 @patch.object(BaseCache, 'clear', side_effect=IOError)
342 @patch('requests_cache.backends.sqlite.unlink', side_effect=os.unlink)
343 def test_clear__failure(self, mock_unlink, mock_clear):
344 """When a corrupted cache prevents a normal DROP TABLE, clear() should still succeed"""
345 session = self.init_session(clear=False)
346 session.cache.responses['key_1'] = 'value_1'
347 db_path = session.cache.responses.db_path
348 session.cache.clear()
349
350 assert len(session.cache.responses) == 0
351 mock_unlink.assert_called_once_with(db_path)
352
353 @patch.object(BaseCache, 'clear', side_effect=IOError)
354 def test_clear__file_already_deleted(self, mock_clear):
355 session = self.init_session(clear=False)
356 session.cache.responses['key_1'] = 'value_1'
357 os.unlink(session.cache.responses.db_path)
358 session.cache.clear()
359
360 assert len(session.cache.responses) == 0
361
362 def test_db_path(self):
363 """This is just provided as an alias, since both requests and redirects share the same db
364 file
365 """
366 session = self.init_session()
367 assert session.cache.db_path == session.cache.responses.db_path
368
369 def test_count(self):
370 """count() should work the same as len(), but with the option to exclude expired responses"""
371 session = self.init_session()
372 now = utcnow()
373 session.cache.responses['key_1'] = CachedResponse(expires=now + timedelta(1))
374 session.cache.responses['key_2'] = CachedResponse(expires=now - timedelta(1))
375
376 assert session.cache.count() == 2
377 assert session.cache.count(expired=False) == 1
378
379 def test_delete__single_key(self):
380 """Vacuum should not be used after delete if there is only a single key"""
381 session = self.init_session()
382 session.cache.responses['key_1'] = 'value_1'
383
384 with patch.object(SQLiteDict, 'vacuum') as mock_vacuum:
385 session.cache.delete('key_1')
386 mock_vacuum.assert_not_called()
387

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…