2017-08-16 23:48:18 +03:00
|
|
|
from __future__ import unicode_literals
|
2016-08-31 15:26:28 +03:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from infi.clickhouse_orm.database import Database
|
|
|
|
from infi.clickhouse_orm.models import Model
|
|
|
|
from infi.clickhouse_orm.fields import *
|
|
|
|
from infi.clickhouse_orm.engines import *
|
|
|
|
|
|
|
|
try:
|
|
|
|
Enum # exists in Python 3.4+
|
|
|
|
except NameError:
|
|
|
|
from enum import Enum # use the enum34 library instead
|
|
|
|
|
|
|
|
|
|
|
|
class EnumFieldsTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2019-06-13 08:12:56 +03:00
|
|
|
self.database = Database('test-db', log_statements=True)
|
2016-08-31 15:26:28 +03:00
|
|
|
self.database.create_table(ModelWithEnum)
|
2016-09-01 15:25:48 +03:00
|
|
|
self.database.create_table(ModelWithEnumArray)
|
2016-08-31 15:26:28 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.database.drop_database()
|
|
|
|
|
|
|
|
def test_insert_and_select(self):
|
|
|
|
self.database.insert([
|
|
|
|
ModelWithEnum(date_field='2016-08-30', enum_field=Fruit.apple),
|
|
|
|
ModelWithEnum(date_field='2016-08-31', enum_field=Fruit.orange)
|
|
|
|
])
|
|
|
|
query = 'SELECT * from $table ORDER BY date_field'
|
|
|
|
results = list(self.database.select(query, ModelWithEnum))
|
2018-08-19 18:22:22 +03:00
|
|
|
self.assertEqual(len(results), 2)
|
|
|
|
self.assertEqual(results[0].enum_field, Fruit.apple)
|
|
|
|
self.assertEqual(results[1].enum_field, Fruit.orange)
|
2016-08-31 15:26:28 +03:00
|
|
|
|
|
|
|
def test_ad_hoc_model(self):
|
|
|
|
self.database.insert([
|
|
|
|
ModelWithEnum(date_field='2016-08-30', enum_field=Fruit.apple),
|
|
|
|
ModelWithEnum(date_field='2016-08-31', enum_field=Fruit.orange)
|
|
|
|
])
|
|
|
|
query = 'SELECT * from $db.modelwithenum ORDER BY date_field'
|
|
|
|
results = list(self.database.select(query))
|
2018-08-19 18:22:22 +03:00
|
|
|
self.assertEqual(len(results), 2)
|
|
|
|
self.assertEqual(results[0].enum_field.name, Fruit.apple.name)
|
|
|
|
self.assertEqual(results[0].enum_field.value, Fruit.apple.value)
|
|
|
|
self.assertEqual(results[1].enum_field.name, Fruit.orange.name)
|
|
|
|
self.assertEqual(results[1].enum_field.value, Fruit.orange.value)
|
2016-08-31 15:26:28 +03:00
|
|
|
|
|
|
|
def test_conversion(self):
|
2018-08-19 18:22:22 +03:00
|
|
|
self.assertEqual(ModelWithEnum(enum_field=3).enum_field, Fruit.orange)
|
|
|
|
self.assertEqual(ModelWithEnum(enum_field='apple').enum_field, Fruit.apple)
|
|
|
|
self.assertEqual(ModelWithEnum(enum_field=Fruit.banana).enum_field, Fruit.banana)
|
2016-08-31 15:26:28 +03:00
|
|
|
|
|
|
|
def test_assignment_error(self):
|
|
|
|
for value in (0, 17, 'pear', '', None, 99.9):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
ModelWithEnum(enum_field=value)
|
|
|
|
|
|
|
|
def test_default_value(self):
|
|
|
|
instance = ModelWithEnum()
|
2018-08-19 18:22:22 +03:00
|
|
|
self.assertEqual(instance.enum_field, Fruit.apple)
|
2016-08-31 15:26:28 +03:00
|
|
|
|
2016-09-01 15:25:48 +03:00
|
|
|
def test_enum_array(self):
|
|
|
|
instance = ModelWithEnumArray(date_field='2016-08-30', enum_array=[Fruit.apple, Fruit.apple, Fruit.orange])
|
|
|
|
self.database.insert([instance])
|
|
|
|
query = 'SELECT * from $table ORDER BY date_field'
|
|
|
|
results = list(self.database.select(query, ModelWithEnumArray))
|
2018-08-19 18:22:22 +03:00
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(results[0].enum_array, instance.enum_array)
|
2016-09-01 15:25:48 +03:00
|
|
|
|
2016-08-31 15:26:28 +03:00
|
|
|
|
|
|
|
Fruit = Enum('Fruit', u'apple banana orange')
|
|
|
|
|
|
|
|
|
|
|
|
class ModelWithEnum(Model):
|
|
|
|
|
|
|
|
date_field = DateField()
|
|
|
|
enum_field = Enum8Field(Fruit)
|
|
|
|
|
|
|
|
engine = MergeTree('date_field', ('date_field',))
|
|
|
|
|
2016-09-01 15:25:48 +03:00
|
|
|
|
|
|
|
class ModelWithEnumArray(Model):
|
|
|
|
|
|
|
|
date_field = DateField()
|
|
|
|
enum_array = ArrayField(Enum16Field(Fruit))
|
|
|
|
|
|
|
|
engine = MergeTree('date_field', ('date_field',))
|