diff --git a/commcare_export/data_types.py b/commcare_export/data_types.py index 3a15340c..d21cdfaa 100644 --- a/commcare_export/data_types.py +++ b/commcare_export/data_types.py @@ -5,12 +5,15 @@ DATA_TYPE_DATE = 'date' DATA_TYPE_DATETIME = 'datetime' DATA_TYPE_INTEGER = 'integer' +DATA_TYPE_DECIMAL = 'decimal' DATA_TYPES_TO_SQLALCHEMY_TYPES = { + DATA_TYPE_TEXT: sqlalchemy.Text(), DATA_TYPE_BOOLEAN: sqlalchemy.Boolean(), DATA_TYPE_DATETIME: sqlalchemy.DateTime(), DATA_TYPE_DATE: sqlalchemy.Date(), DATA_TYPE_INTEGER: sqlalchemy.Integer(), + DATA_TYPE_DECIMAL: sqlalchemy.DECIMAL(), } class UnknownDataType(Exception): diff --git a/tests/test_writers.py b/tests/test_writers.py index b770af3d..1422eeb9 100644 --- a/tests/test_writers.py +++ b/tests/test_writers.py @@ -294,26 +294,28 @@ def test_explicit_types(self, strict_writer): with strict_writer: strict_writer.write_table(TableSpec(**{ 'name': 'foo_explicit_types', - 'headings': ['id', 'a', 'b', 'c', 'd'], + 'headings': ['id', 'a', 'b', 'c', 'd', 'e'], 'rows': [ - ['bizzle', '1', 2, 3, '7'], - ['bazzle', '4', 5, 6, '8'], + ['bizzle', '1', 2, 3, '7', '9'], + ['bazzle', '4', 5, 6, '8.5', '10'], ], 'data_types': [ 'text', 'integer', 'text', None, + 'decimal', ] })) # We can use raw SQL instead of SqlAlchemy expressions because we built the DB above with strict_writer: result = dict([(row['id'], row) for row in strict_writer.connection.execute( - 'SELECT id, a, b, c, d FROM foo_explicit_types' + 'SELECT id, a, b, c, d, e FROM foo_explicit_types' )]) assert len(result) == 2 - # a casts strings to ints, b casts ints to text, c default falls back to ints, d default falls back to text - assert dict(result['bizzle']) == {'id': 'bizzle', 'a': 1, 'b': '2', 'c': 3, 'd': '7'} - assert dict(result['bazzle']) == {'id': 'bazzle', 'a': 4, 'b': '5', 'c': 6, 'd': '8'} + # a casts strings to ints, b casts ints to text, c default falls back to ints, d casts to decimals + # e default falls back to text + assert dict(result['bizzle']) == {'id': 'bizzle', 'a': 1, 'b': '2', 'c': 3, 'd': 7, 'e': '9'} + assert dict(result['bazzle']) == {'id': 'bazzle', 'a': 4, 'b': '5', 'c': 6, 'd': 8.5, 'e': '10'}