mirror of
https://github.com/carrotquest/django-clickhouse.git
synced 2024-11-25 18:43:46 +03:00
Refactoring
This commit is contained in:
parent
abe0ae45c5
commit
166d23ca7c
|
@ -1,4 +1,4 @@
|
||||||
from typing import Optional, Iterable
|
from typing import Optional, Iterable, List
|
||||||
|
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from infi.clickhouse_orm.database import Database
|
from infi.clickhouse_orm.database import Database
|
||||||
|
@ -70,9 +70,9 @@ class QuerySet(InfiQuerySet):
|
||||||
self.get_database(for_write=True).insert([instance])
|
self.get_database(for_write=True).insert([instance])
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def bulk_create(self, model_instances, batch_size=1000): # type: (Iterable[InfiModel], int)
|
def bulk_create(self, model_instances, batch_size=1000): # type: (Iterable[InfiModel], int) -> List[InfiModel]
|
||||||
self.get_database(for_write=True).insert(model_instances=model_instances, batch_size=batch_size)
|
self.get_database(for_write=True).insert(model_instances=model_instances, batch_size=batch_size)
|
||||||
return model_instances
|
return list(model_instances)
|
||||||
|
|
||||||
|
|
||||||
class AggregateQuerySet(QuerySet, InfiAggregateQuerySet):
|
class AggregateQuerySet(QuerySet, InfiAggregateQuerySet):
|
||||||
|
|
|
@ -25,7 +25,7 @@ class TestQuerySet(TestCase):
|
||||||
def test_all(self):
|
def test_all(self):
|
||||||
self.db.insert([ClickHouseTestModel(id=i, created_date=datetime.date.today(), value=i) for i in range(1, 4)])
|
self.db.insert([ClickHouseTestModel(id=i, created_date=datetime.date.today(), value=i) for i in range(1, 4)])
|
||||||
qs = ClickHouseTestModel.objects.all()
|
qs = ClickHouseTestModel.objects.all()
|
||||||
print(qs.get_database(for_write=True).db_name)
|
|
||||||
self.assertIsInstance(qs, QuerySet)
|
self.assertIsInstance(qs, QuerySet)
|
||||||
self.assertEqual(3, qs.count())
|
self.assertEqual(3, qs.count())
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ class SyncTest(TransactionTestCase):
|
||||||
obj = TestModel.objects.create(value=1, created_date=datetime.date.today())
|
obj = TestModel.objects.create(value=1, created_date=datetime.date.today())
|
||||||
ClickHouseTestModel.sync_batch_from_storage()
|
ClickHouseTestModel.sync_batch_from_storage()
|
||||||
|
|
||||||
synced_data = list(ClickHouseTestModel.objects_in(connections['default']))
|
synced_data = list(ClickHouseTestModel.objects.all())
|
||||||
self.assertEqual(1, len(synced_data))
|
self.assertEqual(1, len(synced_data))
|
||||||
self.assertEqual(obj.created_date, synced_data[0].created_date)
|
self.assertEqual(obj.created_date, synced_data[0].created_date)
|
||||||
self.assertEqual(obj.value, synced_data[0].value)
|
self.assertEqual(obj.value, synced_data[0].value)
|
||||||
|
@ -41,7 +41,7 @@ class SyncTest(TransactionTestCase):
|
||||||
ClickHouseCollapseTestModel.sync_batch_from_storage()
|
ClickHouseCollapseTestModel.sync_batch_from_storage()
|
||||||
|
|
||||||
# sync_batch_from_storage uses FINAL, so data would be collapsed by now
|
# sync_batch_from_storage uses FINAL, so data would be collapsed by now
|
||||||
synced_data = list(ClickHouseCollapseTestModel.objects_in(connections['default']))
|
synced_data = list(ClickHouseCollapseTestModel.objects.all())
|
||||||
self.assertEqual(1, len(synced_data))
|
self.assertEqual(1, len(synced_data))
|
||||||
self.assertEqual(obj.created_date, synced_data[0].created_date)
|
self.assertEqual(obj.created_date, synced_data[0].created_date)
|
||||||
self.assertEqual(obj.value, synced_data[0].value)
|
self.assertEqual(obj.value, synced_data[0].value)
|
||||||
|
@ -65,7 +65,7 @@ class SyncTest(TransactionTestCase):
|
||||||
ClickHouseCollapseTestModel.sync_batch_from_storage()
|
ClickHouseCollapseTestModel.sync_batch_from_storage()
|
||||||
|
|
||||||
# sync_batch_from_storage uses FINAL, so data would be collapsed by now
|
# sync_batch_from_storage uses FINAL, so data would be collapsed by now
|
||||||
synced_data = list(ClickHouseCollapseTestModel.objects_in(connections['default']))
|
synced_data = list(ClickHouseCollapseTestModel.objects.all())
|
||||||
self.assertEqual(0, len(synced_data))
|
self.assertEqual(0, len(synced_data))
|
||||||
|
|
||||||
def test_multi_model(self):
|
def test_multi_model(self):
|
||||||
|
@ -74,14 +74,14 @@ class SyncTest(TransactionTestCase):
|
||||||
obj.save()
|
obj.save()
|
||||||
ClickHouseMultiTestModel.sync_batch_from_storage()
|
ClickHouseMultiTestModel.sync_batch_from_storage()
|
||||||
|
|
||||||
synced_data = list(ClickHouseTestModel.objects_in(connections['default']))
|
synced_data = list(ClickHouseTestModel.objects.all())
|
||||||
self.assertEqual(1, len(synced_data))
|
self.assertEqual(1, len(synced_data))
|
||||||
self.assertEqual(obj.created_date, synced_data[0].created_date)
|
self.assertEqual(obj.created_date, synced_data[0].created_date)
|
||||||
self.assertEqual(obj.value, synced_data[0].value)
|
self.assertEqual(obj.value, synced_data[0].value)
|
||||||
self.assertEqual(obj.id, synced_data[0].id)
|
self.assertEqual(obj.id, synced_data[0].id)
|
||||||
|
|
||||||
# sync_batch_from_storage uses FINAL, so data would be collapsed by now
|
# sync_batch_from_storage uses FINAL, so data would be collapsed by now
|
||||||
synced_data = list(ClickHouseCollapseTestModel.objects_in(connections['default']))
|
synced_data = list(ClickHouseCollapseTestModel.objects.all())
|
||||||
self.assertEqual(1, len(synced_data))
|
self.assertEqual(1, len(synced_data))
|
||||||
self.assertEqual(obj.created_date, synced_data[0].created_date)
|
self.assertEqual(obj.created_date, synced_data[0].created_date)
|
||||||
self.assertEqual(obj.value, synced_data[0].value)
|
self.assertEqual(obj.value, synced_data[0].value)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user