From ad9626e3fefec3ca7144f7bed30faf73f657180c Mon Sep 17 00:00:00 2001 From: Ziming Dong Date: Fri, 8 Dec 2017 19:09:41 -0800 Subject: [PATCH] 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 --- synapseclient/table.py | 6 ++--- tests/unit/unit_test_tables.py | 46 +++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/synapseclient/table.py b/synapseclient/table.py index c1ddff8cc..4c2c7fe9c 100644 --- a/synapseclient/table.py +++ b/synapseclient/table.py @@ -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: @@ -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 diff --git a/tests/unit/unit_test_tables.py b/tests/unit/unit_test_tables.py index e1c8334de..3ef5aa8cb 100644 --- a/tests/unit/unit_test_tables.py +++ b/tests/unit/unit_test_tables.py @@ -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 @@ -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 @@ -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: @@ -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 @@ -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): @@ -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)