-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'xzkostyan:master' into master
- Loading branch information
Showing
8 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import json | ||
from sqlalchemy import Column, text, inspect, func | ||
from sqlalchemy.sql.ddl import CreateTable | ||
|
||
from clickhouse_sqlalchemy import types, engines, Table | ||
from tests.testcase import BaseTestCase, CompilationTestCase | ||
from tests.util import class_name_func | ||
from parameterized import parameterized_class | ||
from tests.session import native_session | ||
|
||
|
||
class JSONCompilationTestCase(CompilationTestCase): | ||
def test_create_table(self): | ||
table = Table( | ||
'test', CompilationTestCase.metadata(), | ||
Column('x', types.JSON), | ||
engines.Memory() | ||
) | ||
|
||
self.assertEqual( | ||
self.compile(CreateTable(table)), | ||
'CREATE TABLE test (x JSON) ENGINE = Memory' | ||
) | ||
|
||
|
||
@parameterized_class( | ||
[{'session': native_session}], | ||
class_name_func=class_name_func | ||
) | ||
class JSONTestCase(BaseTestCase): | ||
required_server_version = (22, 3, 2) | ||
|
||
table = Table( | ||
'test', BaseTestCase.metadata(), | ||
Column('x', types.JSON), | ||
engines.Memory() | ||
) | ||
|
||
def test_select_insert(self): | ||
data = {'k1': 1, 'k2': '2', 'k3': True} | ||
|
||
self.table.drop(bind=self.session.bind, if_exists=True) | ||
try: | ||
# http session is unsupport | ||
self.session.execute( | ||
text('SET allow_experimental_object_type = 1;') | ||
) | ||
self.session.execute(text(self.compile(CreateTable(self.table)))) | ||
self.session.execute(self.table.insert(), [{'x': data}]) | ||
coltype = inspect(self.session.bind).get_columns('test')[0]['type'] | ||
self.assertIsInstance(coltype, types.JSON) | ||
# https://clickhouse.com/docs/en/sql-reference/functions/json-functions#tojsonstring | ||
# The json type returns a tuple of values by default, | ||
# which needs to be converted to json using the | ||
# toJSONString function. | ||
res = self.session.query( | ||
func.toJSONString(self.table.c.x) | ||
).scalar() | ||
self.assertEqual(json.loads(res), data) | ||
finally: | ||
self.table.drop(bind=self.session.bind, if_exists=True) |