diff --git a/datadotworld/models/query.py b/datadotworld/models/query.py index 49c65bd..7be549b 100644 --- a/datadotworld/models/query.py +++ b/datadotworld/models/query.py @@ -102,6 +102,24 @@ def _iter_rows(self): values.append(None) table_row = schema_obj.cast_row(values) + + # when the column is a string value, the jsontableschema + # library is incorrectly mapping the several literal + # string values ('null', 'none', '-', etc.) to the python + # `None` value - a deeper fix might be to reconsider using + # that library, or maybe fixing this issue in that + # library (since it's probably not a good idea to render + # a number of strings un-representable) - this fixes the + # problem for our result sets. Essentially, this zips + # over each result set and checks whether we mapped a + # non-null value to `None` in a string field, and if + # so it restores the non-null value before continuing + table_row = map(lambda field, original, mapped: + original if (not mapped) and original + and field.type == 'string' + else mapped, + schema_obj.fields, values, table_row) + yield OrderedDict(zip(field_names, table_row)) elif 'boolean' in self.raw_data: # Results of an ASK query diff --git a/tests/datadotworld/models/test_query.py b/tests/datadotworld/models/test_query.py index 9f2c04d..75b5b87 100644 --- a/tests/datadotworld/models/test_query.py +++ b/tests/datadotworld/models/test_query.py @@ -94,5 +94,15 @@ def test_dataframe(self, query_result_example, query_results): equal_to((len(query_result_example['results']['bindings']), len(metadata_names)))) + def test_str_column(self, query_results): + ''' + Test added for https://github.com/datadotworld/data.world-py/issues/68 + added a value in the `st.name` column in sql_select.json of "NONE", + which was previously mapped to Python `None` - this verifies that we + have fixed that issue. + ''' + for value in query_results.dataframe['st.name']: + assert_that(value) + def test_str(self, query_results, query_result_example): assert_that(str(query_results), equal_to(str(query_result_example))) diff --git a/tests/fixtures/queries/sql_select.json b/tests/fixtures/queries/sql_select.json index f01f551..aaa0ce4 100644 --- a/tests/fixtures/queries/sql_select.json +++ b/tests/fixtures/queries/sql_select.json @@ -589,7 +589,7 @@ }, "v_5": { "type": "literal", - "value": "UT West Mall @ Guadalupe" + "value": "NONE" }, "v_6": { "type": "literal",