Skip to content

Commit

Permalink
refactor nbia client and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jermiah committed Nov 25, 2023
1 parent 709551d commit b199a83
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
8 changes: 7 additions & 1 deletion driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
PASSWORD = "q6mJyLD8cPeGTwg!"


client = NBIAClient(username=USERNAME, password=PASSWORD)
client = NBIAClient(
username=USERNAME,
password=PASSWORD,
log_level="DEBUG")

series = client.getSeries(Collection="RADCURE")

seriesList = [_["SeriesInstanceUID"] for _ in series]
seriesList = seriesList[0:5]




def download(series) -> bool:
response = client.downloadSeries(
SeriesInstanceUID = series,
Expand Down
7 changes: 5 additions & 2 deletions src/nbiatoolkit/nbia.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def __init__(self,

# Setup logger
self.logger = setup_logger(
name = "NBIAClient", console_logging=True, log_level=log_level)
name = "NBIAClient",
console_logging=True,
log_level=log_level,
log_file = None)

# Setup OAuth2 client
self.logger.debug("Setting up OAuth2 client... with username %s", username)
Expand All @@ -33,7 +36,7 @@ def query_api(self, endpoint: NBIA_ENDPOINTS, params: dict = {}) -> dict:
query_url = base_url + endpoint.value

self.logger.debug("Querying API endpoint: %s", query_url)
self.logger.debug("API headers: %s", (self._createDebugURL(endpoint, params)))
# self.logger.debug("API headers: %s", (self._createDebugURL(endpoint, params)))

try:
response = requests.get(
Expand Down
58 changes: 47 additions & 11 deletions tests/test_nbia.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ def nbia_client_bad_username():
nbia = NBIAClient(username="bad_username", password="bad_password")
return nbia


@pytest.fixture(scope="session")
def nbia_collections(nbia_client):
collections = nbia_client.getCollections()
return collections

@pytest.fixture(scope="session")
def nbia_patients(nbia_client, nbia_collections):
patients = nbia_client.getPatients(
collection=nbia_collections[0], modality = "CT")
return patients

def test_nbiaclient_access_token(nbia_client):
assert nbia_client.api_headers is not None

def test_getCollections(nbia_client):
collections = nbia_client.getCollections()
assert isinstance(collections, list)
assert len(collections) > 0
def test_getCollections(nbia_collections):
assert isinstance(nbia_collections, list)
assert len(nbia_collections) > 0

def test_getBodyPartCounts_all(nbia_client):
bodyparts = nbia_client.getBodyPartCounts()
Expand Down Expand Up @@ -48,10 +59,35 @@ def test_getCollectionPatientCount(nbia_client):
assert "PatientCount" in patientCount[0]
assert int(patientCount[0]["PatientCount"]) > 0

def test_getPatients(nbia_client):
patientList = nbia_client.getPatients("LIDC-IDRI", "CT")
assert isinstance(patientList, list)
assert len(patientList) > 0
assert isinstance(patientList[0], str)
assert len(patientList[0]) > 0

def test_getPatients(nbia_patients):
assert isinstance(nbia_patients, list)
assert len(nbia_patients) > 0
assert isinstance(nbia_patients[0], str)
assert len(nbia_patients[0]) > 0

@pytest.mark.getSeries
def test_getSeries(nbia_client, nbia_collections, nbia_patients):
seriesList = nbia_client.getSeries(
Collection=nbia_collections[0],
PatientID=nbia_patients[0],
Modality="CT"
)
assert seriesList is not None
assert isinstance(seriesList, list)
assert len(seriesList) > 0
assert isinstance(seriesList[0], dict)

@pytest.mark.getSeries
def test_fail_getSeries(nbia_client, nbia_collections, nbia_patients):
with pytest.raises(Exception):
seriesList = nbia_client.getSeries(
Collection=nbia_collections[0],
PatientID=nbia_patients[0],
Modality="CT",
SeriesInstanceUID="bad_series_uid"
)
assert seriesList is not None
assert isinstance(seriesList, list)
assert len(seriesList) > 0
assert isinstance(seriesList[0], dict)

0 comments on commit b199a83

Please sign in to comment.