From c29569892f3d91c3ad6686a5cb7a0a1e2d39c5ec Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Wed, 21 Apr 2021 20:18:02 -0500 Subject: Reorganize backend integration tests and add some more thorough tests --- tests/integration/test_sqlite.py | 92 ++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 46 deletions(-) (limited to 'tests/integration/test_sqlite.py') diff --git a/tests/integration/test_sqlite.py b/tests/integration/test_sqlite.py index a6fd50d..1924909 100644 --- a/tests/integration/test_sqlite.py +++ b/tests/integration/test_sqlite.py @@ -4,78 +4,78 @@ from threading import Thread from unittest.mock import patch from requests_cache.backends.sqlite import DbDict, DbPickleDict -from tests.integration.test_backends import BaseStorageTestCase +from tests.integration.test_backends import CACHE_NAME, BaseStorageTestCase class SQLiteTestCase(BaseStorageTestCase): def tearDown(self): try: - os.unlink(self.NAMESPACE) + os.unlink(CACHE_NAME) except Exception: pass def test_bulk_commit(self): - d = self.storage_class(self.NAMESPACE, self.TABLES[0]) - with d.bulk_commit(): + cache = self.init_cache() + with cache.bulk_commit(): pass - d.clear() + n = 1000 - with d.bulk_commit(): + with cache.bulk_commit(): for i in range(n): - d[i] = i - assert list(d.keys()) == list(range(n)) + cache[i] = i + assert list(cache.keys()) == list(range(n)) def test_switch_commit(self): - d = self.storage_class(self.NAMESPACE) - d.clear() - d[1] = 1 - d = self.storage_class(self.NAMESPACE) - assert 1 in d + cache = self.init_cache() + cache.clear() + cache['key_1'] = 'value_1' + cache = self.init_cache(clear=False) + assert 'key_1' in cache - d._can_commit = False - d[2] = 2 + cache._can_commit = False + cache['key_2'] = 'value_2' - d = self.storage_class(self.NAMESPACE) - assert 2 not in d - assert d._can_commit is True + cache = self.init_cache(clear=False) + assert 2 not in cache + assert cache._can_commit is True def test_fast_save(self): - d1 = self.storage_class(self.NAMESPACE, fast_save=True) - d2 = self.storage_class(self.NAMESPACE, self.TABLES[1], fast_save=True) - d1.clear() + cache_1 = self.init_cache(1, fast_save=True) + cache_2 = self.init_cache(2, fast_save=True) + n = 1000 for i in range(n): - d1[i] = i - d2[i * 2] = i - # HACK if we will not sort, fast save can produce different order of records - assert sorted(d1.keys()) == list(range(n)) - assert sorted(d2.values()) == list(range(n)) + cache_1[i] = i + cache_2[i * 2] = i + + assert set(cache_1.keys()) == set(range(n)) + assert set(cache_2.values()) == set(range(n)) def test_usage_with_threads(self): - def do_test_for(d, n_threads=5): - d.clear() + def do_test_for(cache, n_threads=5): + cache.clear() def do_inserts(values): for v in values: - d[v] = v + cache[v] = v def values(x, n): return [i * x for i in range(n)] threads = [Thread(target=do_inserts, args=(values(i, n_threads),)) for i in range(n_threads)] - for t in threads: - t.start() - for t in threads: - t.join() + for thread in threads: + thread.start() + for thread in threads: + thread.join() for i in range(n_threads): for x in values(i, n_threads): - assert d[x] == x + assert cache[x] == x - do_test_for(self.storage_class(self.NAMESPACE)) - do_test_for(self.storage_class(self.NAMESPACE, fast_save=True), 20) - do_test_for(self.storage_class(self.NAMESPACE, fast_save=True)) - do_test_for(self.storage_class(self.NAMESPACE, self.TABLES[1], fast_save=True)) + do_test_for(self.init_cache()) + do_test_for(self.init_cache(fast_save=True), 20) + do_test_for(self.init_cache(fast_save=True)) + do_test_for(self.init_cache('table_2', fast_save=True)) def test_noop(self): def do_noop_bulk(d): @@ -83,14 +83,14 @@ class SQLiteTestCase(BaseStorageTestCase): pass del d - d = self.storage_class(self.NAMESPACE) - t = Thread(target=do_noop_bulk, args=(d,)) - t.start() - t.join() + cache = self.init_cache() + thread = Thread(target=do_noop_bulk, args=(cache,)) + thread.start() + thread.join() # make sure connection is not closed by the thread - d[0] = 0 - assert str(d) == "{0: 0}" + cache[0] = 0 + assert str(cache) == "{0: 0}" class DbDictTestCase(SQLiteTestCase, unittest.TestCase): @@ -106,5 +106,5 @@ class DbPickleDictTestCase(SQLiteTestCase, unittest.TestCase): @patch('requests_cache.backends.sqlite.sqlite3') def test_connection_kwargs(mock_sqlite): """A spot check to make sure optional connection kwargs gets passed to connection""" - DbDict('test', timeout=0.5, invalid_kwarg='???') - mock_sqlite.connect.assert_called_with('test', timeout=0.5) + cache = DbDict('test', timeout=0.5, invalid_kwarg='???') + mock_sqlite.connect.assert_called_with(cache.db_path, timeout=0.5) -- cgit v1.2.1 From b1070a950e0365085b7edf8ef2e4a65c77630ac9 Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Wed, 21 Apr 2021 23:33:18 -0500 Subject: Turn remaining unittest.TestCase classes into pytest-style test classes --- tests/integration/test_sqlite.py | 62 +++++++++++----------------------------- 1 file changed, 17 insertions(+), 45 deletions(-) (limited to 'tests/integration/test_sqlite.py') diff --git a/tests/integration/test_sqlite.py b/tests/integration/test_sqlite.py index 1924909..e93216d 100644 --- a/tests/integration/test_sqlite.py +++ b/tests/integration/test_sqlite.py @@ -1,14 +1,14 @@ import os -import unittest from threading import Thread from unittest.mock import patch from requests_cache.backends.sqlite import DbDict, DbPickleDict -from tests.integration.test_backends import CACHE_NAME, BaseStorageTestCase +from tests.integration.test_backends import CACHE_NAME, BaseStorageTest -class SQLiteTestCase(BaseStorageTestCase): - def tearDown(self): +class SQLiteTestCase(BaseStorageTest): + @classmethod + def teardown_class(cls): try: os.unlink(CACHE_NAME) except Exception: @@ -51,37 +51,11 @@ class SQLiteTestCase(BaseStorageTestCase): assert set(cache_1.keys()) == set(range(n)) assert set(cache_2.values()) == set(range(n)) - def test_usage_with_threads(self): - def do_test_for(cache, n_threads=5): - cache.clear() - - def do_inserts(values): - for v in values: - cache[v] = v - - def values(x, n): - return [i * x for i in range(n)] - - threads = [Thread(target=do_inserts, args=(values(i, n_threads),)) for i in range(n_threads)] - for thread in threads: - thread.start() - for thread in threads: - thread.join() - - for i in range(n_threads): - for x in values(i, n_threads): - assert cache[x] == x - - do_test_for(self.init_cache()) - do_test_for(self.init_cache(fast_save=True), 20) - do_test_for(self.init_cache(fast_save=True)) - do_test_for(self.init_cache('table_2', fast_save=True)) - def test_noop(self): - def do_noop_bulk(d): - with d.bulk_commit(): + def do_noop_bulk(cache): + with cache.bulk_commit(): pass - del d + del cache cache = self.init_cache() thread = Thread(target=do_noop_bulk, args=(cache,)) @@ -92,19 +66,17 @@ class SQLiteTestCase(BaseStorageTestCase): cache[0] = 0 assert str(cache) == "{0: 0}" - -class DbDictTestCase(SQLiteTestCase, unittest.TestCase): - def __init__(self, *args, **kwargs): - super().__init__(*args, storage_class=DbDict, **kwargs) + @patch('requests_cache.backends.sqlite.sqlite3') + 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', timeout=0.5, invalid_kwarg='???') + mock_sqlite.connect.assert_called_with(cache.db_path, timeout=0.5) -class DbPickleDictTestCase(SQLiteTestCase, unittest.TestCase): - def __init__(self, *args, **kwargs): - super().__init__(*args, storage_class=DbPickleDict, picklable=True, **kwargs) +class TestDbDict(SQLiteTestCase): + storage_class = DbDict -@patch('requests_cache.backends.sqlite.sqlite3') -def test_connection_kwargs(mock_sqlite): - """A spot check to make sure optional connection kwargs gets passed to connection""" - cache = DbDict('test', timeout=0.5, invalid_kwarg='???') - mock_sqlite.connect.assert_called_with(cache.db_path, timeout=0.5) +class TestDbPickleDict(SQLiteTestCase): + storage_class = DbPickleDict + picklable = True -- cgit v1.2.1 From 3ec85f0107caedbe3b3bd8080bd3dac81b7baa17 Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Thu, 22 Apr 2021 00:02:08 -0500 Subject: Turn multi-threaded stress tests into test (sub)classes This is mainly to take advantage of fail-fast connection tests; otherwise, these tests may just hang if backend dependenices are installed but backend services are not set up. See issue #221 for details. --- tests/integration/test_sqlite.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'tests/integration/test_sqlite.py') diff --git a/tests/integration/test_sqlite.py b/tests/integration/test_sqlite.py index e93216d..63c67ec 100644 --- a/tests/integration/test_sqlite.py +++ b/tests/integration/test_sqlite.py @@ -2,8 +2,8 @@ import os from threading import Thread from unittest.mock import patch -from requests_cache.backends.sqlite import DbDict, DbPickleDict -from tests.integration.test_backends import CACHE_NAME, BaseStorageTest +from requests_cache.backends.sqlite import DbCache, DbDict, DbPickleDict +from tests.integration.test_backends import CACHE_NAME, BaseCacheTest, BaseStorageTest class SQLiteTestCase(BaseStorageTest): @@ -63,8 +63,8 @@ class SQLiteTestCase(BaseStorageTest): thread.join() # make sure connection is not closed by the thread - cache[0] = 0 - assert str(cache) == "{0: 0}" + cache['key_1'] = 'value_1' + assert list(cache.keys()) == ['key_1'] @patch('requests_cache.backends.sqlite.sqlite3') def test_connection_kwargs(self, mock_sqlite): @@ -80,3 +80,15 @@ class TestDbDict(SQLiteTestCase): class TestDbPickleDict(SQLiteTestCase): storage_class = DbPickleDict picklable = True + + +class TestDbCache(BaseCacheTest): + backend_class = DbCache + init_kwargs = {'use_temp': True} + + @classmethod + def teardown_class(cls): + try: + os.unlink(CACHE_NAME) + except Exception: + pass -- cgit v1.2.1