Make tests pass

This commit is contained in:
Itai Shirav 2018-04-21 13:14:15 +03:00
parent b6229125a3
commit 66eda2214c
4 changed files with 19 additions and 37 deletions

View File

@ -328,8 +328,12 @@ class Database(object):
return pytz.utc
def _get_server_version(self, as_tuple=True):
try:
r = self._send('SELECT version();')
ver = r.text
except ServerError as e:
logger.exception('Cannot determine server version (%s), assuming 1.1.0', e)
ver = '1.1.0'
return tuple(int(n) for n in ver.split('.')) if as_tuple else ver
def _is_connection_readonly(self):

View File

@ -197,7 +197,7 @@ class Merge(Engine):
self.table_regex = table_regex
def create_table_sql(self, db):
self.db_name = None + return "Merge(`%s`, '%s')" % (db.db_name, self.table_regex)
return "Merge(`%s`, '%s')" % (db.db_name, self.table_regex)
class Distributed(Engine):
@ -210,20 +210,17 @@ class Distributed(Engine):
See full documentation here
https://clickhouse.yandex/docs/en/table_engines/distributed.html
"""
def __init__(self, cluster, table=None, db_name=None, sharding_key=None):
def __init__(self, cluster, table=None, sharding_key=None):
"""
:param cluster: what cluster to access data from
:param table: underlying table that actually stores data.
If you are not specifying any table here, ensure that it can be inferred
from your model's superclass (see models.DistributedModel.fix_engine_table)
:param db_name: which database to access data from
By default it is 'currentDatabase()'
:param sharding_key: how to distribute data among shards when inserting
straightly into Distributed table, optional
"""
self.cluster = cluster
self.table = table
self.db_name = db_name
self.sharding_key = sharding_key
@property
@ -238,23 +235,17 @@ class Distributed(Engine):
return table
def set_db_name(self, db_name):
assert isinstance(db_name, six.string_types), "'db_name' parameter must be string"
self.db_name = db_name
def create_table_sql(self):
def create_table_sql(self, db):
name = self.__class__.__name__
params = self._build_sql_params()
params = self._build_sql_params(db)
return '%s(%s)' % (name, ', '.join(params))
def _build_sql_params(self):
db_name = ("`%s`" % self.db_name) if self.db_name else 'currentDatabase()'
def _build_sql_params(self, db):
if self.table_name is None:
raise ValueError("Cannot create {} engine: specify an underlying table".format(
self.__class__.__name__))
params = [self.cluster, db_name, self.table_name]
params = ["`%s`" % p for p in [self.cluster, db.db_name, self.table_name]]
if self.sharding_key:
params.append(self.sharding_key)
return params

View File

@ -304,7 +304,6 @@ class DistributedModel(Model):
def set_database(self, db):
assert isinstance(self.engine, Distributed), "engine must be engines.Distributed instance"
res = super(DistributedModel, self).set_database(db)
self.engine.set_db_name(db.db_name)
return res
@classmethod
@ -359,16 +358,13 @@ class DistributedModel(Model):
cls.engine.table = storage_models[0]
@classmethod
def create_table_sql(cls, db_name):
def create_table_sql(cls, db):
assert isinstance(cls.engine, Distributed), "engine must be engines.Distributed instance"
cls.engine.set_db_name(db_name)
cls.fix_engine_table()
parts = [
'CREATE TABLE IF NOT EXISTS `{0}`.`{1}` AS `{0}`.`{2}`'.format(
db_name, cls.table_name(), cls.engine.table_name),
'ENGINE = ' + cls.engine.create_table_sql()]
db.db_name, cls.table_name(), cls.engine.table_name),
'ENGINE = ' + cls.engine.create_table_sql(db)]
return '\n'.join(parts)
cls.engine.set_db_name(db_name)
return super(MergeModel, cls).create_table_sql(db_name)

View File

@ -1,18 +1,9 @@
from __future__ import unicode_literals
import unittest
<<<<<<< HEAD
from infi.clickhouse_orm.system_models import SystemPart
from infi.clickhouse_orm.database import Database, DatabaseException, ServerError
from infi.clickhouse_orm.models import Model, MergeModel, DistributedModel
||||||| merged common ancestors
from infi.clickhouse_orm.database import Database, DatabaseException
from infi.clickhouse_orm.models import Model, MergeModel
=======
from infi.clickhouse_orm.system_models import SystemPart
from infi.clickhouse_orm.database import Database, DatabaseException
from infi.clickhouse_orm.models import Model, MergeModel
>>>>>>> 7fb05896926acab163a1f373092bf22cc0f3cb4f
from infi.clickhouse_orm.fields import *
from infi.clickhouse_orm.engines import *
@ -167,15 +158,15 @@ class DistributedTestCase(_EnginesHelperTestCase):
engine = Distributed('my_cluster')
with self.assertRaises(ValueError) as cm:
engine.create_table_sql()
engine.create_table_sql(self.database)
exc = cm.exception
self.assertEqual(str(exc), 'Cannot create Distributed engine: specify an underlying table')
def test_with_table_name(self):
engine = Distributed('my_cluster', 'foo')
sql = engine.create_table_sql()
self.assertEqual(sql, 'Distributed(my_cluster, currentDatabase(), foo)')
sql = engine.create_table_sql(self.database)
self.assertEqual(sql, 'Distributed(`my_cluster`, `test-db`, `foo`)')
class TestModel(SampleModel):
engine = TinyLog()