diff --git a/clickhouse_sqlalchemy/types/common.py b/clickhouse_sqlalchemy/types/common.py index 84cd78ff..947a8708 100644 --- a/clickhouse_sqlalchemy/types/common.py +++ b/clickhouse_sqlalchemy/types/common.py @@ -152,12 +152,13 @@ def __init__(self, precision=3, timezone=None): class Enum(types.Enum, ClickHouseTypeEngine): __visit_name__ = 'enum' + native_enum = True def __init__(self, *enums, **kw): if not enums: enums = kw.get('_enums', ()) # passed as keyword - super(Enum, self).__init__(*enums, **kw) + super(Enum, self).__init__(*enums, **kw, convert_unicode=False) class Enum8(Enum): diff --git a/tests/types/test_enum16.py b/tests/types/test_enum16.py new file mode 100644 index 00000000..a3dd61af --- /dev/null +++ b/tests/types/test_enum16.py @@ -0,0 +1,45 @@ +import enum + +from sqlalchemy import Column +from sqlalchemy.sql.ddl import CreateTable + +from clickhouse_sqlalchemy import types, engines, Table +from tests.testcase import BaseTestCase, CompilationTestCase +from tests.util import with_native_and_http_sessions + + +class TestEnum(enum.IntEnum): + First = 1 + Second = 2 + + +class Enum16CompilationTestCase(CompilationTestCase): + def test_create_table(self): + table = Table( + "test", + BaseTestCase.metadata(), + Column("x", types.Enum16(TestEnum)), + engines.Memory(), + ) + + self.assertEqual( + self.compile(CreateTable(table)), + "CREATE TABLE test (x Enum16('First' = 1, 'Second' = 2)) " + "ENGINE = Memory" + ) + + +@with_native_and_http_sessions +class Enum8TestCase(BaseTestCase): + table = Table( + 'test', BaseTestCase.metadata(), + Column('x', types.Enum16(TestEnum)), + engines.Memory() + ) + + def test_select_insert(self): + value = TestEnum.First + with self.create_table(self.table): + self.session.execute(self.table.insert(), [{'x': value}]) + self.assertEqual(self.session.query(self.table.c.x).scalar(), + value) diff --git a/tests/types/test_enum8.py b/tests/types/test_enum8.py new file mode 100644 index 00000000..9f2afc02 --- /dev/null +++ b/tests/types/test_enum8.py @@ -0,0 +1,45 @@ +import enum + +from sqlalchemy import Column +from sqlalchemy.sql.ddl import CreateTable + +from clickhouse_sqlalchemy import types, engines, Table +from tests.testcase import BaseTestCase, CompilationTestCase +from tests.util import with_native_and_http_sessions + + +class TestEnum(enum.IntEnum): + First = 1 + Second = 2 + + +class Enum8CompilationTestCase(CompilationTestCase): + def test_create_table(self): + table = Table( + "test", + BaseTestCase.metadata(), + Column("x", types.Enum8(TestEnum)), + engines.Memory(), + ) + + self.assertEqual( + self.compile(CreateTable(table)), + "CREATE TABLE test (x Enum8('First' = 1, 'Second' = 2)) " + "ENGINE = Memory" + ) + + +@with_native_and_http_sessions +class Enum8TestCase(BaseTestCase): + table = Table( + 'test', BaseTestCase.metadata(), + Column('x', types.Enum8(TestEnum)), + engines.Memory() + ) + + def test_select_insert(self): + value = TestEnum.First + with self.create_table(self.table): + self.session.execute(self.table.insert(), [{'x': value}]) + self.assertEqual(self.session.query(self.table.c.x).scalar(), + value)