Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions commcare_export/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 9 additions & 7 deletions tests/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}