Skip to content

Commit

Permalink
Add tests for CSV Datatype constraint enforcement
Browse files Browse the repository at this point in the history
diff --git a/onadata/libs/tests/utils/fixtures/bad_decimal.csv b/onadata/libs/tests/utils/fixtures/bad_decimal.csv
new file mode 100644
index 000000000..52e361c28
--- /dev/null
+++ b/onadata/libs/tests/utils/fixtures/bad_decimal.csv
@@ -0,0 +1,2 @@
+Enter_measurement,__version__,meta/instanceID,_id,_uuid,_submission_time,_tags,_notes,_version,_duration,_submitted_by,_total_media,_media_count,_media_all_received,_xform_id
+10,vFm6aq2HXxNpNrqjN9Du4L,uuid:3323c78f-f070-49c3-b955-5a6fa934b17e,53734025,3323c78f-f070-49c3-b955-5a6fa934b17e,2019-11-18T09:28:39,,,vFm6aq2HXxNpNrqjN9Du4L,,davisraym,0,0,True,464257
diff --git a/onadata/libs/tests/utils/fixtures/bad_decimal.xlsx b/onadata/libs/tests/utils/fixtures/bad_decimal.xlsx
new file mode 100644
index 000000000..5a596d087
Binary files /dev/null and b/onadata/libs/tests/utils/fixtures/bad_decimal.xlsx differ
diff --git a/onadata/libs/tests/utils/fixtures/bad_interger.csv b/onadata/libs/tests/utils/fixtures/bad_interger.csv
new file mode 100644
index 000000000..0e4b348ac
--- /dev/null
+++ b/onadata/libs/tests/utils/fixtures/bad_interger.csv
@@ -0,0 +1,10 @@
+name,age,gender,photo,date,location,_location_latitude,_location_longitude,_location_altitude,_location_precision,pizza_fan,pizza_type,favorite_toppings/cheese,favorite_toppings/pepperoni,favorite_toppings/sausauge,favorite_toppings/green_peppers,favorite_toppings/mushrooms,favorite_toppings/anchovies,start_time,end_time,today,imei,phonenumber,meta/instanceID,_uuid,_submission_time,_tags,_notes,_submitted_by,_version,_duration
+Name_1,10,male,NA,n/a,83.3595 -32.8601 0 1,83.3595,-32.8601,0,1,no,n/a,n/a,n/a,n/a,n/a,n/a,n/a,2014-09-04T15:06:01.000+03:00,2014-09-04T15:07:17.000+03:00,2014-09-04,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:685dd371-4831-4fdc-a205-f285337dd98d,685dd371-4831-4fdc-a205-f285337dd98d,2014-09-04T12:08:04,,,bob,,
+Name_2,20.85,male,NA,2014-09-30,-84.5351 29.0149 100000 1,-84.5351,29.0149,100000,2,yes,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:00,2014-09-04T12:24:00.000+03:00,2014-09-04,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:e92dad0d-ee3f-41eb-82d0-4cc0e7f12cb9,e92dad0d-ee3f-41eb-82d0-4cc0e7f12cb9,2014-09-04T09:24:57,,,bob,,
+Name_3,21.53,male,NA,2014-09-29,-84.5351 29.0149 100000 1,-83.5351,30.0149,2456,3,no,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:01,2014-09-04T12:24:00.000+03:01,2014-09-03,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:0b6d4344-6f64-41bc-8bab-a46a5493f9ad,0b6d4344-6f64-41bc-8bab-a46a5493f9ad,2014-09-04T09:24:58,,,bob,,
+Name_4,22.32,male,NA,2014-09-28,-84.5351 29.0149 100000 2,-82.5351,31.0149,7653,4,yes,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:02,2014-09-04T12:24:00.000+03:02,2014-09-02,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:15148861-93bc-45b8-ab56-6a9242c5a79d,15148861-93bc-45b8-ab56-6a9242c5a79d,2014-09-04T09:24:59,,,bob,,
+Name_5,23,male,NA,2014-09-27,-84.5351 29.0149 100000 3,-81.5351,32.0149,245,5,no,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:03,2014-09-04T12:24:00.000+03:03,2014-09-01,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:137e1fb7-81a3-43ae-9039-6f6f599d55a6,137e1fb7-81a3-43ae-9039-6f6f599d55a6,2014-09-04T09:24:32,,,bob,,
+Name_6,24,male,NA,2014-09-26,-84.5351 29.0149 100000 4,-80.5351,33.0149,65345,6,yes,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:04,2014-09-04T12:24:00.000+03:04,2014-0900,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:fb0af0bf-d476-4136-a51f-13d84f6f9d62,fb0af0bf-d476-4136-a51f-13d84f6f9d62,2014-09-04T09:24:33,,,bob,,
+Name_7,25,male,NA,2014-09-25,-84.5351 29.0149 100000 5,-79.5351,34.0149,23466,7,no,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:05,2014-09-04T12:24:00.000+03:05,2014-0901,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:f70bce6b-1785-43fd-8904-e8bb0975838a,f70bce6b-1785-43fd-8904-e8bb0975838a,2014-09-04T09:24:34,,,bob,,
+Name_8,26,male,NA,2014-09-24,-84.5351 29.0149 100000 6,-78.5351,35.0149,5634562,8,yes,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:06,2014-09-04T12:24:00.000+03:06,2014-0902,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:db78c788-2ea3-4250-ab32-866e946811b6,db78c788-2ea3-4250-ab32-866e946811b6,2014-09-04T09:24:35,,,tori,,
+Name_9,27,male,NA,2014-09-23,-84.5351 29.0149 100000 7,-77.5351,36.0149,24365,9,no,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:07,2014-09-04T12:24:00.000+03:07,2014-0903,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:0e1accb5-1c43-4789-ad2f-b9c663bbbc5d,0e1accb5-1c43-4789-ad2f-b9c663bbbc5d,2014-09-04T09:24:36,,,bob,,
\ No newline at end of file
diff --git a/onadata/libs/tests/utils/test_csv_import.py b/onadata/libs/tests/utils/test_csv_import.py
index 552c1df5b..2c23885ad 100644
--- a/onadata/libs/tests/utils/test_csv_import.py
+++ b/onadata/libs/tests/utils/test_csv_import.py
@@ -401,3 +401,35 @@ class CSVImportTestCase(TestBase):
             conv_datetime,
             [u'2020-06-12T13:20:00.000000', u'2019-03-11T16:00:51.147+02:00'])
         self.assertEqual(conv_dates, ['2019-03-01', '2019-02-26'])
+
+    def test_enforces_data_type(self):
+        """
+        Test that data type constraints are enforced
+        """
+        # Test interger constraint is enforced
+        xls_file_path = os.path.join(settings.PROJECT_ROOT, "apps", "main",
+                                     "tests", "fixtures", "tutorial.xls")
+        self._publish_xls_file(xls_file_path)
+        self.xform = XForm.objects.last()
+
+        bad_interger_csv = open(
+            os.path.join(self.fixtures_dir, 'bad_interger.csv'),
+            'rb')
+        result = csv_import.submit_csv(self.user.username, self.xform,
+                                       bad_interger_csv)
+        self.assertEqual(
+            result.get('error'),
+            'Unknown interger format: 20.85')
+
+        # Test decimal constraint is enforced
+        xls_file_path = os.path.join(self.fixtures_dir, 'bad_decimal.xlsx')
+        self._publish_xls_file(xls_file_path)
+        self.xform = XForm.objects.last()
+        bad_decimal_csv = open(
+            os.path.join(self.fixtures_dir, 'bad_decimal.csv'),
+            'rb')
+        result = csv_import.submit_csv(self.user.username, self.xform,
+                                       bad_decimal_csv)
+        self.assertEqual(
+            result.get('error'),
+            'Unknown decimal format: 10')
diff --git a/onadata/libs/utils/csv_import.py b/onadata/libs/utils/csv_import.py
index 1a42bc626..ab17c772d 100644
--- a/onadata/libs/utils/csv_import.py
+++ b/onadata/libs/utils/csv_import.py
@@ -270,21 +270,19 @@ def submit_csv(username, xform, csv_file, overwrite=False):
                     try:
                         interger = int(val)
                     except ValueError:
-                        return async_status(
-                            FAILED,
-                            (f'Unknown interger format: {val}'))
+                        raise Exception(f'Unknown interger format: {val}')
                     else:
                         row.update({key: interger})

             for key in xl_decimal_columns:
+                # For some reason Decimal fields are not validated
+                # We check that the passed in data is a valid decimal
                 val = row.get(key, '')
                 if val:
                     try:
                         decimal = float(row.get('key', ''))
                     except ValueError:
-                        return async_status(
-                            FAILED,
-                            (f'Unkown decimal format: {val}'))
+                        raise Exception(f'Unknown decimal format: {val}')
                     else:
                         row.update({key: decimal})
  • Loading branch information
DavisRayM committed Nov 18, 2019
1 parent 524af7e commit e8531a9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 0 deletions onadata/libs/tests/utils/fixtures/bad_decimal.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Enter_measurement,__version__,meta/instanceID,_id,_uuid,_submission_time,_tags,_notes,_version,_duration,_submitted_by,_total_media,_media_count,_media_all_received,_xform_id
10,vFm6aq2HXxNpNrqjN9Du4L,uuid:3323c78f-f070-49c3-b955-5a6fa934b17e,53734025,3323c78f-f070-49c3-b955-5a6fa934b17e,2019-11-18T09:28:39,,,vFm6aq2HXxNpNrqjN9Du4L,,davisraym,0,0,True,464257
Binary file not shown.
10 changes: 10 additions & 0 deletions onadata/libs/tests/utils/fixtures/bad_interger.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name,age,gender,photo,date,location,_location_latitude,_location_longitude,_location_altitude,_location_precision,pizza_fan,pizza_type,favorite_toppings/cheese,favorite_toppings/pepperoni,favorite_toppings/sausauge,favorite_toppings/green_peppers,favorite_toppings/mushrooms,favorite_toppings/anchovies,start_time,end_time,today,imei,phonenumber,meta/instanceID,_uuid,_submission_time,_tags,_notes,_submitted_by,_version,_duration
Name_1,10,male,NA,n/a,83.3595 -32.8601 0 1,83.3595,-32.8601,0,1,no,n/a,n/a,n/a,n/a,n/a,n/a,n/a,2014-09-04T15:06:01.000+03:00,2014-09-04T15:07:17.000+03:00,2014-09-04,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:685dd371-4831-4fdc-a205-f285337dd98d,685dd371-4831-4fdc-a205-f285337dd98d,2014-09-04T12:08:04,,,bob,,
Name_2,20.85,male,NA,2014-09-30,-84.5351 29.0149 100000 1,-84.5351,29.0149,100000,2,yes,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:00,2014-09-04T12:24:00.000+03:00,2014-09-04,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:e92dad0d-ee3f-41eb-82d0-4cc0e7f12cb9,e92dad0d-ee3f-41eb-82d0-4cc0e7f12cb9,2014-09-04T09:24:57,,,bob,,
Name_3,21.53,male,NA,2014-09-29,-84.5351 29.0149 100000 1,-83.5351,30.0149,2456,3,no,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:01,2014-09-04T12:24:00.000+03:01,2014-09-03,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:0b6d4344-6f64-41bc-8bab-a46a5493f9ad,0b6d4344-6f64-41bc-8bab-a46a5493f9ad,2014-09-04T09:24:58,,,bob,,
Name_4,22.32,male,NA,2014-09-28,-84.5351 29.0149 100000 2,-82.5351,31.0149,7653,4,yes,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:02,2014-09-04T12:24:00.000+03:02,2014-09-02,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:15148861-93bc-45b8-ab56-6a9242c5a79d,15148861-93bc-45b8-ab56-6a9242c5a79d,2014-09-04T09:24:59,,,bob,,
Name_5,23,male,NA,2014-09-27,-84.5351 29.0149 100000 3,-81.5351,32.0149,245,5,no,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:03,2014-09-04T12:24:00.000+03:03,2014-09-01,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:137e1fb7-81a3-43ae-9039-6f6f599d55a6,137e1fb7-81a3-43ae-9039-6f6f599d55a6,2014-09-04T09:24:32,,,bob,,
Name_6,24,male,NA,2014-09-26,-84.5351 29.0149 100000 4,-80.5351,33.0149,65345,6,yes,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:04,2014-09-04T12:24:00.000+03:04,2014-0900,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:fb0af0bf-d476-4136-a51f-13d84f6f9d62,fb0af0bf-d476-4136-a51f-13d84f6f9d62,2014-09-04T09:24:33,,,bob,,
Name_7,25,male,NA,2014-09-25,-84.5351 29.0149 100000 5,-79.5351,34.0149,23466,7,no,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:05,2014-09-04T12:24:00.000+03:05,2014-0901,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:f70bce6b-1785-43fd-8904-e8bb0975838a,f70bce6b-1785-43fd-8904-e8bb0975838a,2014-09-04T09:24:34,,,bob,,
Name_8,26,male,NA,2014-09-24,-84.5351 29.0149 100000 6,-78.5351,35.0149,5634562,8,yes,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:06,2014-09-04T12:24:00.000+03:06,2014-0902,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:db78c788-2ea3-4250-ab32-866e946811b6,db78c788-2ea3-4250-ab32-866e946811b6,2014-09-04T09:24:35,,,tori,,
Name_9,27,male,NA,2014-09-23,-84.5351 29.0149 100000 7,-77.5351,36.0149,24365,9,no,chitown,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,2014-09-04T12:19:31.000+03:07,2014-09-04T12:24:00.000+03:07,2014-0903,enketo.org:2gnoXEilHRGn6V5i,no phonenumber property in enketo,uuid:0e1accb5-1c43-4789-ad2f-b9c663bbbc5d,0e1accb5-1c43-4789-ad2f-b9c663bbbc5d,2014-09-04T09:24:36,,,bob,,
32 changes: 32 additions & 0 deletions onadata/libs/tests/utils/test_csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,35 @@ def test_excel_date_conversion(self):
conv_datetime,
[u'2020-06-12T13:20:00.000000', u'2019-03-11T16:00:51.147+02:00'])
self.assertEqual(conv_dates, ['2019-03-01', '2019-02-26'])

def test_enforces_data_type(self):
"""
Test that data type constraints are enforced
"""
# Test interger constraint is enforced
xls_file_path = os.path.join(settings.PROJECT_ROOT, "apps", "main",
"tests", "fixtures", "tutorial.xls")
self._publish_xls_file(xls_file_path)
self.xform = XForm.objects.last()

bad_interger_csv = open(
os.path.join(self.fixtures_dir, 'bad_interger.csv'),
'rb')
result = csv_import.submit_csv(self.user.username, self.xform,
bad_interger_csv)
self.assertEqual(
result.get('error'),
'Unknown interger format: 20.85')

# Test decimal constraint is enforced
xls_file_path = os.path.join(self.fixtures_dir, 'bad_decimal.xlsx')
self._publish_xls_file(xls_file_path)
self.xform = XForm.objects.last()
bad_decimal_csv = open(
os.path.join(self.fixtures_dir, 'bad_decimal.csv'),
'rb')
result = csv_import.submit_csv(self.user.username, self.xform,
bad_decimal_csv)
self.assertEqual(
result.get('error'),
'Unknown decimal format: 10')
10 changes: 4 additions & 6 deletions onadata/libs/utils/csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,21 +270,19 @@ def submit_csv(username, xform, csv_file, overwrite=False):
try:
interger = int(val)
except ValueError:
return async_status(
FAILED,
(f'Unknown interger format: {val}'))
raise Exception(f'Unknown interger format: {val}')
else:
row.update({key: interger})

for key in xl_decimal_columns:
# For some reason Decimal fields are not validated
# We check that the passed in data is a valid decimal
val = row.get(key, '')
if val:
try:
decimal = float(row.get('key', ''))
except ValueError:
return async_status(
FAILED,
(f'Unkown decimal format: {val}'))
raise Exception(f'Unknown decimal format: {val}')
else:
row.update({key: decimal})

Expand Down

0 comments on commit e8531a9

Please sign in to comment.