-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom option --upload-only to pytest. The test will only run when that argument is passed. Skipped otherwise. Minor changes to the test_parameters file.
- Loading branch information
Showing
8 changed files
with
94 additions
and
19 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
data/ | ||
tests/test_local_parameters.json | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
|
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
File renamed without changes.
File renamed without changes.
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
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,23 +1,66 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
|
||
import mdai | ||
import pytest | ||
|
||
_current_dir = Path(__file__).parent if "__file__" in locals() else Path(".") | ||
from mdai_utils.common import get_mdai_access_token | ||
|
||
|
||
def pytest_addoption(parser): | ||
"""Add a command line option to pytest.""" | ||
parser.addoption( | ||
"--upload-only", | ||
action="store_true", | ||
default=False, | ||
help="run only tests marked as upload_only", | ||
) | ||
|
||
|
||
# Register the custom mark to avoid pytest warnings | ||
def pytest_configure(config): | ||
config.addinivalue_line( | ||
"markers", "upload_only: Mark test to run only when --upload-only is provided" | ||
) | ||
|
||
|
||
@pytest.hookimpl(tryfirst=True) | ||
def pytest_runtest_setup(item): | ||
"""Hook that runs before a test is set up.""" | ||
is_upload_only_test = "upload_only" in item.keywords | ||
upload_only_mode = item.config.getoption("--upload-only") | ||
|
||
if upload_only_mode: | ||
if not is_upload_only_test: | ||
pytest.skip("Skipping non-upload tests in upload-only mode") | ||
else: | ||
if is_upload_only_test: | ||
pytest.skip("Skipping upload-only tests in standard mode") | ||
|
||
|
||
_current_dir = Path(__file__).parent | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def mdai_setup(parameters_file=_current_dir / "test_parameters.json", input_token=None): | ||
token = input_token or os.getenv("MDAI_TOKEN", "") | ||
if token == "": | ||
raise ValueError( | ||
"Please set the MDAI_TOKEN environment variable with your MDAI credentials." | ||
def mdai_setup( | ||
parameters_file=_current_dir / "test_local_parameters.json", input_token=None | ||
): | ||
# Check if the parameters file exists | ||
print(f"Parameters file: {parameters_file}") | ||
if not parameters_file.exists(): | ||
existing_parameters_file = _current_dir / "test_parameters.json" | ||
raise FileNotFoundError( | ||
f"Parameters file {parameters_file} not found. Please create one, using {existing_parameters_file} as a template." | ||
) | ||
token = input_token or get_mdai_access_token() | ||
with open(parameters_file) as f: | ||
parameters = json.load(f) | ||
|
||
fixture_dir = _current_dir / "fixtures" | ||
mdai_domain = parameters.get("mdai_domain") or "md.ai" | ||
mdai_client = mdai.Client(domain=mdai_domain, access_token=token) | ||
return {"mdai_client": mdai_client, "parameters": parameters} | ||
return { | ||
"mdai_client": mdai_client, | ||
"parameters": parameters, | ||
"fixture_dir": fixture_dir, | ||
} |
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,17 +1,15 @@ | ||
{ | ||
"mdai_domain": "uab.md.ai", | ||
"mdai_domain": "md.ai", | ||
"mdai_token_env_variable": "MDAI_TOKEN", | ||
"mdai_project_id": "L1NprBvP", | ||
"mdai_dataset_id": "D_odXMLm", | ||
"mdai_label_group_id": "G_P914xZ", | ||
"mdai_label_group_id": "G_2Jy2yZ", | ||
"mdai_label_ids": { | ||
"heart": "L_W4Xx64", | ||
"inner-chest": "L_npal0d" | ||
"mylabel": "L_W4XQKd" | ||
}, | ||
"mdai_annotations_only": true, | ||
"mdai_no_fixing_metadata": false, | ||
"labels": [ | ||
"heart", | ||
"inner-chest" | ||
"mylabel", | ||
] | ||
} |
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,7 +1,29 @@ | ||
# from mdai_utils.upload import upload_data_annotation_slice | ||
import pytest | ||
|
||
from mdai_utils.upload_dataset import upload_dataset | ||
|
||
def test_upload_mask_annotation(mdai_setup): | ||
|
||
def test_pytest_fixture(mdai_setup): | ||
mdai_parameters = mdai_setup["parameters"] | ||
mdai_project_id = mdai_parameters.get("mdai_project_id") | ||
assert mdai_project_id is not None | ||
|
||
|
||
@pytest.mark.upload_only( | ||
reason="Only need to upload once. run pytest tests with --upload-only to run it." | ||
) | ||
def test_upload_dataset(mdai_setup): | ||
mdai_parameters = mdai_setup["parameters"] | ||
mdai_dataset_id = mdai_parameters.get("mdai_dataset_id") | ||
fixture_dir = mdai_setup["fixture_dir"] | ||
dicom_dir = fixture_dir / "humanct_0002_1000_1004" | ||
assert fixture_dir.exists() | ||
completed_process = upload_dataset(mdai_dataset_id, dicom_dir) | ||
process_message = completed_process.stdout.strip() | ||
print(process_message) | ||
# Check the status of subprocess | ||
assert completed_process.returncode == 0 | ||
|
||
|
||
def test_upload_annotation(mdai_setup): | ||
mdai_setup["parameters"] |