From aed0928ba87827f90329f6e49d8c2b6a144c368b Mon Sep 17 00:00:00 2001 From: M1ha Date: Thu, 10 Jan 2019 17:02:58 +0500 Subject: [PATCH] Added tests on syncing data to non default database --- tests/fixtures/test_secondary_model.json | 18 ++++ tests/migrations/0001_initial.py | 2 +- tests/routers.py | 6 +- tests/settings.py | 8 ++ tests/test_models.py | 116 +++++++++++++---------- 5 files changed, 99 insertions(+), 51 deletions(-) create mode 100644 tests/fixtures/test_secondary_model.json diff --git a/tests/fixtures/test_secondary_model.json b/tests/fixtures/test_secondary_model.json new file mode 100644 index 0000000..1a37bf5 --- /dev/null +++ b/tests/fixtures/test_secondary_model.json @@ -0,0 +1,18 @@ +[ + { + "model": "tests.SecondaryTestModel", + "pk": 1, + "fields": { + "value": 100, + "created_date": "2018-01-01" + } + }, + { + "model": "tests.SecondaryTestModel", + "pk": 2, + "fields": { + "value": 200, + "created_date": "2018-02-01" + } + } +] \ No newline at end of file diff --git a/tests/migrations/0001_initial.py b/tests/migrations/0001_initial.py index ead88c1..41b0a33 100644 --- a/tests/migrations/0001_initial.py +++ b/tests/migrations/0001_initial.py @@ -24,7 +24,7 @@ class Migration(migrations.Migration): }, ), migrations.CreateModel( - name='SecondTestModel', + name='SecondaryTestModel', fields=[ ('id', models.AutoField()), ('value', models.IntegerField()), diff --git a/tests/routers.py b/tests/routers.py index ec04820..68d3e39 100644 --- a/tests/routers.py +++ b/tests/routers.py @@ -12,5 +12,7 @@ class SecondaryRouter: return True def allow_migrate(self, db, app_label, model=None, **hints): - if model.__name__.lower().startswith('secondary'): - return 'secondary' + if model and model.__name__.lower().startswith('secondary'): + return db == 'secondary' + else: + return False if db == 'secondary' else None diff --git a/tests/settings.py b/tests/settings.py index 3ce169a..5c4f558 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -12,6 +12,14 @@ DATABASES = { 'HOST': '127.0.0.1', 'PORT': '5432' }, + 'secondary': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'test2', + 'USER': 'test', + 'PASSWORD': 'test', + 'HOST': '127.0.0.1', + 'PORT': '5432' + }, # I need separate connections for multiprocessing tests 'test_db': { diff --git a/tests/test_models.py b/tests/test_models.py index 36b1a6d..c9bfd05 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -12,10 +12,21 @@ from tests.models import TestModel, SecondaryTestModel # 2) I call transaction.on_commit(), expecting no transaction at the moment # 3) TestCase rollbacks transaction, on_commit not called class ClickHouseDjangoModelTest(TransactionTestCase): + def test_clickhouse_sync_models(self): + self.assertSetEqual({ClickHouseSecondTestModel}, SecondaryTestModel.get_clickhouse_sync_models()) + self.assertSetEqual({ClickHouseTestModel, ClickHouseCollapseTestModel, ClickHouseMultiTestModel}, + TestModel.get_clickhouse_sync_models()) + + +class TestOperations(TransactionTestCase): fixtures = ['test_model'] + django_model = TestModel + clickhouse_model = ClickHouseTestModel + db_alias = 'default' + multi_db = True def setUp(self): - self.storage = TestModel.get_clickhouse_storage() + self.storage = self.django_model.get_clickhouse_storage() self.storage.flush() def tearDown(self): @@ -23,96 +34,105 @@ class ClickHouseDjangoModelTest(TransactionTestCase): def test_save(self): # INSERT operation - instance = TestModel(created_date=datetime.date.today(), value=2) + instance = self.django_model(created_date=datetime.date.today(), value=2) instance.save() - self.assertListEqual([('insert', "default.%d" % instance.pk)], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) # UPDATE operation instance.save() - self.assertListEqual([('insert', "default.%d" % instance.pk), ('update', "default.%d" % instance.pk)], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk)), + ('update', "%s.%d" % (self.db_alias, instance.pk))], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) def test_create(self): - instance = TestModel.objects.create(pk=100555, created_date=datetime.date.today(), value=2) - self.assertListEqual([('insert', "default.%d" % instance.pk)], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + instance = self.django_model.objects.create(pk=100555, created_date=datetime.date.today(), value=2) + self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) def test_bulk_create(self): - items = [TestModel(created_date=datetime.date.today(), value=i) for i in range(5)] - items = TestModel.objects.bulk_create(items) + items = [self.django_model(created_date=datetime.date.today(), value=i) for i in range(5)] + items = self.django_model.objects.bulk_create(items) self.assertEqual(5, len(items)) - self.assertListEqual([('insert', "default.%d" % instance.pk) for instance in items], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk)) for instance in items], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) def test_get_or_create(self): - instance, created = TestModel.objects. \ + instance, created = self.django_model.objects. \ get_or_create(pk=100, defaults={'created_date': datetime.date.today(), 'value': 2}) self.assertTrue(created) - self.assertListEqual([('insert', "default.%d" % instance.pk)], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) - instance, created = TestModel.objects. \ + instance, created = self.django_model.objects. \ get_or_create(pk=100, defaults={'created_date': datetime.date.today(), 'value': 2}) self.assertFalse(created) - self.assertListEqual([('insert', "default.%d" % instance.pk)], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) def test_update_or_create(self): - instance, created = TestModel.objects. \ + instance, created = self.django_model.objects. \ update_or_create(pk=100, defaults={'created_date': datetime.date.today(), 'value': 2}) self.assertTrue(created) - self.assertListEqual([('insert', "default.%d" % instance.pk)], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) - instance, created = TestModel.objects. \ + instance, created = self.django_model.objects. \ update_or_create(pk=100, defaults={'created_date': datetime.date.today(), 'value': 2}) self.assertFalse(created) - self.assertListEqual([('insert', "default.%d" % instance.pk), ('update', "default.%d" % instance.pk)], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual( + [('insert', "%s.%d" % (self.db_alias, instance.pk)), ('update', "%s.%d" % (self.db_alias, instance.pk))], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) def test_qs_update(self): - TestModel.objects.filter(pk=1).update(created_date=datetime.date.today()) - self.assertListEqual([('update', 'default.1')], self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.django_model.objects.filter(pk=1).update(created_date=datetime.date.today()) + self.assertListEqual([('update', "%s.1" % self.db_alias)], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) # Update, after which updated element will not suit update conditions - TestModel.objects.filter(created_date__lt=datetime.date.today()). \ + self.django_model.objects.filter(created_date__lt=datetime.date.today()). \ update(created_date=datetime.date.today()) - self.assertListEqual([('update', 'default.1'), ('update', 'default.2')], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual([('update', "%s.1" % self.db_alias), ('update', "%s.2" % self.db_alias)], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) def test_qs_update_returning(self): - TestModel.objects.filter(pk=1).update_returning(created_date=datetime.date.today()) - self.assertListEqual([('update', 'default.1')], self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.django_model.objects.filter(pk=1).update_returning(created_date=datetime.date.today()) + self.assertListEqual([('update', "%s.1" % self.db_alias)], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) # Update, after which updated element will not suit update conditions - TestModel.objects.filter(created_date__lt=datetime.date.today()). \ + self.django_model.objects.filter(created_date__lt=datetime.date.today()). \ update_returning(created_date=datetime.date.today()) - self.assertListEqual([('update', 'default.1'), ('update', 'default.2')], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual([('update', "%s.1" % self.db_alias), ('update', "%s.2" % self.db_alias)], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) def test_qs_delete_returning(self): - TestModel.objects.filter(pk=1).delete_returning() - self.assertListEqual([('delete', 'default.1')], self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.django_model.objects.filter(pk=1).delete_returning() + self.assertListEqual([('delete', "%s.1" % self.db_alias)], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) # Update, после которого исходный фильтр уже не сработает - TestModel.objects.filter(created_date__lt=datetime.date.today()).delete_returning() - self.assertListEqual([('delete', 'default.1'), ('delete', 'default.2')], - self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.django_model.objects.filter(created_date__lt=datetime.date.today()).delete_returning() + self.assertListEqual([('delete', "%s.1" % self.db_alias), ('delete', "%s.2" % self.db_alias)], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) def test_delete(self): - instance = TestModel.objects.get(pk=1) + instance = self.django_model.objects.get(pk=1) instance.delete() - self.assertListEqual([('delete', 'default.1')], self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.assertListEqual([('delete', "%s.1" % self.db_alias)], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) def test_qs_delete(self): - TestModel.objects.filter(pk=1).delete() - self.assertListEqual([('delete', 'default.1')], self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) + self.django_model.objects.filter(pk=1).delete() + self.assertListEqual([('delete', "%s.1" % self.db_alias)], + self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)) - def test_clickhouse_sync_models(self): - self.assertSetEqual({ClickHouseSecondTestModel}, SecondaryTestModel.get_clickhouse_sync_models()) - self.assertSetEqual({ClickHouseTestModel, ClickHouseCollapseTestModel, ClickHouseMultiTestModel}, - TestModel.get_clickhouse_sync_models()) + +class TestSecondaryOperations(TestOperations): + fixtures = ['test_secondary_model'] + django_model = SecondaryTestModel + clickhouse_model = ClickHouseSecondTestModel + db_alias = 'secondary'