2017-01-31 10:43:11 +03:00
|
|
|
import unittest
|
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
from infi.clickhouse_orm.database import Database
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class SystemPartTest(unittest.TestCase):
|
|
|
|
BACKUP_DIR = '/opt/clickhouse/shadow/'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.database = Database('test-db')
|
|
|
|
self.database.create_table(TestTable)
|
|
|
|
self.database.insert([TestTable(date_field=date.today())])
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.database.drop_database()
|
|
|
|
|
2017-01-31 16:13:46 +03:00
|
|
|
def _get_backups(self):
|
2017-02-19 10:01:02 +03:00
|
|
|
if not os.path.exists(self.BACKUP_DIR):
|
|
|
|
return []
|
2017-01-31 10:43:11 +03:00
|
|
|
_, dirnames, _ = next(os.walk(self.BACKUP_DIR))
|
2017-01-31 16:13:46 +03:00
|
|
|
return dirnames
|
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)
|
2017-01-31 10:43:11 +03:00
|
|
|
self.assertEqual(len(list(parts)), 1)
|
|
|
|
|
|
|
|
def test_get_active(self):
|
|
|
|
parts = list(SystemPart.get_active(self.database))
|
|
|
|
self.assertEqual(len(parts), 1)
|
2017-02-09 14:49:25 +03:00
|
|
|
parts[0].detach()
|
2017-01-31 10:43:11 +03:00
|
|
|
self.assertEqual(len(list(SystemPart.get_active(self.database))), 0)
|
|
|
|
|
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)
|
2017-06-06 18:00:15 +03:00
|
|
|
parts = list(SystemPart.get(self.database, conditions=u"table='othertable'"))
|
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))
|
|
|
|
self.assertEqual(len(parts), 1)
|
2017-02-09 14:49:25 +03:00
|
|
|
parts[0].detach()
|
2017-01-31 10:43:11 +03:00
|
|
|
self.assertEqual(len(list(SystemPart.get_active(self.database))), 0)
|
2017-02-09 14:49:25 +03:00
|
|
|
parts[0].attach()
|
2017-01-31 10:43:11 +03:00
|
|
|
self.assertEqual(len(list(SystemPart.get_active(self.database))), 1)
|
|
|
|
|
|
|
|
def test_drop(self):
|
|
|
|
parts = list(SystemPart.get_active(self.database))
|
2017-02-09 14:49:25 +03:00
|
|
|
parts[0].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())
|
2017-02-09 14:49:25 +03:00
|
|
|
parts[0].freeze()
|
2017-01-31 16:13:46 +03:00
|
|
|
backups = set(self._get_backups())
|
|
|
|
self.assertEqual(len(backups), len(prev_backups) + 1)
|
2017-01-31 10:43:11 +03:00
|
|
|
# Clean created backup
|
2017-01-31 16:13:46 +03:00
|
|
|
shutil.rmtree(self.BACKUP_DIR + '{0}'.format(list(backups - prev_backups)[0]))
|
2017-01-31 10:43:11 +03:00
|
|
|
|
|
|
|
def test_fetch(self):
|
|
|
|
# TODO Not tested, as I have no replication set
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TestTable(Model):
|
|
|
|
date_field = DateField()
|
|
|
|
|
|
|
|
engine = MergeTree('date_field', ('date_field',))
|