Skip to content

Commit

Permalink
Added test and updated request methods
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed Feb 12, 2024
1 parent ff6735b commit c9fe461
Show file tree
Hide file tree
Showing 14 changed files with 502 additions and 158 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.10"]
python-version: ["3.11"]
os: [windows-latest]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.8", "3.11"]
python-version: ["3.8", "3.12"]
os: [ubuntu-20.04]

steps:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ run-coverage:
coverage run -m pytest

html-report:
coverage html
coverage html --omit="*/test*"

open-coverage:
cd htmlcov && google-chrome index.html
Expand Down
8 changes: 4 additions & 4 deletions pephubclient/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from pydantic import BaseModel, field_validator

# PEPHUB_BASE_URL = os.getenv(
# "PEPHUB_BASE_URL", default="https://pephub-api.databio.org/"
# )
PEPHUB_BASE_URL = "http://0.0.0.0:8000/"
PEPHUB_BASE_URL = os.getenv(
"PEPHUB_BASE_URL", default="https://pephub-api.databio.org/"
)
# PEPHUB_BASE_URL = "http://0.0.0.0:8000/"
PEPHUB_PEP_API_BASE_URL = f"{PEPHUB_BASE_URL}api/v1/projects/"
PEPHUB_PEP_SEARCH_URL = f"{PEPHUB_BASE_URL}api/v1/namespaces/{{namespace}}/projects"
PEPHUB_PUSH_URL = f"{PEPHUB_BASE_URL}api/v1/namespaces/{{namespace}}/projects/json"
Expand Down
1 change: 0 additions & 1 deletion pephubclient/files_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import yaml
import zipfile

from pephubclient.constants import RegistryPath
from pephubclient.exceptions import PEPExistsError


Expand Down
5 changes: 4 additions & 1 deletion pephubclient/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from typing import Optional, List
from typing import Optional, List, Union

from pydantic import BaseModel, Field, field_validator, ConfigDict
from peppy.const import CONFIG_KEY, SUBSAMPLE_RAW_LIST_KEY, SAMPLE_RAW_DICT_KEY
Expand Down Expand Up @@ -43,6 +43,9 @@ class ProjectAnnotationModel(BaseModel):
submission_date: datetime.datetime
digest: str
pep_schema: str
pop: bool = False
stars_number: Optional[int] = 0
forked_from: Optional[Union[str, None]] = None


class SearchReturnModel(BaseModel):
Expand Down
16 changes: 9 additions & 7 deletions pephubclient/modules/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ def get(
response = self.send_request(
method="GET", url=url, headers=self.parse_header(self.__jwt_data)
)
if response.status_code != ResponseStatusCodes.OK:
if response.status_code == ResponseStatusCodes.OK:
return self.decode_response(response, output_json=True)
if response.status_code == ResponseStatusCodes.NOT_EXIST:
raise ResponseError("Sample does not exist.")
elif response.status_code == ResponseStatusCodes.INTERNAL_ERROR:
raise ResponseError("Internal server error. Unexpected return value.")
else:
raise ResponseError(
f"Sample does not exist, or Internal server error occurred."
f"Unexpected return value. Error: {response.status_code}"
)
else:
return self.decode_response(response, output_json=True)

def create(
self,
Expand Down Expand Up @@ -93,9 +97,7 @@ def create(
return None

elif response.status_code == ResponseStatusCodes.NOT_EXIST:
raise ResponseError(
f"Project '{namespace}/{name}:{tag}' does not exist. Error: {response.status_code}"
)
raise ResponseError(f"Project '{namespace}/{name}:{tag}' does not exist.")
elif response.status_code == ResponseStatusCodes.CONFLICT:
raise ResponseError(
f"Sample '{sample_name}' already exists. Set overwrite to True to overwrite sample."
Expand Down
4 changes: 3 additions & 1 deletion pephubclient/modules/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ResponseStatusCodes,
)
from pephubclient.exceptions import ResponseError
from pephubclient.models import ProjectDict


class PEPHubView(RequestManager):
Expand Down Expand Up @@ -52,6 +53,7 @@ def get(
output = self.decode_response(response, output_json=True)
if raw:
return output
output = ProjectDict(**output).model_dump(by_alias=True)
return peppy.Project.from_dict(output)
elif response.status_code == ResponseStatusCodes.NOT_EXIST:
raise ResponseError("View does not exist, or you are unauthorized.")
Expand Down Expand Up @@ -213,7 +215,7 @@ def remove_sample(
)
elif response.status_code == ResponseStatusCodes.UNAUTHORIZED:
raise ResponseError(
f"You are unauthorized to remove this sample from the view."
"You are unauthorized to remove this sample from the view."
)
else:
raise ResponseError(
Expand Down
25 changes: 5 additions & 20 deletions pephubclient/pephubclient.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import json
from typing import NoReturn, Optional, Literal
from typing_extensions import deprecated

import peppy
from peppy.const import NAME_KEY
import requests
import urllib3
from pydantic import ValidationError
from ubiquerg import parse_registry_path
Expand Down Expand Up @@ -107,7 +105,6 @@ def load_project(
:param query_param: query parameters used in get request
:return Project: peppy project.
"""
jwt = FilesManager.load_jwt_data_from_file(PATH_TO_FILE_WITH_JWT)
raw_pep = self.load_raw_pep(project_registry_path, query_param)
peppy_project = peppy.Project().from_dict(raw_pep)
return peppy_project
Expand Down Expand Up @@ -250,11 +247,11 @@ def find_project(
cookies=None,
)
if pephub_response.status_code == ResponseStatusCodes.OK:
decoded_response = self._handle_pephub_response(pephub_response)
decoded_response = self.decode_response(pephub_response, output_json=True)
project_list = []
for project_found in json.loads(decoded_response)["items"]:
for project_found in decoded_response["items"]:
project_list.append(ProjectAnnotationModel(**project_found))
return SearchReturnModel(**json.loads(decoded_response))
return SearchReturnModel(**decoded_response)

@deprecated("This method is deprecated. Use load_raw_pep instead.")
def _load_raw_pep(
Expand Down Expand Up @@ -297,8 +294,8 @@ def load_raw_pep(
cookies=None,
)
if pephub_response.status_code == ResponseStatusCodes.OK:
decoded_response = self._handle_pephub_response(pephub_response)
correct_proj_dict = ProjectDict(**json.loads(decoded_response))
decoded_response = self.decode_response(pephub_response, output_json=True)
correct_proj_dict = ProjectDict(**decoded_response)

# This step is necessary because of this issue: https://github.com/pepkit/pephub/issues/124
return correct_proj_dict.model_dump(by_alias=True)
Expand Down Expand Up @@ -362,15 +359,3 @@ def _build_push_request_url(namespace: str) -> str:
:return: url string
"""
return PEPHUB_PUSH_URL.format(namespace=namespace)

@staticmethod
def _handle_pephub_response(pephub_response: requests.Response):
"""
Check pephub response
"""
decoded_response = PEPHubClient.decode_response(pephub_response)

if pephub_response.status_code != ResponseStatusCodes.OK:
raise ResponseError(message=json.loads(decoded_response).get("detail"))

return decoded_response
Empty file removed requirements/requirements-dev.txt
Empty file.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def read_reqs(reqs_name):
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering :: Bio-Informatics",
],
keywords="project, bioinformatics, metadata",
Expand Down
4 changes: 1 addition & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import pytest

from pephubclient.pephub_oauth.models import InitializeDeviceCodeResponse
Expand Down Expand Up @@ -29,7 +27,7 @@ def test_raw_pep_return():
{"time": "0", "file_path": "source1", "sample_name": "frog_0h"},
],
}
return json.dumps(sample_prj)
return sample_prj


@pytest.fixture
Expand Down
101 changes: 101 additions & 0 deletions tests/test_manual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from pephubclient.pephubclient import PEPHubClient
import pytest


@pytest.mark.skip(reason="Manual test")
class TestViewsManual:

def test_get(self):
ff = PEPHubClient().view.get(
"databio",
"bedset1",
"default",
"test_view",
)
print(ff)

def test_create(self):
PEPHubClient().view.create(
"databio",
"bedset1",
"default",
"test_view",
sample_list=["orange", "grape1", "apple1"],
)

def test_delete(self):
PEPHubClient().view.delete(
"databio",
"bedset1",
"default",
"test_view",
)

def test_add_sample(self):
PEPHubClient().view.add_sample(
"databio",
"bedset1",
"default",
"test_view",
"name",
)

def test_delete_sample(self):
PEPHubClient().view.remove_sample(
"databio",
"bedset1",
"default",
"test_view",
"name",
)


@pytest.mark.skip(reason="Manual test")
class TestSamplesManual:
def test_manual(self):
ff = PEPHubClient().sample.get(
"databio",
"bedset1",
"default",
"grape1",
)
ff

def test_update(self):
ff = PEPHubClient().sample.get(
"databio",
"bedset1",
"default",
"newf",
)
ff.update({"shefflab": "test1"})
ff["sample_type"] = "new_type"
PEPHubClient().sample.update(
"databio",
"bedset1",
"default",
"newf",
sample_dict=ff,
)

def test_add(self):
ff = {
"genome": "phc_test1",
"sample_type": "phc_test",
}
PEPHubClient().sample.create(
"databio",
"bedset1",
"default",
"new_2222",
overwrite=False,
sample_dict=ff,
)

def test_delete(self):
PEPHubClient().sample.remove(
"databio",
"bedset1",
"default",
"new_2222",
)
Loading

0 comments on commit c9fe461

Please sign in to comment.