-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from aanil/fix_git
Update Readme: first draft, log error instead of raising exception on failed upload, cleanup tests a bit
- Loading branch information
Showing
7 changed files
with
115 additions
and
75 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
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
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 |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
|
||
from datetime import date, timedelta | ||
|
||
from daily_read import ngi_data, config | ||
from daily_read import ngi_data, config, order_portal | ||
|
||
dummy_order_open = { | ||
"orderer": "[email protected]", | ||
|
@@ -376,30 +376,26 @@ def create_report_path(tmp_path): | |
return create_report_path | ||
|
||
|
||
def mocked_requests_get(*args, **kwargs): | ||
class MockResponse: | ||
def __init__(self, json_data, status_code): | ||
self.json_data = json_data | ||
self.status_code = status_code | ||
@pytest.fixture(autouse=True) | ||
def mocked_requests_get(monkeypatch): | ||
"""order_portal.OrderPortal._get() mocked to return {'items': [order list]}.""" | ||
|
||
class MockResponse: | ||
def json(self): | ||
return self.json_data | ||
|
||
if args[0] == "api/v1/orders": | ||
return MockResponse( | ||
{ | ||
return { | ||
"items": [ | ||
order_portal_resp_order_processing, | ||
order_portal_resp_order_closed, | ||
order_portal_resp_order_processing_mult_reports, | ||
order_portal_resp_order_processing_single_report, | ||
order_portal_resp_order_processing_to_aborted, | ||
] | ||
}, | ||
200, | ||
) | ||
} | ||
|
||
def mock_get(*args, **kwargs): | ||
return MockResponse() | ||
|
||
return MockResponse(None, 404) | ||
monkeypatch.setattr(order_portal.OrderPortal, "_get", mock_get) | ||
|
||
|
||
@pytest.fixture | ||
|
@@ -414,10 +410,7 @@ def mocked_statusdb_conn_rows(): | |
"order_year": "2023", | ||
"project_id": "P123457", | ||
"project_name": "D.Dummysson_23_03", | ||
"proj_dates": { | ||
"2023-06-15": ["Samples Received"], | ||
"2023-06-28": ["Reception Control finished", "Library QC finished"], | ||
}, | ||
"proj_dates": dummy_order_open["project_dates"], | ||
"status": "Ongoing", | ||
}, | ||
) | ||
|
@@ -430,12 +423,7 @@ def mocked_statusdb_conn_rows(): | |
"order_year": "2023", | ||
"project_id": "P123458", | ||
"project_name": "T.Dummysson_23_04", | ||
"proj_dates": { | ||
"2023-06-15": ["Samples Received"], | ||
"2023-06-28": ["Reception Control finished", "Library QC finished"], | ||
"2023-07-28": ["All Samples Sequenced"], | ||
"2023-07-29": ["All Raw data Delivered"], | ||
}, | ||
"proj_dates": dummy_order_closed["project_dates"], | ||
"status": "Closed", | ||
}, | ||
) | ||
|
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
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,12 +1,37 @@ | ||
import base64 | ||
import logging | ||
import pytest | ||
|
||
from conftest import mocked_requests_get | ||
from unittest import mock | ||
|
||
from daily_read import order_portal, config, ngi_data | ||
|
||
|
||
def test_get_and_process_orders_open_upload_fail(data_repo_full, mock_project_data_record, caplog): | ||
"""Test getting and processing an open order and upload to Order portal failing""" | ||
orderer = "[email protected]" | ||
order_id = "NGI123456" | ||
config_values = config.Config() | ||
with mock.patch("daily_read.statusdb.StatusDBSession"): | ||
data_master = ngi_data.ProjectDataMaster(config_values) | ||
|
||
data_master.data = {order_id: mock_project_data_record("open")} | ||
|
||
op = order_portal.OrderPortal(config_values, data_master) | ||
op.get_orders(orderer=orderer) | ||
|
||
assert op.all_orders[0]["identifier"] == order_id | ||
modified_orders = op.process_orders(config_values.STATUS_PRIORITY_REV) | ||
assert modified_orders[orderer]["projects"]["Library QC finished"][0] == data_master.data[order_id] | ||
with mock.patch("daily_read.order_portal.requests.post") as mock_post: | ||
mock_post.return_value.status_code = 404 | ||
uploaded = op.upload_report_to_order_portal( | ||
"<html>test data</html>", modified_orders[orderer]["projects"]["Library QC finished"][0], "published" | ||
) | ||
assert not uploaded | ||
assert f"Report not uploaded for order with project id: {order_id}\nReason: 404" in caplog.text | ||
|
||
|
||
def test_get_and_process_orders_open_and_upload(data_repo_full, mock_project_data_record): | ||
"""Test getting and processing an open order and uploading its Project progress report and uploading the report to the Order portal""" | ||
orderer = "[email protected]" | ||
|
@@ -18,8 +43,7 @@ def test_get_and_process_orders_open_and_upload(data_repo_full, mock_project_dat | |
data_master.data = {order_id: mock_project_data_record("open")} | ||
|
||
op = order_portal.OrderPortal(config_values, data_master) | ||
with mock.patch("daily_read.order_portal.OrderPortal._get", side_effect=mocked_requests_get): | ||
op.get_orders(orderer=orderer) | ||
op.get_orders(orderer=orderer) | ||
|
||
assert op.all_orders[0]["identifier"] == order_id | ||
modified_orders = op.process_orders(config_values.STATUS_PRIORITY_REV) | ||
|
@@ -45,7 +69,7 @@ def test_get_and_process_orders_open_and_upload(data_repo_full, mock_project_dat | |
) | ||
|
||
|
||
def test_get_and_process_orders_open_with_report_and_upload(data_repo_full, mock_project_data_record): | ||
def test_get_and_process_orders_open_with_report_and_upload(data_repo_full, mock_project_data_record, caplog): | ||
"""Test getting, processing an open order with an existing Project progress report and uploading the report to the Order portal""" | ||
orderer = "[email protected]" | ||
order_id = "NGI123453" | ||
|
@@ -56,31 +80,33 @@ def test_get_and_process_orders_open_with_report_and_upload(data_repo_full, mock | |
data_master.data = {order_id: mock_project_data_record("open_with_report")} | ||
|
||
op = order_portal.OrderPortal(config_values, data_master) | ||
with mock.patch("daily_read.order_portal.OrderPortal._get", side_effect=mocked_requests_get): | ||
op.get_orders(orderer=orderer) | ||
op.get_orders(orderer=orderer) | ||
|
||
assert op.all_orders[3]["identifier"] == order_id | ||
modified_orders = op.process_orders(config_values.STATUS_PRIORITY_REV) | ||
assert modified_orders[orderer]["projects"]["Library QC finished"][0] == data_master.data[order_id] | ||
with mock.patch("daily_read.order_portal.requests.post") as mock_post: | ||
mock_post.return_value.status_code = 200 | ||
op.upload_report_to_order_portal( | ||
"<html>test data</html>", modified_orders[orderer]["projects"]["Library QC finished"][0], "published" | ||
) | ||
url = f"{config_values.ORDER_PORTAL_URL}/api/v1/report/{op.all_orders[3]['reports'][0]['iuid']}" | ||
indata = dict( | ||
order=order_id, | ||
name="Project Progress", | ||
status="published", | ||
file=dict( | ||
data=base64.b64encode("<html>test data</html>".encode()).decode("utf-8"), | ||
filename="project_progress.html", | ||
content_type="text/html", | ||
), | ||
) | ||
mock_post.assert_called_once_with( | ||
url, headers={"X-OrderPortal-API-key": config_values.ORDER_PORTAL_API_KEY}, json=indata | ||
) | ||
with caplog.at_level(logging.INFO): | ||
uploaded = op.upload_report_to_order_portal( | ||
"<html>test data</html>", modified_orders[orderer]["projects"]["Library QC finished"][0], "published" | ||
) | ||
url = f"{config_values.ORDER_PORTAL_URL}/api/v1/report/{op.all_orders[3]['reports'][0]['iuid']}" | ||
indata = dict( | ||
order=order_id, | ||
name="Project Progress", | ||
status="published", | ||
file=dict( | ||
data=base64.b64encode("<html>test data</html>".encode()).decode("utf-8"), | ||
filename="project_progress.html", | ||
content_type="text/html", | ||
), | ||
) | ||
assert uploaded | ||
mock_post.assert_called_once_with( | ||
url, headers={"X-OrderPortal-API-key": config_values.ORDER_PORTAL_API_KEY}, json=indata | ||
) | ||
assert f"Updated report for order with project id: {order_id}" in caplog.text | ||
|
||
|
||
def test_get_and_process_orders_open_to_aborted_with_report_and_upload(data_repo_full, mock_project_data_record): | ||
|
@@ -94,8 +120,7 @@ def test_get_and_process_orders_open_to_aborted_with_report_and_upload(data_repo | |
data_master.data = {order_id: mock_project_data_record("open_to_aborted_with_report")} | ||
|
||
op = order_portal.OrderPortal(config_values, data_master) | ||
with mock.patch("daily_read.order_portal.OrderPortal._get", side_effect=mocked_requests_get): | ||
op.get_orders(orderer=orderer) | ||
op.get_orders(orderer=orderer) | ||
|
||
assert op.all_orders[4]["identifier"] == order_id | ||
modified_orders = op.process_orders(config_values.STATUS_PRIORITY_REV) | ||
|
@@ -128,8 +153,7 @@ def test_get_and_process_orders_closed(data_repo_full, mock_project_data_record) | |
data_master.data = {order_id: mock_project_data_record("closed")} | ||
|
||
op = order_portal.OrderPortal(config_values, data_master) | ||
with mock.patch("daily_read.order_portal.OrderPortal._get", side_effect=mocked_requests_get): | ||
op.get_orders(orderer=orderer) | ||
op.get_orders(orderer=orderer) | ||
|
||
assert op.all_orders[1]["identifier"] == order_id | ||
modified_orders = op.process_orders(config_values.STATUS_PRIORITY_REV) | ||
|
@@ -147,8 +171,7 @@ def test_get_and_process_orders_mult_reports(data_repo_full, mock_project_data_r | |
data_master.data = {order_id: mock_project_data_record("open")} | ||
|
||
op = order_portal.OrderPortal(config_values, data_master) | ||
with mock.patch("daily_read.order_portal.OrderPortal._get", side_effect=mocked_requests_get): | ||
op.get_orders(orderer=orderer) | ||
op.get_orders(orderer=orderer) | ||
|
||
assert op.all_orders[2]["identifier"] == order_id | ||
with pytest.raises( | ||
|