2018-11-20 15:24:15 +03:00
|
|
|
import datetime
|
|
|
|
|
2018-11-14 10:52:01 +03:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2019-01-24 09:31:16 +03:00
|
|
|
from django_clickhouse.exceptions import RedisLockTimeoutError
|
2018-11-15 11:02:05 +03:00
|
|
|
from django_clickhouse.storages import RedisStorage
|
2019-01-24 09:31:16 +03:00
|
|
|
from tests.clickhouse_models import ClickHouseTestModel, ClickHouseCollapseTestModel
|
2018-11-14 10:52:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
class StorageTest(TestCase):
|
|
|
|
storage = RedisStorage()
|
|
|
|
|
|
|
|
def setUp(self):
|
2018-11-15 11:02:05 +03:00
|
|
|
self.storage.flush()
|
2021-07-08 10:27:35 +03:00
|
|
|
|
2018-11-15 11:02:05 +03:00
|
|
|
def tearDown(self):
|
|
|
|
self.storage.flush()
|
2018-11-14 10:52:01 +03:00
|
|
|
|
|
|
|
def test_operation_pks(self):
|
2018-11-14 13:18:06 +03:00
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100500)
|
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100501)
|
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100502)
|
2018-11-14 10:52:01 +03:00
|
|
|
self.assertListEqual([
|
|
|
|
('insert', '100500'),
|
|
|
|
('insert', '100501'),
|
|
|
|
('insert', '100502'),
|
|
|
|
], self.storage.get_operations('test', 10))
|
|
|
|
|
|
|
|
def test_operation_types(self):
|
2018-11-14 13:18:06 +03:00
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100500)
|
|
|
|
self.storage.register_operations_wrapped('test', 'update', 100500)
|
|
|
|
self.storage.register_operations_wrapped('test', 'delete', 100500)
|
2018-11-14 10:52:01 +03:00
|
|
|
self.assertListEqual([
|
|
|
|
('insert', '100500'),
|
|
|
|
('update', '100500'),
|
|
|
|
('delete', '100500'),
|
|
|
|
], self.storage.get_operations('test', 10))
|
|
|
|
|
|
|
|
def test_operation_import_keys(self):
|
2018-11-14 13:18:06 +03:00
|
|
|
self.storage.register_operations_wrapped('test1', 'insert', 100500)
|
|
|
|
self.storage.register_operations_wrapped('test2', 'insert', 100500)
|
|
|
|
self.storage.register_operations_wrapped('test2', 'insert', 100501)
|
2018-11-14 10:52:01 +03:00
|
|
|
self.assertListEqual([
|
|
|
|
('insert', '100500')
|
|
|
|
], self.storage.get_operations('test1', 10))
|
|
|
|
self.assertListEqual([
|
|
|
|
('insert', '100500'),
|
|
|
|
('insert', '100501'),
|
|
|
|
], self.storage.get_operations('test2', 10))
|
|
|
|
|
2018-11-14 11:02:33 +03:00
|
|
|
def test_post_sync(self):
|
2018-11-20 15:24:15 +03:00
|
|
|
self.storage.pre_sync('test')
|
2018-11-14 13:18:06 +03:00
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100500)
|
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100501)
|
2018-11-14 11:02:33 +03:00
|
|
|
self.storage.get_operations('test', 10)
|
2018-11-14 13:18:06 +03:00
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100502)
|
2018-11-14 11:02:33 +03:00
|
|
|
|
|
|
|
self.storage.post_sync('test')
|
|
|
|
self.assertListEqual([
|
|
|
|
('insert', '100502')
|
|
|
|
], self.storage.get_operations('test', 10))
|
2018-11-20 15:24:15 +03:00
|
|
|
|
|
|
|
def test_last_sync(self):
|
|
|
|
dt = datetime.datetime.now()
|
|
|
|
self.storage.set_last_sync_time('test', dt)
|
|
|
|
self.assertEqual(dt, self.storage.get_last_sync_time('test'))
|
2018-11-29 13:19:25 +03:00
|
|
|
|
|
|
|
def test_operations_count(self):
|
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100500)
|
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100501)
|
|
|
|
self.assertEqual(2, self.storage.operations_count('test'))
|
|
|
|
self.storage.register_operations_wrapped('test', 'insert', 100502)
|
|
|
|
self.assertEqual(3, self.storage.operations_count('test'))
|
2019-01-24 09:31:16 +03:00
|
|
|
|
|
|
|
def test_locks(self):
|
|
|
|
# Test that multiple can acquire locks in parallel
|
|
|
|
# And single model can't
|
2021-07-08 10:27:35 +03:00
|
|
|
lock = self.storage.get_lock(ClickHouseTestModel.get_import_key())
|
|
|
|
lock.acquire()
|
2019-01-24 09:31:16 +03:00
|
|
|
with self.assertRaises(RedisLockTimeoutError):
|
2021-07-08 10:27:35 +03:00
|
|
|
lock.acquire()
|
2019-01-24 09:31:16 +03:00
|
|
|
|
2021-07-08 10:27:35 +03:00
|
|
|
lock_2 = self.storage.get_lock(ClickHouseCollapseTestModel.get_import_key())
|
|
|
|
lock_2.acquire()
|