From 340a3dcd063dbe711e217d0ff1c3bc2007f2ef45 Mon Sep 17 00:00:00 2001 From: Cory Zue Date: Fri, 10 Jul 2020 09:27:15 +0200 Subject: [PATCH 1/4] add numeric type support --- commcare_export/data_types.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/commcare_export/data_types.py b/commcare_export/data_types.py index 3a15340c..73dbcbc8 100644 --- a/commcare_export/data_types.py +++ b/commcare_export/data_types.py @@ -5,12 +5,14 @@ DATA_TYPE_DATE = 'date' DATA_TYPE_DATETIME = 'datetime' DATA_TYPE_INTEGER = 'integer' +DATA_TYPE_NUMERIC = 'numeric' DATA_TYPES_TO_SQLALCHEMY_TYPES = { DATA_TYPE_BOOLEAN: sqlalchemy.Boolean(), DATA_TYPE_DATETIME: sqlalchemy.DateTime(), DATA_TYPE_DATE: sqlalchemy.Date(), DATA_TYPE_INTEGER: sqlalchemy.Integer(), + DATA_TYPE_NUMERIC: sqlalchemy.Numeric(), } class UnknownDataType(Exception): From 7e44da598263310272fca0a30ec5b4d0dbe81d57 Mon Sep 17 00:00:00 2001 From: Cory Zue Date: Fri, 10 Jul 2020 09:44:00 +0200 Subject: [PATCH 2/4] add explicit text type --- commcare_export/data_types.py | 1 + 1 file changed, 1 insertion(+) diff --git a/commcare_export/data_types.py b/commcare_export/data_types.py index 73dbcbc8..0ecbe40a 100644 --- a/commcare_export/data_types.py +++ b/commcare_export/data_types.py @@ -8,6 +8,7 @@ DATA_TYPE_NUMERIC = 'numeric' 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(), From c517e315b1eb96f96ab87f71023e579d7972f277 Mon Sep 17 00:00:00 2001 From: Cory Zue Date: Fri, 10 Jul 2020 09:44:26 +0200 Subject: [PATCH 3/4] add test for numeric types --- tests/test_writers.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/test_writers.py b/tests/test_writers.py index b770af3d..6b0a4ce5 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, + 'numeric', ] })) # 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 numbers + # 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'} From a99e4c4d74ac34c6dc1b56ba601f0136d7747f03 Mon Sep 17 00:00:00 2001 From: Cory Zue Date: Mon, 13 Jul 2020 14:00:13 +0200 Subject: [PATCH 4/4] use decimal instead of numeric type --- commcare_export/data_types.py | 4 ++-- tests/test_writers.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/commcare_export/data_types.py b/commcare_export/data_types.py index 0ecbe40a..d21cdfaa 100644 --- a/commcare_export/data_types.py +++ b/commcare_export/data_types.py @@ -5,7 +5,7 @@ DATA_TYPE_DATE = 'date' DATA_TYPE_DATETIME = 'datetime' DATA_TYPE_INTEGER = 'integer' -DATA_TYPE_NUMERIC = 'numeric' +DATA_TYPE_DECIMAL = 'decimal' DATA_TYPES_TO_SQLALCHEMY_TYPES = { DATA_TYPE_TEXT: sqlalchemy.Text(), @@ -13,7 +13,7 @@ DATA_TYPE_DATETIME: sqlalchemy.DateTime(), DATA_TYPE_DATE: sqlalchemy.Date(), DATA_TYPE_INTEGER: sqlalchemy.Integer(), - DATA_TYPE_NUMERIC: sqlalchemy.Numeric(), + DATA_TYPE_DECIMAL: sqlalchemy.DECIMAL(), } class UnknownDataType(Exception): diff --git a/tests/test_writers.py b/tests/test_writers.py index 6b0a4ce5..1422eeb9 100644 --- a/tests/test_writers.py +++ b/tests/test_writers.py @@ -304,7 +304,7 @@ def test_explicit_types(self, strict_writer): 'integer', 'text', None, - 'numeric', + 'decimal', ] })) @@ -315,7 +315,7 @@ def test_explicit_types(self, strict_writer): )]) assert len(result) == 2 - # a casts strings to ints, b casts ints to text, c default falls back to ints, d casts to numbers + # 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'}