From c3c9c50763c83f5fd0b6d0e5ae7de266d4f10ade Mon Sep 17 00:00:00 2001 From: Scott Burns Date: Wed, 22 Jan 2014 09:58:25 -0600 Subject: [PATCH 1/2] Initialize projects for testing only once setUp runs before every test! This vastly speeds up running the test suite. --- test/test_project.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/test_project.py b/test/test_project.py index 7028d8f1..6d065c10 100644 --- a/test/test_project.py +++ b/test/test_project.py @@ -13,15 +13,15 @@ class ProjectTests(unittest.TestCase): """docstring for ProjectTests""" - def setUp(self): - self.url = 'https://redcap.vanderbilt.edu/api/' - self.bad_url = 'https://redcap.vanderbilt.edu/api' - self.reg_token = '8E66DB6844D58E990075AFB51658A002' - self.long_proj = Project(self.url, '1387872621BBF1C17CC47FD8AE25FF54') - self.reg_proj = Project(self.url, self.reg_token) - self.ssl_proj = Project(self.url, self.reg_token, - verify_ssl=False) + url = 'https://redcap.vanderbilt.edu/api/' + bad_url = 'https://redcap.vanderbilt.edu/api' + reg_token = '8E66DB6844D58E990075AFB51658A002' + long_proj = Project(url, '1387872621BBF1C17CC47FD8AE25FF54') + reg_proj = Project(url, reg_token) + ssl_proj = Project(url, reg_token, verify_ssl=False) + def setUp(self): + pass def tearDown(self): pass From 4e26ce1def7dc6b80f6962d6cd2e464faad33167 Mon Sep 17 00:00:00 2001 From: Scott Burns Date: Wed, 22 Jan 2014 10:41:47 -0600 Subject: [PATCH 2/2] Add new export parameters Adds: - export_survey_fields to export survey information - export_data_access_groups to export access group info per record --- redcap/project.py | 20 +++++++++++++++++--- test/test_project.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/redcap/project.py b/redcap/project.py index e1e9ebaa..e0ccbbe6 100644 --- a/redcap/project.py +++ b/redcap/project.py @@ -167,7 +167,8 @@ def export_metadata(self, fields=None, forms=None, format='obj', def export_records(self, records=None, fields=None, forms=None, events=None, raw_or_label='raw', event_name='label', - format='obj', df_kwargs=None): + format='obj', export_survey_fields=False, + export_data_access_groups=False, df_kwargs=None): """Return data High level function of Project @@ -196,6 +197,18 @@ def export_records(self, records=None, fields=None, forms=None, Format of returned data. 'obj' returns json-decoded objects 'csv' and 'xml' return other formats. 'df' will attempt to return a pandas.DataFrame. + export_survey_fields: True | False [default] + specifies whether or not to export the survey identifier + field (e.g., "redcap_survey_identifier") or survey timestamp + fields (e.g., form_name+"_timestamp") when surveys are + utilized in the project. + export_data_access_groups: True | False [default] + specifies whether or not to export the + "redcap_data_access_group" field when data access groups + are utilized in the project. NOTE: This flag is only viable + if the user whose token is being used to make the API + request is *not* in a data access group. If the user is + in a group, then this flag will revert to its default value. df_kwargs: dict [default: {'index_col': self.def_field}] Passed to pandas.read_csv to control construction of returned DataFrame @@ -209,9 +222,10 @@ def export_records(self, records=None, fields=None, forms=None, ret_format = 'csv' pl = self.__basepl('record', format=ret_format) keys_to_add = (records, fields, forms, events, - raw_or_label, event_name) + raw_or_label, event_name, export_survey_fields, + export_data_access_groups) str_keys = ('records', 'fields', 'forms', 'events', 'rawOrLabel', - 'eventName') + 'eventName', 'exportSurveyFields', 'exportDataAccessGroups') for key, data in zip(str_keys, keys_to_add): if data: # Make a url-ok string diff --git a/test/test_project.py b/test/test_project.py index 6d065c10..4e3a7e9e 100644 --- a/test/test_project.py +++ b/test/test_project.py @@ -19,6 +19,7 @@ class ProjectTests(unittest.TestCase): long_proj = Project(url, '1387872621BBF1C17CC47FD8AE25FF54') reg_proj = Project(url, reg_token) ssl_proj = Project(url, reg_token, verify_ssl=False) + survey_proj = Project(url, '37CAB1ABC2FEB3BB9D821DF13BA38A7B') def setUp(self): pass @@ -195,6 +196,33 @@ def test_verify_ssl(self): self.assertIn('verify', post_kwargs) self.assertTrue(post_kwargs['verify']) + def test_export_data_access_groups(self): + """Test we get 'redcap_data_access_group' in exported data""" + records = self.reg_proj.export_records(export_data_access_groups=True) + for record in records: + self.assertIn('redcap_data_access_group', record) + # When not passed, that key shouldn't be there + records = self.reg_proj.export_records() + for record in records: + self.assertNotIn('redcap_data_access_group', record) + + def test_export_survey_fields(self): + """Test that we get the appropriate survey keys in the exported + data. + + Note that the 'demographics' form has been setup as the survey + in the `survey_proj` project. The _timestamp field will vary for + users as their survey form will be named differently""" + records = self.survey_proj.export_records(export_survey_fields=True) + for record in records: + self.assertIn('redcap_survey_identifier', record) + self.assertIn('demographics_timestamp', record) + # The regular project doesn't have a survey setup. Users should + # be able this argument as True but it winds up a no-op. + records = self.reg_proj.export_records(export_survey_fields=True) + for record in records: + self.assertNotIn('redcap_survey_identifier', record) + self.assertNotIn('demographics_timestamp', record) @unittest.skipIf(skip_pd, "Couldn't import pandas") def test_metadata_to_df(self):