Skip to content

Commit

Permalink
Row and PartialRow now always cast values to string (SYNPY-615) (#497)
Browse files Browse the repository at this point in the history
* Row and PartialRow now always cast values to string

* newline at end of file

* fixed tests
  • Loading branch information
zimingd authored and xschildw committed Dec 9, 2017
1 parent 41764f7 commit ad9626e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
6 changes: 3 additions & 3 deletions synapseclient/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ class Row(DictObject):
"""
def __init__(self, values, rowId=None, versionNumber=None, etag=None):
super(Row, self).__init__()
self.values = values
self.values = [str(value) for value in values]
if rowId is not None:
self.rowId = rowId
if versionNumber is not None:
Expand Down Expand Up @@ -906,8 +906,8 @@ def __init__(self, values, rowId, etag=None, nameToColumnId=None):

rowId = int(rowId)

self.values = [{'key': nameToColumnId[x_key] if nameToColumnId is not None else x_key,
'value': x_value} for x_key, x_value in six.iteritems(values)]
self.values = [{'key': str(nameToColumnId[x_key]) if nameToColumnId is not None else str(x_key),
'value': str(x_value)} for x_key, x_value in six.iteritems(values)]
self.rowId = rowId
if etag is not None:
self.etag = etag
Expand Down
46 changes: 37 additions & 9 deletions tests/unit/unit_test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import math
import os
import sys
import six
import tempfile
from builtins import zip
from mock import MagicMock
from nose.tools import assert_raises, assert_equals, assert_not_equals, raises, assert_false, assert_not_in, assert_sequence_equal
from nose.tools import assert_raises, assert_equals, assert_not_equals, raises, assert_false, assert_not_in, assert_sequence_equal, assert_in
from nose import SkipTest
import unit

Expand All @@ -22,7 +23,7 @@
except ImportError:
pandas_found = True

from nose.tools import raises, assert_equals, assert_set_equal
from nose.tools import raises, assert_equals, assert_is_instance
import unit
import synapseclient
from synapseclient import Entity
Expand Down Expand Up @@ -313,13 +314,15 @@ def test_csv_table():
# print(table_row, expected_row)
assert table_row==expected_row

expected_rows = [[str(val) for val in row] for row in data]

## test asRowSet
rowset = table.asRowSet()
for rowset_row, expected_row in zip(rowset.rows, data):
for rowset_row, expected_row in zip(rowset.rows, expected_rows):
#print(rowset_row, expected_row)
assert rowset_row['values']==expected_row[2:]
assert rowset_row['rowId']==expected_row[0]
assert rowset_row['versionNumber']==expected_row[1]
assert_equals(rowset_row['values'], expected_row[2:])
assert_equals(rowset_row['rowId'], expected_row[0])
assert_equals(rowset_row['versionNumber'], expected_row[1])

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


for table_row, expected_row in zip(table, data):
assert table_row==expected_row
assert_equals(table_row, expected_row)

expected_rows = [[str(val) for val in row] for row in data]

rowset = table.asRowSet()
for rowset_row, expected_row in zip(rowset.rows, data):
assert rowset_row['values']==expected_row
for rowset_row, expected_row in zip(rowset.rows, expected_rows):
assert_equals(rowset_row['values'], expected_row)

table.columns = cols

Expand Down Expand Up @@ -712,6 +718,22 @@ def test_constructor__name_to_col_id(self):
assert_equals(711, partial_row.rowId)


def test_values_have_string_type(type):
values = {
'12three':321,
456:'65four'
}
partial_row = PartialRow(values, rowId=11111)
for key_val in partial_row.values:
assert_is_instance(key_val['key'], six.string_types)
assert_is_instance(key_val['value'], six.string_types)

expected_values = [{'key':'12three', 'value':'321'}, {'key':'456', 'value':'65four'}]
assert_equals(2, len(expected_values))
assert_in(expected_values[0], partial_row.values)
assert_in(expected_values[1], partial_row.values)


class TestPartialRowSet():
@raises(ValueError)
def test_constructor__not_all_rows_of_type_PartialRow(self):
Expand All @@ -725,3 +747,9 @@ def test_constructor__single_PartialRow(self):
assert_equals([partial_row], partial_rowset.rows)


class TestRow():
def test_values_have_string_type(self):
row = Row([1,2,"three"])
for val in row.values:
assert_is_instance(val, six.string_types)
assert_equals(["1","2","three"], row.values)

0 comments on commit ad9626e

Please sign in to comment.