generated from datalad/datalad-extension-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds tests, sometime rudimentary, for all the functionality. Tests for commands mock API responses and make sure the command succeeds when the response is there.
- Loading branch information
Showing
5 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from unittest.mock import patch | ||
|
||
from datalad.api import export_redcap_form | ||
from datalad.distribution.dataset import Dataset | ||
from datalad_next.tests.utils import ( | ||
assert_status, | ||
eq_, | ||
with_credential, | ||
with_tempfile, | ||
) | ||
|
||
DUMMY_TOKEN = "WTJ3G8XWO9G8V1BB4K8N81KNGRPFJOVL" # needed to pass length assertion | ||
CSV_CONTENT = "foo,bar,baz\nspam,spam,spam" | ||
CREDNAME = "redcap" | ||
|
||
|
||
@with_tempfile | ||
@patch("datalad_redcap.export_form.Records.export_records", return_value=CSV_CONTENT) | ||
@with_credential(CREDNAME, type="token", secret=DUMMY_TOKEN) | ||
def test_export_writes_file(ds_path=None, mocker=None): | ||
ds = Dataset(ds_path).create(result_renderer="disabled") | ||
fname = "form.csv" | ||
|
||
res = export_redcap_form( | ||
url="https://www.example.com/api/", | ||
forms=["foo"], | ||
outfile=fname, | ||
dataset=ds, | ||
credential=CREDNAME, | ||
) | ||
|
||
# check that the command returned ok | ||
assert_status("ok", res) | ||
|
||
# check that the file was created and left in clean state | ||
eq_(ds.status(fname, return_type="item-or-list").get("state"), "clean") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""Tests related to parameter handling | ||
These test for ValueError being raised in example situations related | ||
to parameter validation, and should help ensure that parameter | ||
validation works as expected. The export_records command is used as an | ||
example command, and we still mock the api calls to let the command | ||
run if validation doesn't complain. | ||
""" | ||
|
||
import pytest | ||
from unittest.mock import patch | ||
|
||
from datalad.api import export_redcap_form | ||
from datalad.distribution.dataset import Dataset | ||
|
||
from datalad_next.tests.utils import ( | ||
with_credential, | ||
with_tempfile, | ||
) | ||
from datalad_next.utils import chpwd | ||
|
||
DUMMY_TOKEN = "WTJ3G8XWO9G8V1BB4K8N81KNGRPFJOVL" # needed to pass length assertion | ||
CSV_CONTENT = "foo,bar,baz\nspam,spam,spam" | ||
CREDNAME = "redcap" | ||
|
||
|
||
@with_tempfile | ||
@patch("datalad_redcap.export_form.Records.export_records", return_value=CSV_CONTENT) | ||
@with_credential(CREDNAME, type="token", secret=DUMMY_TOKEN) | ||
def test_url_rejected(ds_path=None, mocker=None): | ||
"""Test that bad-form urls are rejected by validation""" | ||
ds = Dataset(ds_path).create(result_renderer="disabled") | ||
with pytest.raises(ValueError): | ||
export_redcap_form( | ||
url="example.com", # missing scheme, path | ||
forms=["foo"], | ||
outfile="foo.csv", | ||
dataset=ds, | ||
credential=CREDNAME, | ||
) | ||
|
||
|
||
@with_tempfile | ||
@with_credential(CREDNAME, type="token", secret=DUMMY_TOKEN) | ||
@patch("datalad_redcap.export_form.Records.export_records", return_value=CSV_CONTENT) | ||
def test_dataset_not_found(path=None, mocker=None): | ||
"""Test that nonexistent dataset is rejected by validation""" | ||
|
||
# explicit path that isn't a dataset | ||
with pytest.raises(ValueError): | ||
export_redcap_form( | ||
url="https://example.com/api", | ||
forms=["foo"], | ||
outfile="foo", | ||
dataset=path, | ||
credential=CREDNAME, | ||
) | ||
|
||
# no path given, pwd is not a dataset | ||
with chpwd(path, mkdir=True): | ||
with pytest.raises(ValueError): | ||
export_redcap_form( | ||
url="https://example.com/api", | ||
forms=["foo"], | ||
outfile="foo", | ||
credential=CREDNAME, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from unittest.mock import patch | ||
|
||
from datalad.api import redcap_query | ||
from datalad_next.tests.utils import ( | ||
assert_result_count, | ||
with_credential, | ||
) | ||
|
||
DUMMY_URL = "https://www.example.com/api/" | ||
DUMMY_TOKEN = "WTJ3G8XWO9G8V1BB4K8N81KNGRPFJOVL" # needed to pass length assertion | ||
JSON_CONTENT = {"foo": "bar"} | ||
CREDNAME = "redcap" | ||
|
||
|
||
@patch( | ||
"datalad_redcap.query.MyInstruments.export_instruments", return_value=JSON_CONTENT | ||
) | ||
@with_credential(CREDNAME, type="token", secret=DUMMY_TOKEN) | ||
def test_redcap_query_has_result(mocker=None): | ||
assert_result_count( | ||
redcap_query(url=DUMMY_URL, credential=CREDNAME, result_renderer="disabled"), 1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
from datalad.tests.utils_pytest import assert_result_count | ||
|
||
|
||
def test_register(): | ||
import datalad.api as da | ||
assert hasattr(da, 'export_redcap_form') | ||
# assert_result_count( | ||
# da.hello_cmd(), | ||
# 1, | ||
# action='demo') | ||
|
||
assert hasattr(da, "export_redcap_form") | ||
assert hasattr(da, "export_redcap_report") | ||
assert hasattr(da, "redcap_query") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pathlib import Path | ||
|
||
from datalad.distribution.dataset import Dataset | ||
from datalad_next.tests.utils import with_tempfile | ||
|
||
from datalad_redcap.utils import check_ok_to_edit | ||
|
||
|
||
@with_tempfile | ||
def test_check_ok_to_edit(path=None): | ||
"""Tests whether location/state is correctly recognized""" | ||
basedir = Path(path) | ||
ds = Dataset(basedir / "ds").create(result_renderer="disabled") | ||
subds = ds.create("subds", result_renderer="disabled") | ||
|
||
outside = basedir / "file_outside" | ||
inside = basedir / "ds" / "ds_file" | ||
below = basedir / "ds" / "subds" / "subds_file" | ||
|
||
outside.write_text("dummy") | ||
inside.write_text("dummy") | ||
below.write_text("dummy") | ||
|
||
ds.save(recursive=True) | ||
|
||
# outside is not ok | ||
ok2ed, _ = check_ok_to_edit(outside, ds) | ||
assert not ok2ed | ||
|
||
# inside is ok | ||
ok2ed, _ = check_ok_to_edit(inside, ds) | ||
assert ok2ed | ||
|
||
# subdataset is not ok | ||
ok2ed, _ = check_ok_to_edit(below, ds) | ||
assert not ok2ed | ||
|
||
# file with unsaved changes is not ok | ||
ds.unlock(inside) | ||
inside.write_text("new dummy") | ||
ok2ed, _ = check_ok_to_edit(inside, ds) | ||
assert not ok2ed |