diff --git a/tests/clickhouse_models.py b/tests/clickhouse_models.py index 8b9cfb5..12fb2f5 100644 --- a/tests/clickhouse_models.py +++ b/tests/clickhouse_models.py @@ -2,7 +2,7 @@ from infi.clickhouse_orm import fields from django_clickhouse.clickhouse_models import ClickHouseModel, ClickHouseMultiModel from django_clickhouse.engines import ReplacingMergeTree, CollapsingMergeTree -from tests.models import TestModel, SecondTestModel +from tests.models import TestModel, SecondaryTestModel class ClickHouseTestModel(ClickHouseModel): @@ -40,7 +40,7 @@ class ClickHouseMultiTestModel(ClickHouseMultiModel): class ClickHouseSecondTestModel(ClickHouseModel): - django_model = SecondTestModel + django_model = SecondaryTestModel sync_delay = 2 sync_enabled = True diff --git a/tests/models.py b/tests/models.py index 701a7d0..03f8931 100644 --- a/tests/models.py +++ b/tests/models.py @@ -11,6 +11,6 @@ class TestModel(ClickHouseSyncModel): created_date = models.DateField() -class SecondTestModel(ClickHouseSyncModel): +class SecondaryTestModel(ClickHouseSyncModel): value = models.IntegerField() created_date = models.DateField() diff --git a/tests/routers.py b/tests/routers.py new file mode 100644 index 0000000..ec04820 --- /dev/null +++ b/tests/routers.py @@ -0,0 +1,16 @@ + +class SecondaryRouter: + def db_for_read(self, model, **hints): + if model.__name__.lower().startswith('secondary'): + return 'secondary' + + def db_for_write(self, model, **hints): + if model.__name__.lower().startswith('secondary'): + return 'secondary' + + def allow_relation(self, obj1, obj2, **hints): + return True + + def allow_migrate(self, db, app_label, model=None, **hints): + if model.__name__.lower().startswith('secondary'): + return 'secondary' diff --git a/tests/settings.py b/tests/settings.py index c2abae6..3ce169a 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -81,3 +81,5 @@ CLICKHOUSE_REDIS_CONFIG = { 'db': 8, 'socket_timeout': 10 } + +DATABASE_ROUTERS = ['tests.routers.SecondaryRouter'] diff --git a/tests/test_models.py b/tests/test_models.py index 1eb3257..36b1a6d 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -4,7 +4,7 @@ from django.test import TransactionTestCase from tests.clickhouse_models import ClickHouseTestModel, ClickHouseSecondTestModel, ClickHouseCollapseTestModel, \ ClickHouseMultiTestModel -from tests.models import TestModel, SecondTestModel +from tests.models import TestModel, SecondaryTestModel # TestCase can't be used here: @@ -113,6 +113,6 @@ class ClickHouseDjangoModelTest(TransactionTestCase): self.assertListEqual([('delete', 'default.1')], self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) def test_clickhouse_sync_models(self): - self.assertSetEqual({ClickHouseSecondTestModel}, SecondTestModel.get_clickhouse_sync_models()) + self.assertSetEqual({ClickHouseSecondTestModel}, SecondaryTestModel.get_clickhouse_sync_models()) self.assertSetEqual({ClickHouseTestModel, ClickHouseCollapseTestModel, ClickHouseMultiTestModel}, TestModel.get_clickhouse_sync_models())