Skip to content

Commit ad9626e

Browse files
zimingdxschildw
authored andcommitted
Row and PartialRow now always cast values to string (SYNPY-615) (#497)
* Row and PartialRow now always cast values to string * newline at end of file * fixed tests
1 parent 41764f7 commit ad9626e

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

synapseclient/table.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ class Row(DictObject):
856856
"""
857857
def __init__(self, values, rowId=None, versionNumber=None, etag=None):
858858
super(Row, self).__init__()
859-
self.values = values
859+
self.values = [str(value) for value in values]
860860
if rowId is not None:
861861
self.rowId = rowId
862862
if versionNumber is not None:
@@ -906,8 +906,8 @@ def __init__(self, values, rowId, etag=None, nameToColumnId=None):
906906

907907
rowId = int(rowId)
908908

909-
self.values = [{'key': nameToColumnId[x_key] if nameToColumnId is not None else x_key,
910-
'value': x_value} for x_key, x_value in six.iteritems(values)]
909+
self.values = [{'key': str(nameToColumnId[x_key]) if nameToColumnId is not None else str(x_key),
910+
'value': str(x_value)} for x_key, x_value in six.iteritems(values)]
911911
self.rowId = rowId
912912
if etag is not None:
913913
self.etag = etag

tests/unit/unit_test_tables.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
import math
1010
import os
1111
import sys
12+
import six
1213
import tempfile
1314
from builtins import zip
1415
from mock import MagicMock
15-
from nose.tools import assert_raises, assert_equals, assert_not_equals, raises, assert_false, assert_not_in, assert_sequence_equal
16+
from nose.tools import assert_raises, assert_equals, assert_not_equals, raises, assert_false, assert_not_in, assert_sequence_equal, assert_in
1617
from nose import SkipTest
1718
import unit
1819

@@ -22,7 +23,7 @@
2223
except ImportError:
2324
pandas_found = True
2425

25-
from nose.tools import raises, assert_equals, assert_set_equal
26+
from nose.tools import raises, assert_equals, assert_is_instance
2627
import unit
2728
import synapseclient
2829
from synapseclient import Entity
@@ -313,13 +314,15 @@ def test_csv_table():
313314
# print(table_row, expected_row)
314315
assert table_row==expected_row
315316

317+
expected_rows = [[str(val) for val in row] for row in data]
318+
316319
## test asRowSet
317320
rowset = table.asRowSet()
318-
for rowset_row, expected_row in zip(rowset.rows, data):
321+
for rowset_row, expected_row in zip(rowset.rows, expected_rows):
319322
#print(rowset_row, expected_row)
320-
assert rowset_row['values']==expected_row[2:]
321-
assert rowset_row['rowId']==expected_row[0]
322-
assert rowset_row['versionNumber']==expected_row[1]
323+
assert_equals(rowset_row['values'], expected_row[2:])
324+
assert_equals(rowset_row['rowId'], expected_row[0])
325+
assert_equals(rowset_row['versionNumber'], expected_row[1])
323326

324327
## test asDataFrame
325328
try:
@@ -368,12 +371,15 @@ def test_list_of_rows_table():
368371
## need columns to do cast_values w/o storing
369372
table = Table(schema1, data, headers=[SelectColumn.from_column(col) for col in cols])
370373

374+
371375
for table_row, expected_row in zip(table, data):
372-
assert table_row==expected_row
376+
assert_equals(table_row, expected_row)
377+
378+
expected_rows = [[str(val) for val in row] for row in data]
373379

374380
rowset = table.asRowSet()
375-
for rowset_row, expected_row in zip(rowset.rows, data):
376-
assert rowset_row['values']==expected_row
381+
for rowset_row, expected_row in zip(rowset.rows, expected_rows):
382+
assert_equals(rowset_row['values'], expected_row)
377383

378384
table.columns = cols
379385

@@ -712,6 +718,22 @@ def test_constructor__name_to_col_id(self):
712718
assert_equals(711, partial_row.rowId)
713719

714720

721+
def test_values_have_string_type(type):
722+
values = {
723+
'12three':321,
724+
456:'65four'
725+
}
726+
partial_row = PartialRow(values, rowId=11111)
727+
for key_val in partial_row.values:
728+
assert_is_instance(key_val['key'], six.string_types)
729+
assert_is_instance(key_val['value'], six.string_types)
730+
731+
expected_values = [{'key':'12three', 'value':'321'}, {'key':'456', 'value':'65four'}]
732+
assert_equals(2, len(expected_values))
733+
assert_in(expected_values[0], partial_row.values)
734+
assert_in(expected_values[1], partial_row.values)
735+
736+
715737
class TestPartialRowSet():
716738
@raises(ValueError)
717739
def test_constructor__not_all_rows_of_type_PartialRow(self):
@@ -725,3 +747,9 @@ def test_constructor__single_PartialRow(self):
725747
assert_equals([partial_row], partial_rowset.rows)
726748

727749

750+
class TestRow():
751+
def test_values_have_string_type(self):
752+
row = Row([1,2,"three"])
753+
for val in row.values:
754+
assert_is_instance(val, six.string_types)
755+
assert_equals(["1","2","three"], row.values)

0 commit comments

Comments
 (0)