Added tests on syncing data to non default database

This commit is contained in:
M1ha 2019-01-10 17:02:58 +05:00
parent 96300761a7
commit aed0928ba8
5 changed files with 99 additions and 51 deletions

View File

@ -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"
}
}
]

View File

@ -24,7 +24,7 @@ class Migration(migrations.Migration):
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
name='SecondTestModel', name='SecondaryTestModel',
fields=[ fields=[
('id', models.AutoField()), ('id', models.AutoField()),
('value', models.IntegerField()), ('value', models.IntegerField()),

View File

@ -12,5 +12,7 @@ class SecondaryRouter:
return True return True
def allow_migrate(self, db, app_label, model=None, **hints): def allow_migrate(self, db, app_label, model=None, **hints):
if model.__name__.lower().startswith('secondary'): if model and model.__name__.lower().startswith('secondary'):
return 'secondary' return db == 'secondary'
else:
return False if db == 'secondary' else None

View File

@ -12,6 +12,14 @@ DATABASES = {
'HOST': '127.0.0.1', 'HOST': '127.0.0.1',
'PORT': '5432' '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 # I need separate connections for multiprocessing tests
'test_db': { 'test_db': {

View File

@ -12,10 +12,21 @@ from tests.models import TestModel, SecondaryTestModel
# 2) I call transaction.on_commit(), expecting no transaction at the moment # 2) I call transaction.on_commit(), expecting no transaction at the moment
# 3) TestCase rollbacks transaction, on_commit not called # 3) TestCase rollbacks transaction, on_commit not called
class ClickHouseDjangoModelTest(TransactionTestCase): 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'] fixtures = ['test_model']
django_model = TestModel
clickhouse_model = ClickHouseTestModel
db_alias = 'default'
multi_db = True
def setUp(self): def setUp(self):
self.storage = TestModel.get_clickhouse_storage() self.storage = self.django_model.get_clickhouse_storage()
self.storage.flush() self.storage.flush()
def tearDown(self): def tearDown(self):
@ -23,96 +34,105 @@ class ClickHouseDjangoModelTest(TransactionTestCase):
def test_save(self): def test_save(self):
# INSERT operation # INSERT operation
instance = TestModel(created_date=datetime.date.today(), value=2) instance = self.django_model(created_date=datetime.date.today(), value=2)
instance.save() instance.save()
self.assertListEqual([('insert', "default.%d" % instance.pk)], self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))],
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
# UPDATE operation # UPDATE operation
instance.save() instance.save()
self.assertListEqual([('insert', "default.%d" % instance.pk), ('update', "default.%d" % instance.pk)], self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk)),
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) ('update', "%s.%d" % (self.db_alias, instance.pk))],
self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
def test_create(self): def test_create(self):
instance = TestModel.objects.create(pk=100555, created_date=datetime.date.today(), value=2) instance = self.django_model.objects.create(pk=100555, created_date=datetime.date.today(), value=2)
self.assertListEqual([('insert', "default.%d" % instance.pk)], self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))],
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
def test_bulk_create(self): def test_bulk_create(self):
items = [TestModel(created_date=datetime.date.today(), value=i) for i in range(5)] items = [self.django_model(created_date=datetime.date.today(), value=i) for i in range(5)]
items = TestModel.objects.bulk_create(items) items = self.django_model.objects.bulk_create(items)
self.assertEqual(5, len(items)) self.assertEqual(5, len(items))
self.assertListEqual([('insert', "default.%d" % instance.pk) for instance in items], self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk)) for instance in items],
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
def test_get_or_create(self): 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}) get_or_create(pk=100, defaults={'created_date': datetime.date.today(), 'value': 2})
self.assertTrue(created) self.assertTrue(created)
self.assertListEqual([('insert', "default.%d" % instance.pk)], self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))],
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) 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}) get_or_create(pk=100, defaults={'created_date': datetime.date.today(), 'value': 2})
self.assertFalse(created) self.assertFalse(created)
self.assertListEqual([('insert', "default.%d" % instance.pk)], self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))],
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
def test_update_or_create(self): 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}) update_or_create(pk=100, defaults={'created_date': datetime.date.today(), 'value': 2})
self.assertTrue(created) self.assertTrue(created)
self.assertListEqual([('insert', "default.%d" % instance.pk)], self.assertListEqual([('insert', "%s.%d" % (self.db_alias, instance.pk))],
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) 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}) update_or_create(pk=100, defaults={'created_date': datetime.date.today(), 'value': 2})
self.assertFalse(created) self.assertFalse(created)
self.assertListEqual([('insert', "default.%d" % instance.pk), ('update', "default.%d" % instance.pk)], self.assertListEqual(
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) [('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): def test_qs_update(self):
TestModel.objects.filter(pk=1).update(created_date=datetime.date.today()) self.django_model.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.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 # 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()) update(created_date=datetime.date.today())
self.assertListEqual([('update', 'default.1'), ('update', 'default.2')], self.assertListEqual([('update', "%s.1" % self.db_alias), ('update', "%s.2" % self.db_alias)],
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
def test_qs_update_returning(self): def test_qs_update_returning(self):
TestModel.objects.filter(pk=1).update_returning(created_date=datetime.date.today()) self.django_model.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.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 # 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()) update_returning(created_date=datetime.date.today())
self.assertListEqual([('update', 'default.1'), ('update', 'default.2')], self.assertListEqual([('update', "%s.1" % self.db_alias), ('update', "%s.2" % self.db_alias)],
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
def test_qs_delete_returning(self): def test_qs_delete_returning(self):
TestModel.objects.filter(pk=1).delete_returning() self.django_model.objects.filter(pk=1).delete_returning()
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))
# Update, после которого исходный фильтр уже не сработает # Update, после которого исходный фильтр уже не сработает
TestModel.objects.filter(created_date__lt=datetime.date.today()).delete_returning() self.django_model.objects.filter(created_date__lt=datetime.date.today()).delete_returning()
self.assertListEqual([('delete', 'default.1'), ('delete', 'default.2')], self.assertListEqual([('delete', "%s.1" % self.db_alias), ('delete', "%s.2" % self.db_alias)],
self.storage.get_operations(ClickHouseTestModel.get_import_key(), 10)) self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
def test_delete(self): def test_delete(self):
instance = TestModel.objects.get(pk=1) instance = self.django_model.objects.get(pk=1)
instance.delete() 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): def test_qs_delete(self):
TestModel.objects.filter(pk=1).delete() self.django_model.objects.filter(pk=1).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_clickhouse_sync_models(self):
self.assertSetEqual({ClickHouseSecondTestModel}, SecondaryTestModel.get_clickhouse_sync_models()) class TestSecondaryOperations(TestOperations):
self.assertSetEqual({ClickHouseTestModel, ClickHouseCollapseTestModel, ClickHouseMultiTestModel}, fixtures = ['test_secondary_model']
TestModel.get_clickhouse_sync_models()) django_model = SecondaryTestModel
clickhouse_model = ClickHouseSecondTestModel
db_alias = 'secondary'