Skip to content

Commit 718bee9

Browse files
njvackpwildenhain
authored andcommitted
Add background_import param to import_records()
Setting this to True will ask REDCap to import records in the background; this will work similarly to the background import in the Data Import Tool. Because this flag is not supported in old REDCap versions, if you do not specify it, it will not be added to your request parameters at all and you will get the default REDCap behavior. Currently, REDCap defaults to foreground imports.
1 parent 73edc79 commit 718bee9

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

redcap/methods/records.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ def import_records(
259259
record_type: Literal["flat", "eav"] = "flat",
260260
date_format: Literal["YMD", "DMY", "MDY"] = "YMD",
261261
force_auto_number: bool = False,
262+
background_process: Optional[bool] = None,
262263
):
263264
"""
264265
Import data into the REDCap Project
@@ -298,6 +299,8 @@ def import_records(
298299
of imported records by REDCap. If this is set to true, and auto-numbering
299300
for records is enabled for the project, auto-numbering of imported records
300301
will be enabled.
302+
background_process:
303+
Specifies whether to do the import as background process.
301304
302305
Raises:
303306
RedcapError: Bad request made, double check field names and other inputs
@@ -321,6 +324,9 @@ def import_records(
321324
payload["type"] = record_type
322325
payload["dateFormat"] = date_format
323326
payload["forceAutoNumber"] = force_auto_number
327+
if background_process is not None:
328+
# This option is not supported in old REDCap versions (< 14.something)
329+
payload["backgroundProcess"] = int(background_process)
324330

325331
return_type = self._lookup_return_type(
326332
format_type=return_format_type,

tests/unit/test_simple_project.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,68 @@ def test_df_import(simple_project):
615615
assert not "error" in response
616616

617617

618+
def test_import_records_background_process_true(simple_project, mocker):
619+
"""Test that background_process=True passes backgroundProcess=1 in payload"""
620+
mocked_api_call = mocker.patch.object(
621+
simple_project, "_call_api", return_value={"count": 1}
622+
)
623+
624+
data = [{"record_id": "1", "test": "value"}]
625+
simple_project.import_records(data, background_process=True)
626+
627+
args, _ = mocked_api_call.call_args
628+
payload = args[0]
629+
630+
assert "backgroundProcess" in payload
631+
assert payload["backgroundProcess"] == 1
632+
633+
634+
def test_import_records_background_process_false(simple_project, mocker):
635+
"""Test that background_process=False passes backgroundProcess=0 in payload"""
636+
mocked_api_call = mocker.patch.object(
637+
simple_project, "_call_api", return_value={"count": 1}
638+
)
639+
640+
data = [{"record_id": "1", "test": "value"}]
641+
simple_project.import_records(data, background_process=False)
642+
643+
args, _ = mocked_api_call.call_args
644+
payload = args[0]
645+
646+
assert "backgroundProcess" in payload
647+
assert payload["backgroundProcess"] == 0
648+
649+
650+
def test_import_records_background_process_none(simple_project, mocker):
651+
"""Test that background_process=None does not add backgroundProcess to payload"""
652+
mocked_api_call = mocker.patch.object(
653+
simple_project, "_call_api", return_value={"count": 1}
654+
)
655+
656+
data = [{"record_id": "1", "test": "value"}]
657+
simple_project.import_records(data, background_process=None)
658+
659+
args, _ = mocked_api_call.call_args
660+
payload = args[0]
661+
662+
assert "backgroundProcess" not in payload
663+
664+
665+
def test_import_records_background_process_default(simple_project, mocker):
666+
"""Test that not specifying background_process does not add it to payload"""
667+
mocked_api_call = mocker.patch.object(
668+
simple_project, "_call_api", return_value={"count": 1}
669+
)
670+
671+
data = [{"record_id": "1", "test": "value"}]
672+
simple_project.import_records(data)
673+
674+
args, _ = mocked_api_call.call_args
675+
payload = args[0]
676+
677+
assert "backgroundProcess" not in payload
678+
679+
618680
def test_reports_json_export(simple_project):
619681
report = simple_project.export_report(report_id="1")
620682

0 commit comments

Comments
 (0)