|
13 | 13 | from nimare.tests.utils import get_test_data_path |
14 | 14 | from nimare.utils import get_template |
15 | 15 |
|
| 16 | +NEUROSTORE_STUDYSET_ID = "qm2PZBqNsaZK" |
| 17 | +NEUROSTORE_ANNOTATION_ID = "hbTQJVL2kAb8" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="module") |
| 21 | +def vcr_cassette_dir(): |
| 22 | + """Store test_io cassettes in a stable directory.""" |
| 23 | + return os.path.join(os.path.dirname(__file__), "cassettes", "test_io") |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="module") |
| 27 | +def vcr_config(): |
| 28 | + """Keep Neurostore cassettes stable and free of credentials.""" |
| 29 | + return { |
| 30 | + "filter_headers": ["authorization"], |
| 31 | + "decode_compressed_response": True, |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.vcr |
| 36 | +def test_fetch_neurostore_studyset(): |
| 37 | + """Download a nested Neurostore studyset into a NiMARE Studyset.""" |
| 38 | + studyset = io.fetch_neurostore_studyset(NEUROSTORE_STUDYSET_ID, target="ale_2mm") |
| 39 | + |
| 40 | + assert isinstance(studyset, Studyset) |
| 41 | + assert studyset.id == NEUROSTORE_STUDYSET_ID |
| 42 | + assert studyset.space == "ale_2mm" |
| 43 | + assert len(studyset.studies) > 0 |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.vcr |
| 47 | +def test_fetch_neurostore_studyset_with_annotation(): |
| 48 | + """Download a nested Neurostore studyset and attach a regular annotation.""" |
| 49 | + studyset = io.fetch_neurostore_studyset( |
| 50 | + NEUROSTORE_STUDYSET_ID, |
| 51 | + annotation_id=NEUROSTORE_ANNOTATION_ID, |
| 52 | + ) |
| 53 | + |
| 54 | + assert isinstance(studyset, Studyset) |
| 55 | + assert studyset.id == NEUROSTORE_STUDYSET_ID |
| 56 | + assert len(studyset.annotations) == 1 |
| 57 | + assert studyset.annotations[0].id == NEUROSTORE_ANNOTATION_ID |
| 58 | + assert "included" in studyset.annotations_df.columns |
| 59 | + assert studyset.annotations_df["included"].any() |
| 60 | + |
| 61 | + |
| 62 | +def test_fetch_neurostore_studyset_wraps_api_errors(monkeypatch): |
| 63 | + """Neurostore request failures should report the resource that failed.""" |
| 64 | + import neurostore_sdk |
| 65 | + |
| 66 | + class FailingStoreApi: |
| 67 | + def studysets_id_get(self, id, nested=True): |
| 68 | + raise neurostore_sdk.ApiException(status=404, reason="Not Found") |
| 69 | + |
| 70 | + monkeypatch.setattr(neurostore_sdk, "StoreApi", FailingStoreApi) |
| 71 | + |
| 72 | + with pytest.raises(ValueError, match="Failed to download Neurostore studyset 'bad-id'"): |
| 73 | + io.fetch_neurostore_studyset("bad-id") |
| 74 | + |
| 75 | + |
| 76 | +def test_fetch_neurostore_studyset_wraps_annotation_api_errors(monkeypatch): |
| 77 | + """Neurostore annotation request failures should report the annotation ID.""" |
| 78 | + import neurostore_sdk |
| 79 | + |
| 80 | + class FailingAnnotationStoreApi: |
| 81 | + def studysets_id_get(self, id, nested=True): |
| 82 | + return {"id": "ok-id", "studies": []} |
| 83 | + |
| 84 | + def annotations_id_get(self, id): |
| 85 | + raise neurostore_sdk.ApiException(status=404, reason="Not Found") |
| 86 | + |
| 87 | + monkeypatch.setattr(neurostore_sdk, "StoreApi", FailingAnnotationStoreApi) |
| 88 | + |
| 89 | + with pytest.raises( |
| 90 | + ValueError, |
| 91 | + match="Failed to download Neurostore annotation 'bad-annotation'", |
| 92 | + ): |
| 93 | + io.fetch_neurostore_studyset("ok-id", annotation_id="bad-annotation") |
| 94 | + |
16 | 95 |
|
17 | 96 | def test_convert_nimads_to_dataset(example_nimads_studyset, example_nimads_annotation): |
18 | 97 | """Conversion of nimads JSON to nimare dataset.""" |
|
0 commit comments