Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Jan 27, 2025
1 parent 7f94434 commit 219f0d0
Showing 1 changed file with 4 additions and 30 deletions.
34 changes: 4 additions & 30 deletions tests/integration/test_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def teardown_class(cls):
def test_connection_kwargs(self, mock_sqlite):
"""A spot check to make sure optional connection kwargs gets passed to connection"""
cache = self.storage_class('test', use_temp=True, timeout=0.5, invalid_kwarg='???')
mock_sqlite.connect.assert_called_with(cache.db_path, timeout=0.5, check_same_thread=False)
mock_sqlite.connect.assert_called_with(cache.db_path, timeout=0.5, isolation_level=None, check_same_thread=False)

def test_use_cache_dir(self):
relative_path = self.storage_class(CACHE_NAME).db_path
Expand Down Expand Up @@ -171,39 +171,13 @@ def test_wal_sync_mode(self):
r = con.execute('PRAGMA synchronous').fetchone()
assert r[0] == 0

def test_write_retry(self):
def test_write_acquire_lock(self):
"""Writes to the database acquire the sqlite lock"""
cache = self.init_cache()
locked_error = sqlite3.OperationalError('database is locked')
with patch.object(cache, '_write', side_effect=[locked_error, 1]) as mock_write:
with patch.object(cache, '_acquire_sqlite_lock') as mock_write:
cache['key_1'] = 'value_1'
assert mock_write.call_count == 2

def test_write_retry__exceeded_retries(self):
cache = self.init_cache()
locked_error = sqlite3.OperationalError('database is locked')

with patch.object(cache, '_write', side_effect=locked_error) as mock_write:
cache['key_1'] = 'value_1'
assert mock_write.call_count == 3
assert 'key_1' not in cache

# Set a custom number of retries
cache = self.init_cache(retries=5)
with patch.object(cache, '_write', side_effect=locked_error) as mock_write:
cache['key_1'] = 'value_1'
assert mock_write.call_count == 5

# Set retries to 0 to disable retrying
cache = self.init_cache(retries=0)
with patch.object(cache, '_write', side_effect=locked_error) as mock_write:
with pytest.raises(sqlite3.OperationalError):
cache['key_1'] = 'value_1'
assert mock_write.call_count == 1

# Expect no change to behavior if retrying is disabled and there are no errors
cache['key_1'] = 'value_1'
assert 'key_1' in cache

def test_write_retry__other_errors(self):
"""Errors other than 'OperationalError: database is locked' should not be retried"""
cache = self.init_cache()
Expand Down

0 comments on commit 219f0d0

Please sign in to comment.