Skip to content

Commit

Permalink
tests: new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Feb 17, 2024
1 parent 8c9470b commit 2e7afc6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/nbiatoolkit/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __init__(
username: str = "nbia_guest",
password: str = "",
client_id: str = "NBIA",
base_url: Union[str, NBIA_ENDPOINTS] = NBIA_ENDPOINTS.BASE_URL,
base_url: Union[str, NBIA_ENDPOINTS] = NBIA_ENDPOINTS.NBIA,
) -> None:
"""
Initialize the OAuth2 class.
Expand All @@ -127,7 +127,7 @@ def __init__(
The password for authentication. Default is an empty string.
client_id : str, optional
The client ID for authentication. Default is "NBIA".
base_url : str or NBIA_ENDPOINTS, optional. Default is NBIA_ENDPOINTS.BASE_URL
base_url : str or NBIA_ENDPOINTS, optional. Default is NBIA_ENDPOINTS.NBIA
"""

Expand Down
28 changes: 5 additions & 23 deletions src/nbiatoolkit/nbia.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,11 @@
__version__ = "0.29.2"


def get_real_ReturnType(return_type: ReturnType):

if return_type == ReturnType.LIST:
return list
elif return_type == ReturnType.DATAFRAME:
return pd.DataFrame


# function that takes a list of dictionaries and returns either a list or a dataframe
def conv_response_list(
response_json: List[dict[Any, Any]], return_type: ReturnType = ReturnType.LIST
) -> List[dict[Any, Any]] | pd.DataFrame:

if isinstance(response_json, bytes):
return response_json

assert isinstance(response_json, list), "The response JSON must be a list"

if return_type == ReturnType.LIST:
Expand All @@ -59,8 +48,6 @@ class NBIAClient:
The default authentication uses the guest account. If you have a username
and password, you can pass them to the constructor.
TODO:: Add docstring
"""

def __init__(
Expand All @@ -79,17 +66,12 @@ def __init__(
self._log.debug("Setting up OAuth2 client... with username %s", username)
self._oauth2_client = OAuth2(username=username, password=password)

try:
self._api_headers = {
"Authorization": f"Bearer {self._oauth2_client.access_token}",
"Content-Type": "application/json",
}
except Exception as e:
self._log.error("Error retrieving access token: %s", e)
self._api_headers = None
raise e
self._api_headers: dict[str, str] = {
"Authorization": f"Bearer {self._oauth2_client.access_token}",
"Content-Type": "application/json",
}

self._base_url: NBIA_ENDPOINTS = NBIA_ENDPOINTS.BASE_URL
self._base_url: NBIA_ENDPOINTS = NBIA_ENDPOINTS.NBIA
self._return_type: ReturnType = (
return_type
if isinstance(return_type, ReturnType)
Expand Down
4 changes: 2 additions & 2 deletions src/nbiatoolkit/utils/nbia_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class NBIA_ENDPOINTS(Enum):
This enum class defines the NBIA endpoints used in the NBIA toolkit.
"""

BASE_URL = "https://services.cancerimagingarchive.net/nbia-api/services/"
NLST_URL = "https://nlst.cancerimagingarchive.net/nbia-api/services/"
NBIA = "https://services.cancerimagingarchive.net/nbia-api/services/"
NLST = "https://nlst.cancerimagingarchive.net/nbia-api/services/"
LOGOUT_URL = "https://services.cancerimagingarchive.net/nbia-api/logout"

GET_COLLECTIONS = "v2/getCollectionValues"
Expand Down
6 changes: 0 additions & 6 deletions tests/deprecated_test_nbia.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ def nbia_client():
return nbia


@pytest.fixture(scope="session")
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()
Expand Down
18 changes: 17 additions & 1 deletion tests/test_new_query_functionality.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from src.nbiatoolkit import NBIAClient

from src.nbiatoolkit.utils import *
import pandas as pd


Expand All @@ -11,6 +11,12 @@ def nbia_client():
return nbia_client


@pytest.fixture(scope="session")
def nbia_client_bad_username():
nbia = NBIAClient(username="bad_username", password="bad_password")
return nbia


@pytest.fixture(scope="session")
def tcga_collections(nbia_client):
tcga_collections = nbia_client.getCollections(prefix="TCGA")
Expand All @@ -25,6 +31,16 @@ def tcga_collection_df(nbia_client):
return tcga_collection_df


def test_change_base_url(nbia_client):
nbia_client.base_url = NBIA_ENDPOINTS.NLST
assert nbia_client.base_url == NBIA_ENDPOINTS.NLST


def test_failed_NBIA_CLIENT():
with pytest.raises(Exception) as e:
nbia = NBIAClient(username="bad_username", password="bad_password")


def test_tcga_collection(tcga_collections):
assert isinstance(tcga_collections, list)
assert len(tcga_collections) > 1
Expand Down

0 comments on commit 2e7afc6

Please sign in to comment.