mirror of
https://github.com/carrotquest/django-clickhouse.git
synced 2024-11-22 00:56:37 +03:00
Added tests on syncing data to non default database
This commit is contained in:
parent
96300761a7
commit
aed0928ba8
18
tests/fixtures/test_secondary_model.json
vendored
Normal file
18
tests/fixtures/test_secondary_model.json
vendored
Normal 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"
|
||||
}
|
||||
}
|
||||
]
|
|
@ -24,7 +24,7 @@ class Migration(migrations.Migration):
|
|||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='SecondTestModel',
|
||||
name='SecondaryTestModel',
|
||||
fields=[
|
||||
('id', models.AutoField()),
|
||||
('value', models.IntegerField()),
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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': {
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in New Issue
Block a user