2017-08-16 23:48:18 +03:00
|
|
|
from __future__ import unicode_literals
|
2018-04-12 12:21:46 +03:00
|
|
|
|
2017-01-31 10:43:11 +03:00
|
|
|
import unittest
|
|
|
|
from datetime import date
|
2018-04-12 12:21:46 +03:00
|
|
|
|
2017-01-31 10:43:11 +03:00
|
|
|
import os
|
2018-04-12 12:21:46 +03:00
|
|
|
|
2017-09-07 15:44:27 +03:00
|
|
|
from infi.clickhouse_orm.database import Database, DatabaseException
|
2017-01-31 10:43:11 +03:00
|
|
|
from infi.clickhouse_orm.engines import *
|
|
|
|
from infi.clickhouse_orm.fields import *
|
|
|
|
from infi.clickhouse_orm.models import Model
|
|
|
|
from infi.clickhouse_orm.system_models import SystemPart
|
|
|
|
|
|
|
|
|
2017-09-07 15:44:27 +03:00
|
|
|
class SystemTest(unittest.TestCase):
|
2019-06-13 08:38:06 +03:00
|
|
|
|
2017-09-07 15:44:27 +03:00
|
|
|
def setUp(self):
|
2019-06-13 08:12:56 +03:00
|
|
|
self.database = Database('test-db', log_statements=True)
|
2017-09-07 15:44:27 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.database.drop_database()
|
|
|
|
|
|
|
|
def test_insert_system(self):
|
|
|
|
m = SystemPart()
|
|
|
|
with self.assertRaises(DatabaseException):
|
|
|
|
self.database.insert([m])
|
|
|
|
|
|
|
|
def test_create_readonly_table(self):
|
|
|
|
with self.assertRaises(DatabaseException):
|
|
|
|
self.database.create_table(SystemTestModel)
|
|
|
|
|
|
|
|
def test_drop_readonly_table(self):
|
|
|
|
with self.assertRaises(DatabaseException):
|
|
|
|
self.database.drop_table(SystemTestModel)
|
|
|
|
|
|
|
|
|
2017-01-31 10:43:11 +03:00
|
|
|
class SystemPartTest(unittest.TestCase):
|
2017-08-20 09:46:00 +03:00
|
|
|
|
|
|
|
BACKUP_DIRS = ['/var/lib/clickhouse/shadow', '/opt/clickhouse/shadow/']
|
2017-01-31 10:43:11 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
2019-06-13 08:12:56 +03:00
|
|
|
self.database = Database('test-db', log_statements=True)
|
2017-01-31 10:43:11 +03:00
|
|
|
self.database.create_table(TestTable)
|
2018-04-12 12:21:46 +03:00
|
|
|
self.database.create_table(CustomPartitionedTable)
|
2017-01-31 10:43:11 +03:00
|
|
|
self.database.insert([TestTable(date_field=date.today())])
|
2018-04-12 12:21:46 +03:00
|
|
|
self.database.insert([CustomPartitionedTable(date_field=date.today(), group_field=13)])
|
2017-01-31 10:43:11 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.database.drop_database()
|
|
|
|
|
2017-01-31 16:13:46 +03:00
|
|
|
def _get_backups(self):
|
2017-08-20 09:46:00 +03:00
|
|
|
for dir in self.BACKUP_DIRS:
|
|
|
|
if os.path.exists(dir):
|
|
|
|
_, dirnames, _ = next(os.walk(dir))
|
|
|
|
return dirnames
|
|
|
|
raise unittest.SkipTest('Cannot find backups dir')
|
2017-01-31 10:43:11 +03:00
|
|
|
|
2019-06-13 08:38:06 +03:00
|
|
|
def test_is_read_only(self):
|
|
|
|
self.assertTrue(SystemPart.is_read_only())
|
|
|
|
|
|
|
|
def test_is_system_model(self):
|
|
|
|
self.assertTrue(SystemPart.is_system_model())
|
|
|
|
|
2017-01-31 10:43:11 +03:00
|
|
|
def test_get_all(self):
|
2017-02-08 08:31:19 +03:00
|
|
|
parts = SystemPart.get(self.database)
|
2018-04-12 12:21:46 +03:00
|
|
|
self.assertEqual(len(list(parts)), 2)
|
2017-01-31 10:43:11 +03:00
|
|
|
|
|
|
|
def test_get_active(self):
|
|
|
|
parts = list(SystemPart.get_active(self.database))
|
2018-04-12 12:21:46 +03:00
|
|
|
self.assertEqual(len(parts), 2)
|
2017-02-09 14:49:25 +03:00
|
|
|
parts[0].detach()
|
2019-06-13 08:38:06 +03:00
|
|
|
parts = list(SystemPart.get_active(self.database))
|
|
|
|
self.assertEqual(len(parts), 1)
|
2017-01-31 10:43:11 +03:00
|
|
|
|
2017-02-08 08:31:19 +03:00
|
|
|
def test_get_conditions(self):
|
|
|
|
parts = list(SystemPart.get(self.database, conditions="table='testtable'"))
|
|
|
|
self.assertEqual(len(parts), 1)
|
2018-04-12 12:21:46 +03:00
|
|
|
parts = list(SystemPart.get(self.database, conditions=u"table='custompartitionedtable'"))
|
|
|
|
self.assertEqual(len(parts), 1)
|
|
|
|
parts = list(SystemPart.get(self.database, conditions=u"table='invalidtable'"))
|
2017-02-08 08:31:19 +03:00
|
|
|
self.assertEqual(len(parts), 0)
|
|
|
|
|
2017-01-31 10:43:11 +03:00
|
|
|
def test_attach_detach(self):
|
|
|
|
parts = list(SystemPart.get_active(self.database))
|
2018-04-12 12:21:46 +03:00
|
|
|
self.assertEqual(len(parts), 2)
|
|
|
|
for p in parts:
|
|
|
|
p.detach()
|
2017-01-31 10:43:11 +03:00
|
|
|
self.assertEqual(len(list(SystemPart.get_active(self.database))), 0)
|
2018-04-12 12:21:46 +03:00
|
|
|
for p in parts:
|
|
|
|
p.attach()
|
|
|
|
self.assertEqual(len(list(SystemPart.get_active(self.database))), 2)
|
2017-01-31 10:43:11 +03:00
|
|
|
|
|
|
|
def test_drop(self):
|
|
|
|
parts = list(SystemPart.get_active(self.database))
|
2018-04-12 12:21:46 +03:00
|
|
|
for p in parts:
|
|
|
|
p.drop()
|
2017-01-31 10:43:11 +03:00
|
|
|
self.assertEqual(len(list(SystemPart.get_active(self.database))), 0)
|
|
|
|
|
|
|
|
def test_freeze(self):
|
2017-02-08 08:31:19 +03:00
|
|
|
parts = list(SystemPart.get(self.database))
|
2017-01-31 10:43:11 +03:00
|
|
|
# There can be other backups in the folder
|
2017-01-31 16:13:46 +03:00
|
|
|
prev_backups = set(self._get_backups())
|
2018-04-12 12:21:46 +03:00
|
|
|
for p in parts:
|
|
|
|
p.freeze()
|
2017-01-31 16:13:46 +03:00
|
|
|
backups = set(self._get_backups())
|
2018-04-12 12:21:46 +03:00
|
|
|
self.assertEqual(len(backups), len(prev_backups) + 2)
|
2017-01-31 10:43:11 +03:00
|
|
|
|
|
|
|
def test_fetch(self):
|
|
|
|
# TODO Not tested, as I have no replication set
|
|
|
|
pass
|
|
|
|
|
2019-06-13 08:38:06 +03:00
|
|
|
def test_query(self):
|
|
|
|
SystemPart.objects_in(self.database).count()
|
|
|
|
list(SystemPart.objects_in(self.database).filter(table='testtable'))
|
|
|
|
|
2017-01-31 10:43:11 +03:00
|
|
|
|
|
|
|
class TestTable(Model):
|
|
|
|
date_field = DateField()
|
|
|
|
|
|
|
|
engine = MergeTree('date_field', ('date_field',))
|
2017-09-07 15:44:27 +03:00
|
|
|
|
|
|
|
|
2018-04-12 12:21:46 +03:00
|
|
|
class CustomPartitionedTable(Model):
|
|
|
|
date_field = DateField()
|
|
|
|
group_field = UInt32Field()
|
|
|
|
|
|
|
|
engine = MergeTree(order_by=('date_field', 'group_field'), partition_key=('toYYYYMM(date_field)', 'group_field'))
|
|
|
|
|
|
|
|
|
2017-09-07 15:44:27 +03:00
|
|
|
class SystemTestModel(Model):
|
2018-05-14 14:09:57 +03:00
|
|
|
_system = True
|