def test_calls_monitor_scan_import_for_session(self, mock_http, mock_monitor, det, records): mock_http.return_value = self.mock_redcap_export() rc_utils.create_from_request(det) session = dashboard.models.Session.query.get((self.session[:-3], 1)) assert mock_monitor.called_once_with(session)
def test_adds_redcap_record_to_session(self, mock_http, mock_monitor, det, records): mock_http.return_value = self.mock_redcap_export() session = dashboard.models.Session.query.get((self.session[:-3], 1)) assert session.redcap_record is None rc_utils.create_from_request(det) assert session.redcap_record is not None assert session.redcap_record.record.comment == self.comment
def test_sets_event_id_of_redcap_record(self, mock_http, mock_monitor, det, records): mock_http.return_value = self.mock_redcap_export() rc_utils.create_from_request(det) session = dashboard.models.Session.query.get((self.session[:-3], 1)) assert session.redcap_record is not None db_record = session.redcap_record.record assert db_record.event_id == self.event_ids[self.event_name]
def test_det_with_different_version_causes_redcap_config_version_update( self, mock_http, mock_monitor, det, records): mock_http.return_value = self.mock_redcap_export() redcap_config = dashboard.models.RedcapConfig.get_config( project=self.pid, instrument=self.instrument, url=self.url) assert redcap_config is not None assert redcap_config.redcap_version != self.version rc_utils.create_from_request(det) assert redcap_config.redcap_version == self.version
def test_exception_raised_if_multiple_redcap_records_match( self, mock_http, mock_monitor, det, records): export_output = [] export_output.extend(self.redcap_records) export_output.append({ "record": self.record_id, "redcap_event_name": self.event_name }) mock_http.return_value = self.mock_redcap_export(export_output) with pytest.raises(dashboard.exceptions.RedcapException): rc_utils.create_from_request(det)
def test_no_records_added_when_matching_redcap_config_not_found( self, mock_http, mock_monitor, det, records): det.form["project_id"] = 1111 assert det.form["project_id"] != self.pid before_det = self.get_db_records() rc_utils.create_from_request(det) after_det = self.get_db_records() assert mock_http.call_count == 0 for idx in range(len(before_det)): assert before_det[idx] == after_det[idx]
def test_calls_monitor_scan_download_if_download_script_is_set( self, mock_download, mock_http, mock_scan_monitor, det, records): mock_http.return_value = self.mock_redcap_export() study_site = dashboard.models.StudySite.query.get(("STUDY", "ABC")) study_site.download_script = "/some/path/post_download.sh" dashboard.models.db.session.add(study_site) dashboard.models.db.session.commit() rc_utils.create_from_request(det) session = dashboard.models.Session.query.get((self.session[:-3], 1)) assert mock_download.called_once_with(session)
def test_adds_session_if_doesnt_exist(self, mock_http, mock_monitor, det, records): mock_http.return_value = self.mock_redcap_export() timepoint = self.session[:-3] session = dashboard.models.Session.query.get((timepoint, 1)) if session: session.delete() assert dashboard.models.Session.query.get((timepoint, 1)) is None rc_utils.create_from_request(det) session = dashboard.models.Session.query.get((timepoint, 1)) assert session is not None
def test_no_records_added_when_form_not_complete(self, mock_http, mock_monitor, det, dash_db): det.form[self.completed_field] = "1" for item in self.get_db_records(): assert item == [], "Error - Database not empty before test ran." rc_utils.create_from_request(det) assert mock_http.call_count == 0 for item in self.get_db_records(): assert item == [], "Error - New data was added to database."
def test_doesnt_fail_if_config_event_ids_not_set(self, mock_http, mock_monitor, det, records): mock_http.return_value = self.mock_redcap_export() # Delete the event_ids from config rc = dashboard.models.RedcapConfig.query.get(1) rc.event_ids = None dashboard.models.db.session.add(rc) dashboard.models.db.session.commit() assert rc.event_ids is None rc_utils.create_from_request(det) record = dashboard.models.RedcapRecord.query.get(1) assert record is not None
def test_updates_redcap_record_if_updates_on_server( self, mock_http, mock_monitor, det, records): mock_http.return_value = self.mock_redcap_export() # Add a redcap record rc_utils.create_from_request(det) # Modify the comment on the 'server' new_comment = "Scan was NOT ok." new_record = self.redcap_records[0].copy() new_record[self.comment_field] = new_comment mock_http.return_value = self.mock_redcap_export([new_record]) # Resend the data entry trigger rc_utils.create_from_request(det) session = dashboard.models.Session.query.get((self.session[:-3], 1)) assert session is not None assert session.redcap_record.record.comment == new_comment
def test_exception_raised_if_record_not_found_on_server( self, mock_http, mock_monitor, det, records): mock_http.return_value = self.mock_redcap_export([]) with pytest.raises(dashboard.exceptions.RedcapException): rc_utils.create_from_request(det)
def test_raises_exception_when_malformed_request_received( self, mock_http, mock_monitor, det, records): del det.form["project_id"] with pytest.raises(dashboard.exceptions.RedcapException): rc_utils.create_from_request(det)