Added tests on

This commit is contained in:
M1ha 2018-11-21 16:50:54 +05:00
parent a37cd25453
commit 2dab8fcccb
4 changed files with 40 additions and 3 deletions

View File

@ -9,6 +9,7 @@ from typing import List, Tuple
from django.db.models import Model as DjangoModel
from django.utils.timezone import now
from infi.clickhouse_orm.database import Database
from infi.clickhouse_orm.engines import CollapsingMergeTree
from infi.clickhouse_orm.models import Model as InfiModel, ModelBase as InfiModelBase
from six import with_metaclass
from statsd.defaults.django import statsd
@ -91,8 +92,11 @@ class ClickHouseModel(with_metaclass(ClickHouseModelMeta, InfiModel)):
def _prepare_val_for_eq(self, field_name, field, val):
if isinstance(val, datetime.datetime):
return val.replace(microsecond=0)
elif field_name == '_version':
return True # Независимо от версии должно сравнение быть истиной
# Sign column for collapsing should be ignored
if isinstance(self.engine, CollapsingMergeTree) and field_name == self.engine.sign_col:
return True
return val
def __eq__(self, other):
@ -184,4 +188,6 @@ class ClickHouseModel(with_metaclass(ClickHouseModelMeta, InfiModel)):
if last_sync_time is None:
return True
print(last_sync_time)
return (last_sync_time - datetime.datetime.now()).total_seconds() >= cls.get_sync_delay()

View File

@ -1,3 +1,5 @@
from enum import Enum
from django_clickhouse.clickhouse_models import ClickHouseModel
from django_clickhouse.engines import MergeTree, CollapsingMergeTree
from infi.clickhouse_orm import fields
@ -25,4 +27,4 @@ class ClickHouseCollapseTestModel(ClickHouseModel):
value = fields.Int32Field()
sign = fields.Int8Field()
engine = CollapsingMergeTree('created_date', ('id',), 'sign')
engine = CollapsingMergeTree('created_date', ('id',), 'sign')

View File

@ -0,0 +1,29 @@
import datetime
from django.test import TestCase
from tests.clickhouse_models import ClickHouseTestModel
class ClickHouseModelTest(TestCase):
def setUp(self):
self.storage = ClickHouseTestModel.get_storage()
self.storage.flush()
def test_need_sync(self):
# sync is disabled by default
self.assertFalse(ClickHouseTestModel.need_sync())
# There were no syncs. So it should be done
ClickHouseTestModel.sync_enabled = True
self.assertTrue(ClickHouseTestModel.need_sync())
# Time hasn't passed - no sync
self.storage.set_last_sync_time(ClickHouseTestModel.get_import_key(), datetime.datetime.now())
self.assertFalse(ClickHouseTestModel.need_sync())
# Time has passed
sync_delay = ClickHouseTestModel.get_sync_delay()
self.storage.set_last_sync_time(ClickHouseTestModel.get_import_key(),
datetime.datetime.now() - datetime.timedelta(seconds=sync_delay))
self.assertFalse(ClickHouseTestModel.need_sync())