Skip to content

Commit cc92670

Browse files
committed
✅ Add tests for export survey link
1 parent 8e26b37 commit cc92670

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

redcap/methods/surveys.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def export_survey_link(
1717
record: str,
1818
event: Optional[str] = None,
1919
repeat_instance: int = 1,
20-
):
20+
) -> str:
2121
"""
2222
Export one survey link
2323
@@ -32,36 +32,27 @@ def export_survey_link(
3232
event:
3333
Unique event name, only used in longitudinal projects
3434
repeat_instance:
35-
only for projects with repeating instruments/events)
36-
The repeat instance number of the repeating event (if longitudinal)
37-
or the repeating instrument (if classic or longitudinal).
35+
only for projects with repeating instruments/events)
36+
The repeat instance number of the repeating event (if longitudinal)
37+
or the repeating instrument (if classic or longitudinal).
3838
Default value is '1'.
3939
4040
Returns:
41-
Str:
42-
URL of survey link requested
41+
URL of survey link requested
4342
4443
Examples:
45-
>>> proj.export_survey_link(instrument="form_1", record="5", event="event_1_arm_1")
46-
https://redcap.mytld.com/surveys/?s=6B2zFAEWPVSrXnnx
47-
"""
48-
payload = self._initialize_payload(
49-
content="surveyLink"
50-
)
44+
>>> proj.export_survey_link(instrument="form_1", record="1", event="event_1_arm_1")
45+
'https://redcapdemo.vumc.org/surveys/?s=...'
46+
"""
47+
payload = self._initialize_payload(content="surveyLink")
5148
payload["instrument"] = instrument
5249
payload["record"] = record
53-
if event:
54-
payload["event"] = event
5550
payload["repeat_instance"] = repeat_instance
5651

57-
return_type = "str"
58-
response = cast(Union[Json, str], self._call_api(payload, return_type))
52+
if event:
53+
payload["event"] = event
5954

60-
return self._return_data(
61-
response=response,
62-
content="surveyLink",
63-
format_type=return_type,
64-
)
55+
return cast(str, self._call_api(payload, return_type="str"))
6556

6657
def export_survey_participant_list(
6758
self,

tests/integration/test_long_project.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,11 @@ def test_fem_import(long_project):
303303

304304
response = long_project.import_instrument_event_mappings(current_fem)
305305
assert response == 44
306+
307+
308+
@pytest.mark.integration
309+
def test_export_survey_link(long_project):
310+
link = long_project.export_survey_link(
311+
instrument="contact_info", event="enrollment_arm_1", record="1"
312+
)
313+
assert link.startswith("https://redcapdemo.vumc.org/surveys/?s=")

tests/unit/callback_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,22 @@ def handle_long_project_survey_participants_request(**kwargs) -> Any:
781781
return (201, headers, json.dumps(resp))
782782

783783

784+
def handle_long_project_survey_link_request(**kwargs) -> Any:
785+
"""Get the survey link for a record and instrument and event"""
786+
data = kwargs["data"]
787+
headers = kwargs["headers"]
788+
resp = None
789+
790+
if (
791+
"test" in data.get("instrument")
792+
and "raw" in data.get("event")
793+
and "1" in data.get("record")
794+
):
795+
resp = "https://redcapdemo.vumc.org/surveys/?s=DMgheEPAgLETxJkf"
796+
797+
return (201, headers, resp)
798+
799+
784800
def get_simple_project_request_handler(request_type: str) -> Callable:
785801
"""Given a request type, extract the handler function"""
786802
handlers_dict = {
@@ -823,6 +839,7 @@ def get_long_project_request_handler(request_type: str) -> Callable:
823839
"pdf": handle_long_project_pdf_request,
824840
"record": handle_long_project_records_request,
825841
"report": handle_long_project_reports_request,
842+
"surveyLink": handle_long_project_survey_link_request,
826843
"version": handle_long_project_version_request,
827844
}
828845

tests/unit/test_long_project.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def test_export_survey_participants_list(long_project):
7171
assert is_json(res)
7272

7373

74+
def test_export_survey_link(long_project):
75+
res = long_project.export_survey_link(instrument="test", record="1", event="raw")
76+
assert res.startswith("https://redcapdemo.vumc.org/surveys/?s=")
77+
78+
7479
def test_metadata_import_handles_api_error(long_project):
7580
metadata = long_project.export_metadata()
7681

0 commit comments

Comments
 (0)