diff options
| author | Jordan Cook <jordan.cook@pioneer.com> | 2021-03-31 20:50:02 -0500 |
|---|---|---|
| committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-03-31 22:56:46 -0500 |
| commit | a2f0045a7ba3ed302ff91be5c3f5fcd287f69ce2 (patch) | |
| tree | 271f1488c3df40fc3d1ded357b31beb1f9733098 /tests/integration/test_sqlite.py | |
| parent | 7435cb64814809d072dceec3404b2f304d89a0a7 (diff) | |
| download | requests-cache-a2f0045a7ba3ed302ff91be5c3f5fcd287f69ce2.tar.gz | |
Split tests into unit and integration tests and run separately in CI; update Contributing Guide with more notes on testing
Diffstat (limited to 'tests/integration/test_sqlite.py')
| -rw-r--r-- | tests/integration/test_sqlite.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/integration/test_sqlite.py b/tests/integration/test_sqlite.py new file mode 100644 index 0000000..4b5b950 --- /dev/null +++ b/tests/integration/test_sqlite.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +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 BaseBackendTestCase + + +class DbdictTestCase(BaseBackendTestCase, unittest.TestCase): + def test_bulk_commit(self): + d = DbDict(self.NAMESPACE, self.TABLES[0]) + with d.bulk_commit(): + pass + d.clear() + n = 1000 + with d.bulk_commit(): + for i in range(n): + d[i] = i + self.assertEqual(list(d.keys()), list(range(n))) + + def test_switch_commit(self): + d = DbDict(self.NAMESPACE) + d.clear() + d[1] = 1 + d = DbDict(self.NAMESPACE) + self.assertIn(1, d) + + d._can_commit = False + d[2] = 2 + + d = DbDict(self.NAMESPACE) + self.assertNotIn(2, d) + self.assertTrue(d._can_commit) + + def test_fast_save(self): + d1 = DbDict(self.NAMESPACE, fast_save=True) + d2 = DbDict(self.NAMESPACE, self.TABLES[1], fast_save=True) + d1.clear() + 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 + self.assertEqual(sorted(d1.keys()), list(range(n))) + self.assertEqual(sorted(d2.values()), list(range(n))) + + def test_usage_with_threads(self): + def do_test_for(d, n_threads=5): + d.clear() + fails = [] + + def do_inserts(values): + try: + for v in values: + d[v] = v + except Exception: + fails.append(1) + raise + + 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() + + self.assertFalse(fails) + for i in range(n_threads): + for x in values(i, n_threads): + self.assertEqual(d[x], x) + + do_test_for(DbDict(self.NAMESPACE, fast_save=True), 20) + do_test_for(DbPickleDict(self.NAMESPACE, fast_save=True), 10) + d1 = DbDict(self.NAMESPACE, fast_save=True) + d2 = DbDict(self.NAMESPACE, self.TABLES[1], fast_save=True) + do_test_for(d1) + do_test_for(d2) + do_test_for(DbDict(self.NAMESPACE)) + + +@patch('requests_cache.backends.sqlite.sqlite3') +def test_timeout(mock_sqlite): + """Just make sure the optional 'timeout' param gets passed to sqlite3.connect""" + DbDict('test', timeout=0.5) + mock_sqlite.connect.assert_called_with('test', timeout=0.5) + + +if __name__ == '__main__': + unittest.main() |
