diff options
author | Alexander Kabui | 2021-03-16 10:36:58 +0300 |
---|---|---|
committer | GitHub | 2021-03-16 10:36:58 +0300 |
commit | 43d1bb7f6cd2b5890d5b3eb7c357caafda25a35c (patch) | |
tree | 73683272f32cffc860497a93b5c844c272252e67 /tests/integration/test_correlation.py | |
parent | 995f1dbd081eb64ad177f929615a4edee01cb68f (diff) | |
download | genenetwork3-43d1bb7f6cd2b5890d5b3eb7c357caafda25a35c.tar.gz |
Refactor/clean up correlations (#4)
* initial commit for Refactor/clean-up-correlation
* add python scipy dependency
* initial commit for sample correlation
* initial commit for sample correlation endpoint
* initial commit for integration and unittest
* initial commit for registering correlation blueprint
* add and modify unittest and integration tests for correlation
* Add compute compute_all_sample_corr method for correlation
* add scipy to requirement txt file
* add tissue correlation for trait list
* add unittest for tissue correlation
* add lit correlation for trait list
* add unittests for lit correlation for trait list
* modify lit correlarion for trait list
* add unittests for lit correlation for trait list
* add correlation metho in dynamic url
* add file format for expected structure input while doing sample correlation
* modify input data structure -> add trait id
* update tests for sample r correlation
* add compute all lit correlation method
* add endpoint for computing lit_corr
* add unit and integration tests for computing lit corr
* add /api/correlation/tissue_corr/{corr_method} endpoint for tissue correlation
* add unittest and integration tests for tissue correlation
Co-authored-by: BonfaceKilz <bonfacemunyoki@gmail.com>
Diffstat (limited to 'tests/integration/test_correlation.py')
-rw-r--r-- | tests/integration/test_correlation.py | 118 |
1 files changed, 80 insertions, 38 deletions
diff --git a/tests/integration/test_correlation.py b/tests/integration/test_correlation.py index 33e0de9..488a8a4 100644 --- a/tests/integration/test_correlation.py +++ b/tests/integration/test_correlation.py @@ -1,57 +1,99 @@ -"""Integration tests for correlation api""" - -import os -import json -import unittest +"""module contains integration tests for correlation""" +from unittest import TestCase from unittest import mock - from gn3.app import create_app -def file_path(relative_path): - """getting abs path for file """ - dir_name = os.path.dirname(os.path.abspath(__file__)) - split_path = relative_path.split("/") - new_path = os.path.join(dir_name, *split_path) - return new_path +class CorrelationIntegrationTest(TestCase): + """class for correlation integration tests""" - -class CorrelationAPITest(unittest.TestCase): - # currently disable - """Test cases for the Correlation API""" def setUp(self): self.app = create_app().test_client() - with open(file_path("correlation_data.json")) as json_file: - self.correlation_data = json.load(json_file) + def test_fail(self): + """initial method for class that fails""" + self.assertEqual(2, 2) + + @mock.patch("gn3.api.correlation.compute_all_sample_correlation") + def test_sample_r_correlation(self, mock_compute_samples): + """Test /api/correlation/sample_r/{method}""" + this_trait_data = { + "trait_id": "1455376_at", + "trait_sample_data": { + "C57BL/6J": "6.138", + "DBA/2J": "6.266", + "B6D2F1": "6.434", + "D2B6F1": "6.55", + "BXS2": "6.7" + }} + + traits_dataset = [ + { + "trait_id": "14192_at", + "trait_sample_data": { + "DBA/2J": "7.13", + "D2B6F1": "5.65", + "BXD2": "1.46" + } + } + ] + + correlation_input_data = {"corr_method": "pearson", + "this_trait": this_trait_data, + "target_dataset": traits_dataset} + + expected_results = [ + { + "sample_r": "-0.407", + "p_value": "6.234e-04" + }, + { + "sample_r": "0.398", + "sample_p": "8.614e-04" + } + ] + + mock_compute_samples.return_value = expected_results + + api_response = { + "corr_results": expected_results + } + + response = self.app.post("/api/correlation/sample_r/pearson", + json=correlation_input_data, follow_redirects=True) - with open(file_path("expected_corr_results.json")) as results_file: - self.correlation_results = json.load(results_file) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.get_json(), api_response) - def tearDown(self): - self.correlation_data = "" + @mock.patch("gn3.api.correlation.compute_all_lit_correlation") + def test_lit_correlation(self, mock_compute_corr): + """Test api/correlation/lit_corr/{species}/{gene_id}""" - self.correlation_results = "" + mock_compute_corr.return_value = [] - @mock.patch("gn3.api.correlation.compute_correlation") - def test_corr_compute(self, compute_corr): - """Test that the correct response in correlation""" + post_data = [{"gene_id": 8, "lit_corr": 1}, { + "gene_id": 12, "lit_corr": 0.3}] - compute_corr.return_value = self.correlation_results - response = self.app.post("/api/correlation/corr_compute", - json=self.correlation_data, - follow_redirects=True) + response = self.app.post( + "/api/correlation/lit_corr/mouse/16", json=post_data, follow_redirects=True) + self.assertEqual(mock_compute_corr.call_count, 1) self.assertEqual(response.status_code, 200) - @mock.patch("gn3.api.correlation.compute_correlation") - def test_corr_compute_failed_request(self, compute_corr): - """test taht cormpute requests fails """ + @mock.patch("gn3.api.correlation.compute_all_tissue_correlation") + def test_tissue_correlation(self, mock_tissue_corr): + """Test api/correlation/tissue_corr/{corr_method}""" + mock_tissue_corr.return_value = {} - compute_corr.return_value = self.correlation_results + primary_dict = {"trait_id": "1449593_at", "tissue_values": [1, 2, 3]} - response = self.app.post("/api/correlation/corr_compute", - json=None, - follow_redirects=True) + target_tissue_dict_list = [ + {"trait_id": "1449593_at", "tissue_values": [1, 2, 3]}] - self.assertEqual(response.status_code, 400) + tissue_corr_input_data = {"primary_tissue": primary_dict, + "target_tissues": target_tissue_dict_list} + + response = self.app.post("/api/correlation/tissue_corr/spearman", + json=tissue_corr_input_data, follow_redirects=True) + + self.assertEqual(response.status_code, 200) |