-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow skipping utf8 converison in Python3 #548
Changes from 11 commits
9acc61f
888dbde
3938f5f
afb36fe
03e7f3a
1b8a8a4
c68165a
66a590e
08cd5d7
5bc6155
c9ed747
be3edb2
7f831e7
09816fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,9 @@ | |
|
||
import datetime | ||
import pytest | ||
import sys | ||
from pytest import fixture | ||
from decimal import Decimal | ||
|
||
@fixture(scope='module') | ||
def decimal_table(cur): | ||
|
@@ -49,48 +51,97 @@ def test_cursor_description_precision_scale(cur, decimal_table): | |
for (exp, obs) in zip(expected, observed): | ||
assert exp == obs | ||
|
||
|
||
@fixture(scope='module') | ||
def decimal_table2(cur): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was the existing decimal_table not enough for the tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found it more consistent with other tests this way, but it's really minor to change, if you insist. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ack |
||
table_name = 'tmp_decimal_table2' | ||
ddl = """CREATE TABLE {0} (val decimal(18, 9))""".format(table_name) | ||
cur.execute(ddl) | ||
cur.execute('''insert into {0} | ||
values (cast(123456789.123456789 as decimal(18, 9))), | ||
(cast(-123456789.123456789 as decimal(18, 9))), | ||
(cast(0.000000001 as decimal(18, 9))), | ||
(cast(-0.000000001 as decimal(18, 9))), | ||
(cast(999999999.999999999 as decimal(18, 9))), | ||
(cast(-999999999.999999999 as decimal(18, 9)))'''.format(table_name)) | ||
try: | ||
yield table_name | ||
finally: | ||
cur.execute("DROP TABLE {0}".format(table_name)) | ||
|
||
|
||
def common_test_decimal(cur, decimal_table): | ||
"""Read back a few decimal values in a wide range.""" | ||
cur.execute('select val from {0} order by val'.format(decimal_table)) | ||
results = cur.fetchall() | ||
assert results == [(Decimal('-999999999.999999999'),), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice to have: it would increase the coverage of the tests if there would be also NULL values in these test tables There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, added |
||
(Decimal('-123456789.123456789'),), | ||
(Decimal('-0.000000001'),), | ||
(Decimal('0.000000001'),), | ||
(Decimal('123456789.123456789'),), | ||
(Decimal('999999999.999999999'),)] | ||
|
||
|
||
@pytest.mark.connect | ||
def test_decimal_basic(cur, decimal_table2): | ||
common_test_decimal(cur, decimal_table2) | ||
|
||
|
||
@pytest.mark.connect | ||
def test_decimal_no_string_conv(cur_no_string_conv, decimal_table2): | ||
common_test_decimal(cur_no_string_conv, decimal_table2) | ||
|
||
|
||
@fixture(scope='module') | ||
def date_table(cur): | ||
table_name = 'tmp_date_table' | ||
ddl = """CREATE TABLE {0} (d date)""".format(table_name) | ||
cur.execute(ddl) | ||
cur.execute('''insert into {0} | ||
values (date "0001-01-01"), (date "1999-9-9")'''.format(table_name)) | ||
try: | ||
yield table_name | ||
finally: | ||
cur.execute("DROP TABLE {0}".format(table_name)) | ||
|
||
|
||
@pytest.mark.connect | ||
def test_date_basic(cur, date_table): | ||
"""Insert and read back a couple of data values in a wide range.""" | ||
cur.execute('''insert into {0} | ||
values (date "0001-01-01"), (date "1999-9-9")'''.format(date_table)) | ||
def common_test_date(cur, date_table): | ||
"""Read back a couple of data values in a wide range.""" | ||
cur.execute('select d from {0} order by d'.format(date_table)) | ||
results = cur.fetchall() | ||
assert results == [(datetime.date(1, 1, 1),), (datetime.date(1999, 9, 9),)] | ||
|
||
|
||
@pytest.mark.connect | ||
def test_date_basic(cur, date_table): | ||
common_test_date(cur, date_table) | ||
|
||
|
||
@pytest.mark.connect | ||
def test_date_no_string_conv(cur_no_string_conv, date_table): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add similar test for decimals? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
common_test_date(cur_no_string_conv, date_table) | ||
|
||
|
||
@fixture(scope='module') | ||
def timestamp_table(cur): | ||
table_name = 'tmp_timestamp_table' | ||
ddl = """CREATE TABLE {0} (ts timestamp)""".format(table_name) | ||
cur.execute(ddl) | ||
try: | ||
yield table_name | ||
finally: | ||
cur.execute("DROP TABLE {0}".format(table_name)) | ||
|
||
|
||
@pytest.mark.connect | ||
def test_timestamp_basic(cur, timestamp_table): | ||
"""Insert and read back a few timestamp values in a wide range.""" | ||
cur.execute('''insert into {0} | ||
values (cast("1400-01-01 00:00:00" as timestamp)), | ||
(cast("2014-06-23 13:30:51" as timestamp)), | ||
(cast("2014-06-23 13:30:51.123" as timestamp)), | ||
(cast("2014-06-23 13:30:51.123456" as timestamp)), | ||
(cast("2014-06-23 13:30:51.123456789" as timestamp)), | ||
(cast("9999-12-31 23:59:59" as timestamp))'''.format(timestamp_table)) | ||
(cast("9999-12-31 23:59:59" as timestamp))'''.format(table_name)) | ||
try: | ||
yield table_name | ||
finally: | ||
cur.execute("DROP TABLE {0}".format(table_name)) | ||
|
||
|
||
def common_test_timestamp(cur, timestamp_table): | ||
"""Read back a few timestamp values in a wide range.""" | ||
cur.execute('select ts from {0} order by ts'.format(timestamp_table)) | ||
results = cur.fetchall() | ||
assert results == [(datetime.datetime(1400, 1, 1, 0, 0),), | ||
|
@@ -101,6 +152,16 @@ def test_timestamp_basic(cur, timestamp_table): | |
(datetime.datetime(9999, 12, 31, 23, 59, 59),)] | ||
|
||
|
||
@pytest.mark.connect | ||
def test_timestamp_basic(cur, timestamp_table): | ||
common_test_timestamp(cur, timestamp_table) | ||
|
||
|
||
@pytest.mark.connect | ||
def test_timestamp_no_string_conv(cur_no_string_conv, timestamp_table): | ||
common_test_timestamp(cur_no_string_conv, timestamp_table) | ||
|
||
|
||
@pytest.mark.connect | ||
def test_utf8_strings(cur): | ||
"""Use STRING/VARCHAR/CHAR values with multi byte unicode code points in a query.""" | ||
|
@@ -120,3 +181,23 @@ def test_utf8_strings(cur): | |
result = cur.fetchone()[0] | ||
assert result == b"\xaa" | ||
assert result.decode("UTF-8", "replace") == u"�" | ||
|
||
|
||
@pytest.mark.connect | ||
def test_string_conv(cur): | ||
cur.execute('select "Test string"') | ||
result = cur.fetchone() | ||
assert result[0] == u"Test string" | ||
|
||
|
||
@pytest.mark.connect | ||
def test_string_no_string_conv(cur_no_string_conv): | ||
cur = cur_no_string_conv | ||
cur.execute('select "Test string"') | ||
result = cur.fetchone() | ||
|
||
|
||
if sys.version_info[0] < 3: | ||
assert result[0] == u"Test string" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this fail if 'u' was not added? Python does automatic conversion of strings in many cases. We should check the exact type with isinstance. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
else: | ||
assert result[0] == b"Test string" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here and in test_data_types.py: is there a reason for removing the final new lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's by mistake, fixed