From 49ab20b374424f5bc2a75e500532cceb0d70dfa3 Mon Sep 17 00:00:00 2001 From: dapineyro Date: Tue, 22 Oct 2024 16:32:45 +0200 Subject: [PATCH] pytest --- README.md | 1 + cloudos/__init__.py | 2 +- cloudos/__main__.py | 2 +- cloudos/clos.py | 5 ++- environment.yml | 1 + requirements.txt | 3 +- tests/test_clos/test_is_module.py | 41 +++++++++++++++++++ ...process_workflow_list_initial_request.json | 4 +- .../process_workflow_list_results.csv | 8 ++-- 9 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 tests/test_clos/test_is_module.py diff --git a/README.md b/README.md index 2d610b67..ed26b805 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ The package requires Python >= 3.7 and the following python packages: ``` click>=8.0.1 pandas>=1.3.4 +numpy==1.26.4 requests>=2.26.0 ``` diff --git a/cloudos/__init__.py b/cloudos/__init__.py index 35a8dd2c..779cf800 100755 --- a/cloudos/__init__.py +++ b/cloudos/__init__.py @@ -8,4 +8,4 @@ from .clos import Cloudos from ._version import __version__ -__all__ = ['jobs', 'utils', 'clos', 'cohorts'] +__all__ = ['jobs', 'utils', 'clos', 'queue'] diff --git a/cloudos/__main__.py b/cloudos/__main__.py index 6bd585f0..4321154f 100644 --- a/cloudos/__main__.py +++ b/cloudos/__main__.py @@ -345,7 +345,7 @@ def run(apikey, print('\t' + str(j)) print('\t...Sending job to CloudOS\n') if is_module: - if job_queue != None: + if job_queue is not None: print(f'\t\t[Message] Job queue "{job_queue}" was specified but a the ' + 'workflow is a platform workflow that uses a fixed queue provided ' + f'by CloudOS. Ignoring job queue "{job_queue}" and using the ' + diff --git a/cloudos/clos.py b/cloudos/clos.py index 4dccb753..22e9362c 100644 --- a/cloudos/clos.py +++ b/cloudos/clos.py @@ -418,6 +418,7 @@ def process_workflow_list(r, all_fields=False): """ COLUMNS = ['_id', 'name', + 'isModule', 'archived.status', 'mainFile', 'workflowType', @@ -495,8 +496,8 @@ def is_module(self, workflow_name, workspace_id, verify=True): if len(is_module) == 0: raise ValueError(f'No workflow found with name: {workflow_name}') if len(is_module) > 1: - raise ValueError(f'More than one workflow type detected for {workflow_name}: {wt}') - return is_module.values[0].upper() == 'TRUE' + raise ValueError(f'More than one workflow found with name: {workflow_name}') + return is_module.values[0] def get_project_list(self, workspace_id, verify=True): """Get all the project from a CloudOS workspace. diff --git a/environment.yml b/environment.yml index 99049dd7..e84c89cf 100644 --- a/environment.yml +++ b/environment.yml @@ -8,3 +8,4 @@ dependencies: - requests - click - pandas + - numpy==1.26.4 diff --git a/requirements.txt b/requirements.txt index 92b63a5d..4a07ed1f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ click>=8.0.1 requests>=2.26.0 requests-mock>=1.9.3 pandas>=1.3.4 +numpy==1.26.4 pytest>=6.2.5 responses>=0.21.0 -mock>=3.0.5 \ No newline at end of file +mock>=3.0.5 diff --git a/tests/test_clos/test_is_module.py b/tests/test_clos/test_is_module.py new file mode 100644 index 00000000..2e13bd54 --- /dev/null +++ b/tests/test_clos/test_is_module.py @@ -0,0 +1,41 @@ +"""Pytest for method Cloudos.is_module""" +import mock +import responses +from responses import matchers +from cloudos.clos import Cloudos +from tests.functions_for_pytest import load_json_file + +INPUT = "tests/test_data/process_workflow_list_initial_request.json" +APIKEY = 'vnoiweur89u2ongs' +CLOUDOS_URL = 'http://cloudos.lifebit.ai' +WORKSPACE_ID = 'lv89ufc838sdig' + + +@mock.patch('cloudos.clos', mock.MagicMock()) +@responses.activate +def test_is_module(): + """ + Test 'is_module' to work as intended + API request is mocked and replicated with json files + """ + json_data = load_json_file(INPUT) + params = {"teamId": WORKSPACE_ID, "apikey": APIKEY} + header = { + "Accept": "application/json, text/plain, */*", + "Content-Type": "application/json;charset=UTF-8" + } + search_str = f"teamId={WORKSPACE_ID}&apikey={APIKEY}" + # mock GET method with the .json + responses.add( + responses.GET, + url=f"{CLOUDOS_URL}/api/v1/workflows?{search_str}", + body=json_data, + headers=header, + match=[matchers.query_param_matcher(params)], + status=200) + # start cloudOS service + clos = Cloudos(apikey=APIKEY, cromwell_token=None, cloudos_url=CLOUDOS_URL) + # get mock response + response = clos.is_module(workspace_id=WORKSPACE_ID, + workflow_name="multiqc") + assert response diff --git a/tests/test_data/process_workflow_list_initial_request.json b/tests/test_data/process_workflow_list_initial_request.json index cce5d74f..ea2bc615 100644 --- a/tests/test_data/process_workflow_list_initial_request.json +++ b/tests/test_data/process_workflow_list_initial_request.json @@ -292,7 +292,7 @@ "isPredefined": false, "isCurated": false, "isFeatured": false, - "isModule": false, + "isModule": true, "isPublic": false, "priceAmount": 0, "priceUnit": "PER_SAMPLE", @@ -309,4 +309,4 @@ "workflowType": "docker", "containerName": "ewels/multiqc" } -] \ No newline at end of file +] diff --git a/tests/test_data/process_workflow_list_results.csv b/tests/test_data/process_workflow_list_results.csv index c7877ea8..0c83ea56 100644 --- a/tests/test_data/process_workflow_list_results.csv +++ b/tests/test_data/process_workflow_list_results.csv @@ -1,4 +1,4 @@ -_id,name,archived.status,mainFile,workflowType,repository.name,repository.platform,repository.url,repository.isPrivate -XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX -XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX -XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX +_id,name,isModule,archived.status,mainFile,workflowType,repository.name,repository.platform,repository.url,repository.isPrivate +XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX +XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX +XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX,XXX