Skip to content

Commit

Permalink
Merge pull request redcap-tools#24 from sburns/import-error-exception
Browse files Browse the repository at this point in the history
Raise exception when import_records fails
  • Loading branch information
Scott Burns committed Nov 11, 2013
2 parents 271babe + 770db8b commit 328aed9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions redcap/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def export_fem(self, arms=None, format='obj', df_kwargs=None):
arms: list
Limit exported form event mappings to these arm numbers
format: {'obj', 'csv', 'xml'} default obj
Return the form event mappings in native objects,
Return the form event mappings in native objects,
csv or xml, df will return a pandas.DataFrame
df_kwargs: dict
Passed to pandas.read_csv to control construction of
Expand Down Expand Up @@ -347,7 +347,10 @@ def import_records(self, to_import, overwrite='normal', format='json',
pl['format'] = format
pl['returnFormat'] = return_format
pl['returnContent'] = return_content
return self._call_api(pl, 'imp_record')[0]
response = self._call_api(pl, 'imp_record')[0]
if 'error' in response:
raise RedcapError(str(response))
return response

def export_file(self, record, field, event=None, return_format='json'):
""" Export the contents of a file stored for a particular record
Expand Down
16 changes: 16 additions & 0 deletions test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ def test_long_export(self):
for record in data:
self.assertIsInstance(record, dict)

def test_import_records(self):
"Test record import"
data = self.reg_proj.export_records()
response = self.reg_proj.import_records(data)
self.assertIn('count', response)
self.assertNotIn('error', response)

def test_import_exception(self):
"Test record import throws RedcapError for bad import"
data = self.reg_proj.export_records()
data[0]['non_existent_key'] = 'foo'
with self.assertRaises(RedcapError) as cm:
self.reg_proj.import_records(data)
exc = cm.exception
self.assertIn('error', exc.args[0])

def is_good_csv(self, csv_string):
"Helper to test csv strings"
return isinstance(csv_string, basestring)
Expand Down

0 comments on commit 328aed9

Please sign in to comment.