Skip to content

Commit

Permalink
all the strings are Unicode in Python3 (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-detiste authored Aug 10, 2024
1 parent a9800cc commit b7ef0a1
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 82 deletions.
4 changes: 2 additions & 2 deletions vertica_python/tests/integration_tests/test_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@

class ColumnTestCase(VerticaPythonIntegrationTestCase):
def test_column_names_query(self):
columns = ['isocode', 'name', u'\uFF04']
columns = ['isocode', 'name', '\uFF04']

with self._connect() as conn:
cur = conn.cursor()
cur.execute(u"""
cur.execute("""
SELECT 'US' AS {0}, 'United States' AS {1}, 'USD' AS {2}
UNION ALL SELECT 'CA', 'Canada', 'CAD'
UNION ALL SELECT 'MX', 'Mexico', 'MXN' """.format(*columns))
Expand Down
50 changes: 25 additions & 25 deletions vertica_python/tests/integration_tests/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,16 +776,16 @@ def test_qmark_paramstyle(self):
def test_execute_parameters(self):
with self._connect() as conn:
cur = conn.cursor()
all_chars = u"".join(chr(i) for i in range(1, 128))
backslash_data = u"\\backslash\\ \\data\\\\"
all_chars = "".join(chr(i) for i in range(1, 128))
backslash_data = "\\backslash\\ \\data\\\\"
cur.execute("SELECT :a, :b", parameters={"a": all_chars, "b": backslash_data})
self.assertEqual([all_chars, backslash_data], cur.fetchone())

def test_execute_percent_parameters(self):
with self._connect() as conn:
cur = conn.cursor()
all_chars = u"".join(chr(i) for i in range(1, 128))
backslash_data = u"\\backslash\\ \\data\\\\"
all_chars = "".join(chr(i) for i in range(1, 128))
backslash_data = "\\backslash\\ \\data\\\\"
cur.execute("SELECT %s, %s", parameters=[all_chars, backslash_data])
self.assertEqual([all_chars, backslash_data], cur.fetchone())

Expand Down Expand Up @@ -898,16 +898,16 @@ def test_copy_local_stdin_multistat(self):

# Check rejected files
with open(rej1, 'r', encoding='utf-8') as f:
self.assertEqual(f.read(), u'x\u00f1,bla\n')
self.assertEqual(f.read(), 'x\u00f1,bla\n')
with open(except1, 'r', encoding='utf-8') as f:
content = f.read()
self.assertTrue(u"Invalid integer format 'x\u00f1' for column 1 (a)" in content)
self.assertTrue("Invalid integer format 'x\u00f1' for column 1 (a)" in content)
with open(rej2, 'r', encoding='utf-8') as f:
self.assertEqual(f.read(), u'10,kkkkkkkkkkkk\nxx,corge\n')
self.assertEqual(f.read(), '10,kkkkkkkkkkkk\nxx,corge\n')
with open(except2, 'r', encoding='utf-8') as f:
content = f.read()
self.assertTrue(u"The 12-byte value is too long for type Varchar(9), column 2 (b)" in content)
self.assertTrue(u"Invalid integer format 'xx' for column 1 (a)" in content)
self.assertTrue("The 12-byte value is too long for type Varchar(9), column 2 (b)" in content)
self.assertTrue("Invalid integer format 'xx' for column 1 (a)" in content)

# Delete data files
try:
Expand Down Expand Up @@ -1004,18 +1004,18 @@ def test_copy_local_file_multistat(self, fetch_results):

# Check rejected files
with open(rej1, 'r', encoding='utf-8') as f:
self.assertEqual(f.read(), u'x\u00f1,bla\n5,aaaaaaaaaa\n')
self.assertEqual(f.read(), 'x\u00f1,bla\n5,aaaaaaaaaa\n')
with open(except1, 'r', encoding='utf-8') as f:
content = f.read()
self.assertTrue(u"Invalid integer format 'x\u00f1' for column 1 (a)" in content)
self.assertTrue(u"The 10-byte value is too long for type Varchar(9), column 2 (b)" in content)
self.assertTrue("Invalid integer format 'x\u00f1' for column 1 (a)" in content)
self.assertTrue("The 10-byte value is too long for type Varchar(9), column 2 (b)" in content)
with open(rej2, 'r', encoding='utf-8') as f:
self.assertEqual(f.read(), u'10,kkkkkkkkkkkk\nxx,corge\nf,quux\n')
self.assertEqual(f.read(), '10,kkkkkkkkkkkk\nxx,corge\nf,quux\n')
with open(except2, 'r', encoding='utf-8') as f:
content = f.read()
self.assertTrue(u"The 12-byte value is too long for type Varchar(9), column 2 (b)" in content)
self.assertTrue(u"Invalid integer format 'xx' for column 1 (a)" in content)
self.assertTrue(u"Invalid integer format 'f' for column 1 (a)" in content)
self.assertTrue("The 12-byte value is too long for type Varchar(9), column 2 (b)" in content)
self.assertTrue("Invalid integer format 'xx' for column 1 (a)" in content)
self.assertTrue("Invalid integer format 'f' for column 1 (a)" in content)

# Delete files
try:
Expand Down Expand Up @@ -1059,7 +1059,7 @@ def test_copy_local_rejected_as_table(self):
cur.execute("SELECT rejected_data, rejected_reason FROM test_loader_rejects ORDER BY row_number ASC")
self.assertListOfListsEqual(cur.fetchall(),
[['5,aaaaaaaaaa', 'The 10-byte value is too long for type Varchar(9), column 2 (b)'],
[u'x\u00f1,bla', u"Invalid integer format 'x\u00f1' for column 1 (a)"]])
['x\u00f1,bla', "Invalid integer format 'x\u00f1' for column 1 (a)"]])

cur.execute("SELECT * FROM {0} ORDER BY a ASC".format(self._table))
self.assertListOfListsEqual(cur.fetchall(), [[None, 'baz'], [1, 'foo'], [2, 'bar'], [4, None]])
Expand Down Expand Up @@ -1141,7 +1141,7 @@ def test_executemany_quoted_path(self):
self._test_executemany(table, [(1, 'aa'), (2, 'bb')])

def test_executemany_utf8(self):
self._test_executemany(self._table, [(1, u'a\xfc'), (2, u'bb')])
self._test_executemany(self._table, [(1, 'a\xfc'), (2, 'bb')])

# test for #292
def test_executemany_autocommit(self):
Expand Down Expand Up @@ -1283,8 +1283,8 @@ def test_named_paramstyle(self):
self.assertListOfListsEqual(res, [[1, 'varchar']])

def test_executemany(self):
values = ((None, 'foo'), [1, 'aa'], (2, None), [2, u'a\xfc'])
expected = [[None, 'foo'], [1, 'aa'], [2, None], [2, u'a\xfc']]
values = ((None, 'foo'), [1, 'aa'], (2, None), [2, 'a\xfc'])
expected = [[None, 'foo'], [1, 'aa'], [2, None], [2, 'a\xfc']]
with self._connect() as conn:
cur = conn.cursor()
cur.execute("CREATE TABLE {} (a int, b varchar)".format(self._table))
Expand Down Expand Up @@ -1312,7 +1312,7 @@ def test_executemany(self):
self.assertIsNone(cur.fetchone())
self.assertTrue(cur.nextset())

self.assertListOfListsEqual(cur.fetchall(), [[2, u'a\xfc'], [2, None]])
self.assertListOfListsEqual(cur.fetchall(), [[2, 'a\xfc'], [2, None]])
self.assertIsNone(cur.fetchone())
self.assertTrue(cur.nextset())

Expand Down Expand Up @@ -1353,8 +1353,8 @@ def test_bind_datetime(self):
self.assertListOfListsEqual(res, [values])

def test_bind_binary(self):
values = [b'binary data', b'\\backslash data\\', u'\\backslash data\\',
u'\u00f1 encoding', 'raw data', 'long varbinary data', None]
values = [b'binary data', b'\\backslash data\\', '\\backslash data\\',
'\u00f1 encoding', 'raw data', 'long varbinary data', None]
expected = [[b'binary data\x00\x00\x00', b'\\backslash data\\',
b'\\backslash data\\', b'\xc3\xb1 encoding',
b'raw data', b'long varbinary data', None]]
Expand Down Expand Up @@ -1398,9 +1398,9 @@ def test_bind_numeric(self):

def test_bind_character(self):
values = ['char data', b'raw varchar data',
u'long varbinary data \u00f1', None, None, None]
'long varbinary data \u00f1', None, None, None]
expected = [['char data ', 'raw varchar data',
u'long varbinary data \u00f1', None, None, None]]
'long varbinary data \u00f1', None, None, None]]
with self._connect() as conn:
cur = conn.cursor()
cur.execute("""CREATE TABLE {} (
Expand Down
26 changes: 13 additions & 13 deletions vertica_python/tests/integration_tests/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ def test_Array_numeric_type(self):
None, [], [None]], test_executemany=True)

def test_Array_char_type(self):
self._test_insert_complex_type('ARRAY[CHAR(3)]', [['a', u'\u16b1', None, 'foo'], None, [], [None]], [['a ', u'\u16b1', None, 'foo'], None, [], [None]], test_executemany=True)
self._test_insert_complex_type('ARRAY[CHAR(3)]', [['a', '\u16b1', None, 'foo'], None, [], [None]], [['a ', '\u16b1', None, 'foo'], None, [], [None]], test_executemany=True)

def test_Array_varchar_type(self):
self._test_insert_complex_type('ARRAY[VARCHAR(10)]', [['', u'\u16b1\nb', None, 'foo'], None, [], [None]], test_executemany=True)
self._test_insert_complex_type('ARRAY[VARCHAR(10)]', [['', '\u16b1\nb', None, 'foo'], None, [], [None]], test_executemany=True)
self._test_insert_complex_type('ARRAY[VARCHAR]', [[chr(i)] for i in range(1, 128)], test_executemany=True)

def test_Array_date_type(self):
Expand Down Expand Up @@ -178,10 +178,10 @@ def test_1DSet_numeric_type(self):
None, set(), {None}], test_executemany=True)

def test_1DSet_char_type(self):
self._test_insert_complex_type('SET[CHAR(3)]', [{'a ', u'\u16b1', None, 'foo'}, None, set(), {None}], test_executemany=True)
self._test_insert_complex_type('SET[CHAR(3)]', [{'a ', '\u16b1', None, 'foo'}, None, set(), {None}], test_executemany=True)

def test_1DSet_varchar_type(self):
self._test_insert_complex_type('SET[VARCHAR(10)]', [{'', u'\u16b1\nb', None, 'foo'}, None, set(), {None}], test_executemany=True)
self._test_insert_complex_type('SET[VARCHAR(10)]', [{'', '\u16b1\nb', None, 'foo'}, None, set(), {None}], test_executemany=True)
self._test_insert_complex_type('SET[VARCHAR]', [{chr(i)} for i in range(1, 128)], test_executemany=True)

def test_1DSet_date_type(self):
Expand Down Expand Up @@ -339,16 +339,16 @@ def test_1DArray_numeric_type(self):
self.assertEqual(res[2], None)

def test_1DArray_char_type(self):
query = u"SELECT ARRAY['a', '\u16b1b', null, 'foo']::ARRAY[CHAR(3)], ARRAY[]::ARRAY[CHAR(4)], null::ARRAY[CHAR(5)]"
query = "SELECT ARRAY['a', '\u16b1b', null, 'foo']::ARRAY[CHAR(3)], ARRAY[]::ARRAY[CHAR(4)], null::ARRAY[CHAR(5)]"
res = self._query_and_fetchone(query)
self.assertEqual(res[0], ['a ', u'\u16b1', None, 'foo'])
self.assertEqual(res[0], ['a ', '\u16b1', None, 'foo'])
self.assertEqual(res[1], [])
self.assertEqual(res[2], None)

def test_1DArray_varchar_type(self):
query = u"SELECT ARRAY['', '\u16b1\nb', null, 'foo']::ARRAY[VARCHAR(10),4], ARRAY[]::ARRAY[VARCHAR(4)], null::ARRAY[VARCHAR]"
query = "SELECT ARRAY['', '\u16b1\nb', null, 'foo']::ARRAY[VARCHAR(10),4], ARRAY[]::ARRAY[VARCHAR(4)], null::ARRAY[VARCHAR]"
res = self._query_and_fetchone(query)
self.assertEqual(res[0], ['', u'\u16b1\nb', None, 'foo'])
self.assertEqual(res[0], ['', '\u16b1\nb', None, 'foo'])
self.assertEqual(res[1], [])
self.assertEqual(res[2], None)

Expand Down Expand Up @@ -564,16 +564,16 @@ def test_1DSet_numeric_type(self):
self.assertEqual(res[2], None)

def test_1DSet_char_type(self):
query = u"SELECT SET['a', '\u16b1b', null, 'foo']::SET[CHAR(3)], SET[]::SET[CHAR(4)], null::SET[CHAR(5)]"
query = "SELECT SET['a', '\u16b1b', null, 'foo']::SET[CHAR(3)], SET[]::SET[CHAR(4)], null::SET[CHAR(5)]"
res = self._query_and_fetchone(query)
self.assertEqual(res[0], {'a ', u'\u16b1', None, 'foo'})
self.assertEqual(res[0], {'a ', '\u16b1', None, 'foo'})
self.assertEqual(res[1], set())
self.assertEqual(res[2], None)

def test_1DSet_varchar_type(self):
query = u"SELECT SET['', '\u16b1\nb', null, 'foo']::SET[VARCHAR(10),4], SET[]::SET[VARCHAR(4)], null::SET[VARCHAR]"
query = "SELECT SET['', '\u16b1\nb', null, 'foo']::SET[VARCHAR(10),4], SET[]::SET[VARCHAR(4)], null::SET[VARCHAR]"
res = self._query_and_fetchone(query)
self.assertEqual(res[0], {'', u'\u16b1\nb', None, 'foo'})
self.assertEqual(res[0], {'', '\u16b1\nb', None, 'foo'})
self.assertEqual(res[1], set())
self.assertEqual(res[2], None)

Expand All @@ -594,7 +594,7 @@ def test_1DSet_time_type(self):
def test_1DSet_timetz_type(self):
query = "SELECT SET['22:36:33.12345+0630', null, '800-02-03 22:36:33.123456 America/Cayman']::SET[TIMETZ(3)], SET[]::SET[TIMETZ(4)], null::SET[TIMETZ]"
res = self._query_and_fetchone(query)
self.assertEqual(res[0], {time(22, 36, 33, 123000, tzinfo=tzoffset(None, 23400)), None,
self.assertEqual(res[0], {time(22, 36, 33, 123000, tzinfo=tzoffset(None, 23400)), None,
time(22, 36, 33, 123000, tzinfo=tzoffset(None, -19176))})
self.assertEqual(res[1], set())
self.assertEqual(res[2], None)
Expand Down
14 changes: 7 additions & 7 deletions vertica_python/tests/integration_tests/test_transfer_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def tearDownClass(cls):

def _test_equal_value(self, sql_type, data_list, assert_almost_equal=False):
for data in data_list:
query = u"SELECT {}{}".format(data, "::" + sql_type if sql_type else '')
query = "SELECT {}{}".format(data, "::" + sql_type if sql_type else '')
self.text_cursor.execute(query)
self.binary_cursor.execute(query)
text_val = self.text_cursor.fetchone()[0]
Expand Down Expand Up @@ -67,9 +67,9 @@ def test_numeric_type(self):
self._test_equal_value("DECIMAL", ["123456789.98765"])

def test_char_type(self):
self._test_equal_value("CHAR(8)", [u"'\u16b1'"])
self._test_equal_value("VARCHAR", [u"'foo\u16b1'"])
self._test_equal_value("LONG VARCHAR", [u"'foo \u16b1 bar'"])
self._test_equal_value("CHAR(8)", ["'\u16b1'"])
self._test_equal_value("VARCHAR", ["'foo\u16b1'"])
self._test_equal_value("LONG VARCHAR", ["'foo \u16b1 bar'"])

def test_datetime_type(self):
self._test_equal_value("DATE", ["'0340-01-20'", "'2001-12-01'", "'9999-12-31'"])
Expand Down Expand Up @@ -98,9 +98,9 @@ def test_UUID_type(self):
self._test_equal_value("UUID", ["'00010203-0405-0607-0809-0a0b0c0d0e0f'", "'123e4567-e89b-12d3-a456-426655440a00'"])

def test_binary_type(self):
self._test_equal_value("BINARY(2)", [u"'\303\261'"])
self._test_equal_value("VARBINARY", [u"'\303\261'"])
self._test_equal_value("LONG VARBINARY", [u"'\303\261\303\260'"])
self._test_equal_value("BINARY(2)", ["'\303\261'"])
self._test_equal_value("VARBINARY", ["'\303\261'"])
self._test_equal_value("LONG VARBINARY", ["'\303\261\303\260'"])

def test_array_type(self):
self._test_equal_value("ARRAY[INT]", ["ARRAY[1,2,3]"])
Expand Down
32 changes: 16 additions & 16 deletions vertica_python/tests/integration_tests/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@

class UnicodeTestCase(VerticaPythonIntegrationTestCase):
def test_unicode_query(self):
value = u'\u16a0'
query = u"SELECT '{0}'".format(value)
value = '\u16a0'
query = "SELECT '{0}'".format(value)

with self._connect() as conn:
cur = conn.cursor()
Expand All @@ -51,8 +51,8 @@ def test_unicode_query(self):
self.assertResultEqual(value, res[0])

def test_unicode_list_parameter(self):
values = [u'\u00f1', 'foo', 3]
query = u"SELECT {0}".format(", ".join(["%s"] * len(values)))
values = ['\u00f1', 'foo', 3]
query = "SELECT {0}".format(", ".join(["%s"] * len(values)))

with self._connect() as conn:
cur = conn.cursor()
Expand All @@ -63,10 +63,10 @@ def test_unicode_list_parameter(self):
self.assertResultEqual(val, res)

def test_unicode_named_parameter_binding(self):
values = [u'\u16b1', 'foo', 3]
keys = [u'\u16a0', 'foo', 3]
values = ['\u16b1', 'foo', 3]
keys = ['\u16a0', 'foo', 3]

query = u"SELECT {0}".format(", ".join([u":{0}".format(key) for key in keys]))
query = "SELECT {0}".format(", ".join([":{0}".format(key) for key in keys]))

with self._connect() as conn:
cur = conn.cursor()
Expand All @@ -77,8 +77,8 @@ def test_unicode_named_parameter_binding(self):
self.assertResultEqual(val, res)

def test_string_query(self):
value = u'test'
query = u"SELECT '{0}'".format(value)
value = 'test'
query = "SELECT '{0}'".format(value)

with self._connect() as conn:
cur = conn.cursor()
Expand All @@ -88,9 +88,9 @@ def test_string_query(self):
self.assertEqual(value, res[0])

def test_string_named_parameter_binding(self):
key = u'test'
value = u'value'
query = u"SELECT :{0}".format(key)
key = 'test'
value = 'value'
query = "SELECT :{0}".format(key)

with self._connect() as conn:
cur = conn.cursor()
Expand All @@ -101,9 +101,9 @@ def test_string_named_parameter_binding(self):

# unit test for issue #160
def test_null_named_parameter_binding(self):
key = u'test'
key = 'test'
value = None
query = u"SELECT :{0}".format(key)
query = "SELECT :{0}".format(key)

with self._connect() as conn:
cur = conn.cursor()
Expand All @@ -114,8 +114,8 @@ def test_null_named_parameter_binding(self):

# unit test for issue #160
def test_null_list_parameter(self):
values = [u'\u00f1', 'foo', None]
query = u"SELECT {0}".format(", ".join(["%s"] * len(values)))
values = ['\u00f1', 'foo', None]
query = "SELECT {0}".format(", ".join(["%s"] * len(values)))

with self._connect() as conn:
cur = conn.cursor()
Expand Down
6 changes: 3 additions & 3 deletions vertica_python/tests/unit_tests/test_sql_literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ def test_default_adapters(self):
self.assertEqual(cursor.object_to_sql_literal(datetime.date(2018, 9, 7)), "'2018-09-07'")
self.assertEqual(cursor.object_to_sql_literal(datetime.time(13, 50, 9)), "'13:50:09'")
# String
self.assertEqual(cursor.object_to_sql_literal(u"string'1"), "'string''1'")
self.assertEqual(cursor.object_to_sql_literal("string'1"), "'string''1'")
self.assertEqual(cursor.object_to_sql_literal(b"string'1"), "'string''1'")
# Tuple and namedtuple
self.assertEqual(cursor.object_to_sql_literal(
(123, u"string'1", None)), "(123,'string''1',NULL)")
(123, "string'1", None)), "(123,'string''1',NULL)")
self.assertEqual(cursor.object_to_sql_literal(
((1, u"a"), (2, u"b"), (3, u"c"))), "((1,'a'),(2,'b'),(3,'c'))")
((1, "a"), (2, "b"), (3, "c"))), "((1,'a'),(2,'b'),(3,'c'))")
Point = namedtuple('Point', ['x', 'y', 'z'])
p = Point(x=11, y=22, z=33)
self.assertEqual(cursor.object_to_sql_literal(p), "(11,22,33)")
Expand Down
Loading

0 comments on commit b7ef0a1

Please sign in to comment.