Skip to content

Commit

Permalink
Updated type integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lpoulain committed Aug 19, 2022
1 parent 111131c commit fe93827
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
28 changes: 27 additions & 1 deletion tests/integration/test_types_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,27 @@ def test_char(trino_connection):
def test_varbinary(trino_connection):
SqlTest(trino_connection) \
.add_field(sql="X'65683F'", python='ZWg/') \
.add_field(sql="X'0001020304050607080DF9367AA7000000'", python='AAECAwQFBgcIDfk2eqcAAAA=') \
.add_field(sql="X''", python='') \
.add_field(sql="CAST('' AS VARBINARY)", python='') \
.add_field(sql="from_utf8(CAST('😂😂😂😂😂😂' AS VARBINARY))", python='😂😂😂😂😂😂') \
.add_field(sql="CAST(null AS VARBINARY)", python=None) \
.execute()


def test_varbinary_failure(trino_connection):
SqlExpectFailureTest(trino_connection) \
.execute("CAST(42 AS VARBINARY)")


def test_json(trino_connection):
SqlTest(trino_connection) \
.add_field(sql="CAST('{}' AS JSON)", python='"{}"') \
.add_field(sql="CAST('null' AS JSON)", python='"null"') \
.add_field(sql="CAST(null AS JSON)", python=None) \
.add_field(sql="CAST('3.14' AS JSON)", python='"3.14"') \
.add_field(sql="CAST('a string' AS JSON)", python='"a string"') \
.add_field(sql="CAST('a \" complex '' string :' AS JSON)", python='"a \\" complex \' string :"') \
.add_field(sql="CAST('[]' AS JSON)", python='"[]"') \
.execute()


Expand Down Expand Up @@ -219,3 +228,20 @@ def _compare_results(self, actual, expected):
continue

assert actual_val == expected_val


class SqlExpectFailureTest:
def __init__(self, trino_connection):
self.cur = trino_connection.cursor(experimental_python_types=True)

def execute(self, field):
sql = 'SELECT ' + field

try:
self.cur.execute(sql)
self.cur.fetchall()
success = True
except Exception:
success = False

assert not success, "Test not expected to succeed"
3 changes: 2 additions & 1 deletion trino/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ def decorated(*args, **kwargs):
class NoOpRowMapper:
"""
No-op RowMapper which does not perform any transformation
Used when experimental_python_types is False.
"""

def map(self, rows):
Expand Down Expand Up @@ -836,7 +837,7 @@ def _double_map_func(self):
else float(val)

def _timestamp_map_func(self, column, col_type):
datetime_default_size = 20
datetime_default_size = 20 # size of 'YYYY-MM-DD HH:MM:SS.' (the datetime string up to the milliseconds)
pattern = "%Y-%m-%d %H:%M:%S"
ms_size, ms_to_trim = self._get_number_of_digits(column)
if ms_size > 0:
Expand Down

0 comments on commit fe93827

Please sign in to comment.