From e30f9a9b746f7dcdc8707e66cc65e580e6f83f47 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 8 Mar 2022 08:07:38 +0300 Subject: Fix tests, and issues caught by tests Fix some issues caught by tests due to changes introducing the hand-off of the partial correlations computations to an external process Fix some issues due to the changes that introduce context managers for database connections Update some tests to take the above two changes into consideration --- tests/integration/conftest.py | 4 +- tests/integration/test_correlation.py | 4 +- tests/integration/test_partial_correlations.py | 61 ++++++++++++++------------ 3 files changed, 40 insertions(+), 29 deletions(-) (limited to 'tests/integration') diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index e1d1c37..be927a4 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -19,4 +19,6 @@ def client(): def db_conn(): """Create a db connection fixture for tests""" ## Update this to use temp db once that is in place - return database_connector()[0] + conn = database_connector() + yield conn + conn.close() diff --git a/tests/integration/test_correlation.py b/tests/integration/test_correlation.py index cf63c17..d52ab01 100644 --- a/tests/integration/test_correlation.py +++ b/tests/integration/test_correlation.py @@ -71,7 +71,9 @@ class CorrelationIntegrationTest(TestCase): mock_compute_corr.return_value = [] - database_connector.return_value = (mock.Mock(), mock.Mock()) + database_connector.return_value = mock.Mock() + database_connector.return_value.__enter__ = mock.Mock() + database_connector.return_value.__exit__ = mock.Mock() post_data = {"1426678_at": "68031", "1426679_at": "68036", diff --git a/tests/integration/test_partial_correlations.py b/tests/integration/test_partial_correlations.py index 7f9ff30..d249b42 100644 --- a/tests/integration/test_partial_correlations.py +++ b/tests/integration/test_partial_correlations.py @@ -1,4 +1,6 @@ """Test partial correlations""" +from unittest import mock + import pytest from gn3.computations.partial_correlations import partial_correlations_entry @@ -97,43 +99,46 @@ def test_partial_correlation_api_with_missing_request_data(client, post_data): @pytest.mark.parametrize( "post_data", ({# ProbeSet - "primary_trait": {"dataset": "a_dataset", "name": "a_name"}, + "primary_trait": {"dataset": "a_dataset", "trait_name": "a_name"}, "control_traits": [ - {"dataset": "a_dataset", "name": "a_name"}, - {"dataset": "a_dataset2", "name": "a_name2"}], + {"dataset": "a_dataset", "trait_name": "a_name"}, + {"dataset": "a_dataset2", "trait_name": "a_name2"}], "method": "a_method", "target_db": "a_db" }, {# Publish - "primary_trait": {"dataset": "a_Publish_dataset", "name": "a_name"}, + "primary_trait": { + "dataset": "a_Publish_dataset", "trait_name": "a_name"}, "control_traits": [ - {"dataset": "a_dataset", "name": "a_name"}, - {"dataset": "a_dataset2", "name": "a_name2"}], + {"dataset": "a_dataset", "trait_name": "a_name"}, + {"dataset": "a_dataset2", "trait_name": "a_name2"}], "method": "a_method", "target_db": "a_db" }, {# Geno - "primary_trait": {"dataset": "a_Geno_dataset", "name": "a_name"}, + "primary_trait": {"dataset": "a_Geno_dataset", "trait_name": "a_name"}, "control_traits": [ - {"dataset": "a_dataset", "name": "a_name"}, - {"dataset": "a_dataset2", "name": "a_name2"}], + {"dataset": "a_dataset", "trait_name": "a_name"}, + {"dataset": "a_dataset2", "trait_name": "a_name2"}], "method": "a_method", "target_db": "a_db" }, {# Temp - "primary_trait": {"dataset": "a_Temp_dataset", "name": "a_name"}, + "primary_trait": {"dataset": "a_Temp_dataset", "trait_name": "a_name"}, "control_traits": [ - {"dataset": "a_dataset", "name": "a_name"}, - {"dataset": "a_dataset2", "name": "a_name2"}], + {"dataset": "a_dataset", "trait_name": "a_name"}, + {"dataset": "a_dataset2", "trait_name": "a_name2"}], "method": "a_method", "target_db": "a_db" })) -def test_partial_correlation_api_with_non_existent_primary_traits(client, post_data): +def test_partial_correlation_api_with_non_existent_primary_traits( + client, post_data, mocker): """ Check that the system responds appropriately in the case where the user makes a request with a non-existent primary trait. """ + mocker.patch("gn3.api.correlation.redis.Redis", mock.MagicMock()) response = client.post("/api/correlation/partial", json=post_data) assert ( - response.status_code == 404 and response.is_json and - response.json.get("status") != "error") + response.status_code == 200 and response.is_json and + response.json.get("status") == "success") @pytest.mark.integration_test @pytest.mark.slow @@ -141,41 +146,43 @@ def test_partial_correlation_api_with_non_existent_primary_traits(client, post_d "post_data", ({# ProbeSet "primary_trait": { - "dataset": "UCLA_BXDBXH_CARTILAGE_V2", "name": "ILM103710672"}, + "dataset": "UCLA_BXDBXH_CARTILAGE_V2", + "trait_name": "ILM103710672"}, "control_traits": [ - {"dataset": "a_dataset", "name": "a_name"}, - {"dataset": "a_dataset2", "name": "a_name2"}], + {"dataset": "a_dataset", "trait_name": "a_name"}, + {"dataset": "a_dataset2", "trait_name": "a_name2"}], "method": "a_method", "target_db": "a_db" }, {# Publish - "primary_trait": {"dataset": "BXDPublish", "name": "BXD_12557"}, + "primary_trait": {"dataset": "BXDPublish", "trait_name": "BXD_12557"}, "control_traits": [ - {"dataset": "a_dataset", "name": "a_name"}, - {"dataset": "a_dataset2", "name": "a_name2"}], + {"dataset": "a_dataset", "trait_name": "a_name"}, + {"dataset": "a_dataset2", "trait_name": "a_name2"}], "method": "a_method", "target_db": "a_db" }, {# Geno - "primary_trait": {"dataset": "AKXDGeno", "name": "D4Mit16"}, + "primary_trait": {"dataset": "AKXDGeno", "trait_name": "D4Mit16"}, "control_traits": [ - {"dataset": "a_dataset", "name": "a_name"}, - {"dataset": "a_dataset2", "name": "a_name2"}], + {"dataset": "a_dataset", "trait_name": "a_name"}, + {"dataset": "a_dataset2", "trait_name": "a_name2"}], "method": "a_method", "target_db": "a_db" } # Temp -- the data in the database for these is ephemeral, making it # difficult to test for this )) -def test_partial_correlation_api_with_non_existent_control_traits(client, post_data): +def test_partial_correlation_api_with_non_existent_control_traits(client, post_data, mocker): """ Check that the system responds appropriately in the case where the user makes a request with a non-existent control traits. The code repetition here is on purpose - valuing clarity over succinctness. """ + mocker.patch("gn3.api.correlation.redis.Redis", mock.MagicMock()) response = client.post("/api/correlation/partial", json=post_data) assert ( - response.status_code == 404 and response.is_json and - response.json.get("status") != "error") + response.status_code == 200 and response.is_json and + response.json.get("status") == "success") @pytest.mark.integration_test @pytest.mark.slow -- cgit v1.2.3