Added tests and fixes for bulk_create_returning and save_returning

This commit is contained in:
M1ha 2019-10-04 17:54:47 +05:00
parent 6493373c8f
commit a39a04fa52
3 changed files with 44 additions and 4 deletions

View File

@ -102,7 +102,7 @@ class ClickHouseSyncQuerySetMixin(ClickHouseSyncRegisterMixin):
# I add library dependant mixins to base classes only if libraries are installed
qs_bases = [ClickHouseSyncQuerySetMixin, DjangoQuerySet]
qs_bases = [ClickHouseSyncQuerySetMixin]
if not getattr(UpdateReturningMixin, 'fake', False):
qs_bases.append(ClickHouseSyncUpdateReturningQuerySetMixin)
@ -110,6 +110,9 @@ if not getattr(UpdateReturningMixin, 'fake', False):
if not getattr(BulkUpdateManagerMixin, 'fake', False):
qs_bases.append(ClickHouseSyncBulkUpdateQuerySetMixin)
# QuerySet must be the last one, so it can be redeclared in mixins
qs_bases.append(DjangoQuerySet)
ClickHouseSyncQuerySet = type('ClickHouseSyncModelQuerySet', tuple(qs_bases), {})

View File

@ -2,17 +2,31 @@
This file contains sample models to use in tests
"""
from django.db import models
from django.db.models.manager import BaseManager
from django_pg_returning.models import UpdateReturningModel
from django_clickhouse.models import ClickHouseSyncModel
from django_clickhouse.models import ClickHouseSyncModel, ClickHouseSyncQuerySet
class TestModel(ClickHouseSyncModel):
class TestQuerySet(ClickHouseSyncQuerySet):
pass
class TestManager(BaseManager.from_queryset(TestQuerySet)):
pass
class TestModel(UpdateReturningModel, ClickHouseSyncModel):
objects = TestManager()
value = models.IntegerField()
created_date = models.DateField()
created = models.DateTimeField()
class SecondaryTestModel(ClickHouseSyncModel):
class SecondaryTestModel(UpdateReturningModel, ClickHouseSyncModel):
objects = TestManager()
value = models.IntegerField()
created_date = models.DateField()
created = models.DateTimeField()

View File

@ -102,6 +102,16 @@ class TestOperations(TransactionTestCase):
self.assertListEqual([('update', "%s.%d" % (self.db_alias, item.id)) for item in self.before_op_items],
self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
def test_bulk_create_returning(self):
items = [
self.django_model(created_date=datetime.date.today(), created=datetime.datetime.now(), value=i)
for i in range(5)
]
items = self.django_model.objects.bulk_create_returning(items)
self.assertEqual(5, len(items))
self.assertSetEqual({('insert', "%s.%d" % (self.db_alias, instance.pk)) for instance in items},
set(self.storage.get_operations(self.clickhouse_model.get_import_key(), 10)))
def test_qs_update_returning(self):
self.django_model.objects.filter(pk=1).update_returning(created_date=datetime.date.today())
self.assertListEqual([('update', "%s.1" % self.db_alias)],
@ -123,6 +133,19 @@ class TestOperations(TransactionTestCase):
self.assertListEqual([('delete', "%s.%d" % (self.db_alias, item.id)) for item in self.before_op_items],
self.storage.get_operations(self.clickhouse_model.get_import_key(), 10))
def test_save_returning(self):
# INSERT operation
instance = self.django_model(created_date=datetime.date.today(), created=datetime.datetime.now(), value=2)
instance.save_returning()
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_returning()
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_delete(self):
instance = self.django_model.objects.get(pk=1)
instance.delete()