diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/integration/test_datasets.py | 41 | ||||
-rw-r--r-- | tests/integration/test_traits.py | 72 | ||||
-rw-r--r-- | tests/unit/computations/test_correlation.py | 74 | ||||
-rw-r--r-- | tests/unit/computations/test_datasets.py | 219 | ||||
-rw-r--r-- | tests/unit/computations/test_trait.py | 84 |
5 files changed, 459 insertions, 31 deletions
diff --git a/tests/integration/test_datasets.py b/tests/integration/test_datasets.py new file mode 100644 index 0000000..f97d970 --- /dev/null +++ b/tests/integration/test_datasets.py @@ -0,0 +1,41 @@ +"""This module contains integration tests for datasets""" +from unittest import TestCase +from unittest import mock + +from collections import namedtuple +from gn3.app import create_app + + +class DatasetIntegrationTests(TestCase): + """class contains integration tests for datasets""" + + def setUp(self): + self.app = create_app().test_client() + + @mock.patch("gn3.api.datasets.create_dataset") + def test_create_dataset(self, mock_dataset): + """Test for creating dataset object""" + mock_dataset_creator = namedtuple( + 'ProbeSet', ["dataset_name", "dataset_type"]) + new_dataset = mock_dataset_creator("HC_M2_0606_P", "ProbeSet") + mock_dataset.return_value = new_dataset + response = self.app.get( + "/api/dataset/create/HC_M2_0606_P/", follow_redirects=True) + mock_dataset.assert_called_once_with( + dataset_type=None, dataset_name="HC_M2_0606_P") + results = response.get_json()["dataset"] + self.assertEqual(results[1], "ProbeSet") + self.assertEqual(response.status_code, 200) + + @mock.patch("gn3.api.datasets.get_traits_data") + @mock.patch("gn3.api.datasets.database_connector") + def test_fetch_traits_data(self, mock_db, mock_get_trait_data): + """Test api/dataset/fetch_traits_data/d_name/d_type""" + + mock_get_trait_data.return_value = {} + mock_db.return_value = (mock.Mock(), mock.Mock()) + response = self.app.get( + "/api/dataset/fetch_traits_data/Aging-Brain-UCIPublish/Publish", follow_redirects=True) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.get_json(), {"results": {}}) diff --git a/tests/integration/test_traits.py b/tests/integration/test_traits.py new file mode 100644 index 0000000..410ba22 --- /dev/null +++ b/tests/integration/test_traits.py @@ -0,0 +1,72 @@ +"""module contains integration tests for trait endpoints""" +from unittest import TestCase +from unittest import mock + +from gn3.app import create_app + + +class TraitIntegrationTest(TestCase): + """class contains integration tests for\ + traits""" + + def setUp(self): + self.app = create_app().test_client() + + @mock.patch("gn3.api.traits.fetch_trait") + @mock.patch("gn3.api.traits.database_connector") + def test_create_trait(self, mock_database, mock_fetch_trait): + """test the endpoint for creating traits\ + endpoint requires trait name and dataset name""" + mock_database.return_value = (mock.Mock(), mock.Mock()) + trait_results = { + "dataset": None, + "trait_name": "1449593_at", + "trait_data": { + "BXD11": 8.464, + "BXD12": 8.414, + "BXD13": 8.753, + "BXD15": 8.5, + "BXD16": 8.832 + } + + } + mock_fetch_trait.return_value = trait_results + + results = self.app.get( + "/api/trait/1449593_at/HC_M2_0606_P", follow_redirects=True) + + trait_data = results.get_json() + + self.assertEqual(mock_database.call_count, 1) + self.assertEqual(results.status_code, 200) + self.assertEqual(trait_data, trait_results) + + @mock.patch("gn3.api.traits.get_trait_info_data") + def test_retrieve_trait_info(self, mock_get_trait_info): + """integration test for endpoints for retrieving\ + trait info expects the dataset of trait to have been + created""" + + trait_post_data = { + "trait": {"trait_name": ""}, + "trait_dataset": {"dataset_name": ""} + } + + expected_api_results = { + "description": "trait description", + "chr": "", + "locus": "", + "mb": "", + "abbreviation": "trait_abbreviation", + "trait_display_name": "trait_name" + + } + mock_get_trait_info.return_value = expected_api_results + + trait_info = self.app.post( + "/api/trait/trait_info/144_at", json=trait_post_data, follow_redirects=True) + + trait_info_results = trait_info.get_json() + + self.assertEqual(trait_info.status_code, 200) + self.assertEqual(trait_info_results, expected_api_results) diff --git a/tests/unit/computations/test_correlation.py b/tests/unit/computations/test_correlation.py index 52d1f60..8f3ef25 100644 --- a/tests/unit/computations/test_correlation.py +++ b/tests/unit/computations/test_correlation.py @@ -1,4 +1,4 @@ -"""module contains the tests for correlation""" +"""Module contains the tests for correlation""" import unittest from unittest import TestCase from unittest import mock @@ -88,10 +88,10 @@ class DataBase(QueryableMixin): class TestCorrelation(TestCase): - """class for testing correlation functions""" + """Class for testing correlation functions""" def test_normalize_values(self): - """function to test normalizing values """ + """Function to test normalizing values """ results = normalize_values([2.3, None, None, 3.2, 4.1, 5], [3.4, 7.2, 1.3, None, 6.2, 4.1]) @@ -100,7 +100,7 @@ class TestCorrelation(TestCase): self.assertEqual(results, expected_results) def test_bicor(self): - """test for doing biweight mid correlation """ + """Test for doing biweight mid correlation """ results = do_bicor(x_val=[1, 2, 3], y_val=[4, 5, 6]) @@ -110,8 +110,9 @@ class TestCorrelation(TestCase): @mock.patch("gn3.computations.correlations.compute_corr_coeff_p_value") @mock.patch("gn3.computations.correlations.normalize_values") def test_compute_sample_r_correlation(self, norm_vals, compute_corr): - """test for doing sample correlation gets the cor\ - and p value and rho value using pearson correlation""" + """Test for doing sample correlation gets the cor\ + and p value and rho value using pearson correlation + """ primary_values = [2.3, 4.1, 5] target_values = [3.4, 6.2, 4.1] @@ -141,7 +142,7 @@ class TestCorrelation(TestCase): spearman_results, tuple, "message") def test_filter_shared_sample_keys(self): - """function to tests shared key between two dicts""" + """Function to tests shared key between two dicts""" this_samplelist = { "C57BL/6J": "6.638", @@ -170,7 +171,7 @@ class TestCorrelation(TestCase): @mock.patch("gn3.computations.correlations.compute_sample_r_correlation") @mock.patch("gn3.computations.correlations.filter_shared_sample_keys") def test_compute_all_sample(self, filter_shared_samples, sample_r_corr): - """given target dataset compute all sample r correlation""" + """Given target dataset compute all sample r correlation""" filter_shared_samples.return_value = (["1.23", "6.565", "6.456"], [ "6.266", "6.565", "6.456"]) @@ -200,7 +201,6 @@ class TestCorrelation(TestCase): sample_all_results = [{"1419792_at": {"corr_coeffient": -1.0, "p_value": 0.9, "num_overlap": 6}}] - # ?corr_method: str, trait_vals, target_samples_vals self.assertEqual(compute_all_sample_correlation( this_trait=this_trait_data, target_dataset=traits_dataset), sample_all_results) @@ -212,9 +212,10 @@ class TestCorrelation(TestCase): @unittest.skip("not implemented") def test_tissue_lit_corr_for_probe_type(self): - """tests for doing tissue and lit correlation for trait list\ + """Tests for doing tissue and lit correlation for trait list\ if both the dataset and target dataset are probeset runs\ - on after initial correlation has been done""" + on after initial correlation has been done + """ results = tissue_lit_corr_for_probe_type( corr_type="tissue", top_corr_results={}) @@ -223,8 +224,9 @@ class TestCorrelation(TestCase): @mock.patch("gn3.computations.correlations.compute_corr_coeff_p_value") def test_tissue_correlation_for_trait_list(self, mock_compute_corr_coeff): - """test given a primary tissue values for a trait and and a list of\ - target tissues for traits do the tissue correlation for them""" + """Test given a primary tissue values for a trait and and a list of\ + target tissues for traits do the tissue correlation for them + """ primary_tissue_values = [1.1, 1.5, 2.3] target_tissues_values = [1, 2, 3] @@ -241,8 +243,9 @@ class TestCorrelation(TestCase): @mock.patch("gn3.computations.correlations.fetch_lit_correlation_data") @mock.patch("gn3.computations.correlations.map_to_mouse_gene_id") def test_lit_correlation_for_trait_list(self, mock_mouse_gene_id, fetch_lit_data): - """fetch results from db call for lit correlation given a trait list\ - after doing correlation""" + """Fetch results from db call for lit correlation given a trait list\ + after doing correlation + """ target_trait_lists = [("1426679_at", 15), ("1426702_at", 17), @@ -265,8 +268,9 @@ class TestCorrelation(TestCase): self.assertEqual(lit_results, expected_results) def test_fetch_lit_correlation_data(self): - """test for fetching lit correlation data from\ - the database where the input and mouse geneid are none""" + """Test for fetching lit correlation data from\ + the database where the input and mouse geneid are none + """ conn = DataBase() results = fetch_lit_correlation_data(conn=conn, @@ -277,8 +281,9 @@ class TestCorrelation(TestCase): self.assertEqual(results, ("1", 0)) def test_fetch_lit_correlation_data_db_query(self): - """test for fetching lit corr coefficent givent the input\ - input trait mouse gene id and mouse gene id""" + """Test for fetching lit corr coefficent givent the input\ + input trait mouse gene id and mouse gene id + """ expected_db_results = [namedtuple("lit_coeff", "val")(x*0.1) for x in range(1, 4)] @@ -293,9 +298,12 @@ class TestCorrelation(TestCase): self.assertEqual(expected_results, lit_results) def test_query_lit_correlation_for_db_empty(self): - """test that corr coeffient returned is 0 given the\ - db value if corr coefficient is empty""" - database_instance = DataBase() + """Test that corr coeffient returned is 0 given the\ + db value if corr coefficient is empty + """ + database_instance = mock.Mock() + database_instance.execute.return_value.fetchone.return_value = None + lit_results = fetch_lit_correlation_data(conn=database_instance, input_mouse_gene_id="12", gene_id="16", @@ -304,8 +312,9 @@ class TestCorrelation(TestCase): self.assertEqual(lit_results, ("16", 0)) def test_query_formatter(self): - """test for formatting a query given the query string and also the\ - values""" + """Test for formatting a query given the query string and also the\ + values + """ query = """ SELECT VALUE FROM LCorr @@ -330,16 +339,18 @@ class TestCorrelation(TestCase): self.assertEqual(formatted_query, expected_formatted_query) def test_query_formatter_no_query_values(self): - """test for formatting a query where there are no\ - string placeholder""" + """Test for formatting a query where there are no\ + string placeholder + """ query = """SELECT * FROM USERS""" formatted_query = query_formatter(query) self.assertEqual(formatted_query, query) def test_map_to_mouse_gene_id(self): - """test for converting a gene id to mouse geneid\ - given a species which is not mouse""" + """Test for converting a gene id to mouse geneid\ + given a species which is not mouse + """ database_instance = mock.Mock() test_data = [("Human", 14), (None, 9), ("Mouse", 15), ("Rat", 14)] @@ -361,9 +372,10 @@ class TestCorrelation(TestCase): @mock.patch("gn3.computations.correlations.lit_correlation_for_trait_list") def test_compute_all_lit_correlation(self, mock_lit_corr): - """test for compute all lit correlation which acts\ + """Test for compute all lit correlation which acts\ as an abstraction for lit_correlation_for_trait_list - and is used in the api/correlation/lit""" + and is used in the api/correlation/lit + """ database = mock.Mock() @@ -385,7 +397,7 @@ class TestCorrelation(TestCase): @mock.patch("gn3.computations.correlations.tissue_correlation_for_trait_list") @mock.patch("gn3.computations.correlations.process_trait_symbol_dict") def test_compute_all_tissue_correlation(self, process_trait_symbol, mock_tissue_corr): - """test for compute all tissue corelation which abstracts + """Test for compute all tissue corelation which abstracts api calling the tissue_correlation for trait_list""" primary_tissue_dict = {"trait_id": "1419792_at", diff --git a/tests/unit/computations/test_datasets.py b/tests/unit/computations/test_datasets.py new file mode 100644 index 0000000..f9e9c2b --- /dev/null +++ b/tests/unit/computations/test_datasets.py @@ -0,0 +1,219 @@ +"""Module contains tests from datasets""" +import json + +from unittest import TestCase +from unittest import mock + +from collections import namedtuple + +from gn3.computations.datasets import retrieve_trait_sample_data +from gn3.computations.datasets import get_query_for_dataset_sample +from gn3.computations.datasets import fetch_from_db_sample_data +from gn3.computations.datasets import create_dataset +from gn3.computations.datasets import dataset_creator_store +from gn3.computations.datasets import dataset_type_getter +from gn3.computations.datasets import fetch_dataset_type_from_gn2_api +from gn3.computations.datasets import fetch_dataset_sample_id +from gn3.computations.datasets import divide_into_chunks +from gn3.computations.datasets import get_traits_data + + +class TestDatasets(TestCase): + """Class contains tests for datasets""" + + @mock.patch("gn3.computations.datasets.fetch_from_db_sample_data") + def test_retrieve_trait_sample_data(self, mock_fetch_sample_results): + """Test retrieving sample data\ + for trait from the dataset + """ + trait_name = "1419792_at" + dataset_id = "HC_M2_0606_P&" + dataset_type = "Publish" + + database = mock.Mock() + + dataset = { + "id": dataset_id, + "type": dataset_type, + "name": dataset_id + } + + fetch_results = [('BXD32', 8.001, None, None, 'BXD32')] + + mock_fetch_sample_results.return_value = fetch_results + + results = retrieve_trait_sample_data( + dataset, trait_name, database) + self.assertEqual(mock_fetch_sample_results.call_count, 1) + self.assertEqual(results, fetch_results) + + def test_query_for_dataset_sample(self): + """Test for getting query for sample data""" + + no_results = get_query_for_dataset_sample("does not exists") + + query_exists = get_query_for_dataset_sample("Publish") + + self.assertEqual(no_results, None) + self.assertIsInstance(query_exists, str) + + def test_fetch_from_db_sample_data(self): + """Test for function that fetches sample\ + results from the database + """ + + database_results = [('BXD31', 8.001, None, None, 'BXD31'), + ('BXD32', 7.884, None, None, 'BXD32'), + ('BXD42', 7.682, None, None, 'BXD42'), + ('BXD42', 7.682, None, None, 'BXD42'), + ('BXD40', 7.945, None, None, 'BXD40'), + ('BXD43', 7.873, None, None, 'BXD43') + ] + + database = mock.Mock() + db_cursor = mock.Mock() + db_cursor.execute.return_value = 6 + db_cursor.fetchall.return_value = database_results + database.cursor.return_value = db_cursor + + mock_pheno_query = """ + SELECT + Strain.Name, PublishData.value, PublishSE.error,NStrain.count, Strain.Name2 + WHERE + PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND + PublishData.Id = PublishXRef.DataId AND PublishXRef.Id = 1419792_at AND + PublishFreeze.Id = '12' AND PublishData.StrainId = Strain.Id + Order BY + Strain.Name + """ + fetch_results = fetch_from_db_sample_data(mock_pheno_query, database) + + self.assertEqual(fetch_results, database_results) + + @mock.patch("gn3.computations.datasets.dataset_creator_store") + @mock.patch("gn3.computations.datasets.dataset_type_getter") + def test_create_dataset(self, mock_dataset_type, mock_store): + """Test function that creates/fetches required dataset\ + can either be published phenotype,genotype,Microarray or\ + user defined ->Temp + """ + probe_name = "HC_M2_0606_P" + probe_type = "ProbeSet" + + mock_dataset_creator = namedtuple( + 'ProbeSet', ["dataset_name", "dataset_type"]) + + mock_store.return_value = mock_dataset_creator + mock_dataset_type.return_value = probe_type + dataset = create_dataset( + dataset_type=None, dataset_name=probe_name) + + self.assertEqual(dataset.dataset_name, probe_name) + self.assertEqual(dataset.dataset_type, probe_type) + + def test_dataset_creator_store(self): + """Test for functions that actual + function to create differerent \ + datasets + """ + results = dataset_creator_store("ProbeSet") + + self.assertTrue(results) + + def test_dataset_type_getter(self): + """Test for fetching type of dataset given\ + the dataset name + """ + + redis_instance = mock.Mock() + # fetched in redis + redis_instance.get.return_value = "ProbeSet" + results = dataset_type_getter("HC_M2_0_P", redis_instance) + self.assertEqual(results, "ProbeSet") + + @mock.patch("gn3.computations.datasets.requests") + def test_fetch_dataset_type_from_gn2_api(self, mock_request): + """Test for function that test fetching\ + all datasets from gn2 api in order to store\ + in redis + """ + + expected_json_results = {"datasets": { + "arabidopsis": { + "BayXSha": { + "Genotypes": [ + [ + "None", + "BayXShaGeno", + "BayXSha Genotypes" + ] + ], + "Phenotypes": [ + [ + "642", + "BayXShaPublish", + "BayXSha Published Phenotypes" + ] + ] + } + } + }} + + request_results = json.dumps(expected_json_results) + mock_request.get.return_value.content = request_results + results = fetch_dataset_type_from_gn2_api("HC_M2_0_P") + expected_results = { + "BayXShaGeno": "Geno", + "642": "Publish" + } + + self.assertEqual(expected_results, results) + + def test_fetch_dataset_sample_id(self): + """Get from the database the sample\ + id if only in the samplelists + """ + + expected_results = {"B6D2F1": 1, "BXD1": 4, "BXD11": 10, + "BXD12": 11, "BXD13": 12, "BXD15": 14, "BXD16": 15} + + database_instance = mock.Mock() + database_cursor = mock.Mock() + + database_cursor.execute.return_value = 5 + database_cursor.fetchall.return_value = list(expected_results.items()) + database_instance.cursor.return_value = database_cursor + strain_list = ["B6D2F1", "BXD1", "BXD11", + "BXD12", "BXD13", "BXD16", "BXD15"] + + results = fetch_dataset_sample_id( + samplelist=strain_list, database=database_instance, species="mouse") + + self.assertEqual(results, expected_results) + + @mock.patch("gn3.computations.datasets.fetch_from_db_sample_data") + @mock.patch("gn3.computations.datasets.divide_into_chunks") + def test_get_traits_data(self, mock_divide_into_chunks, mock_fetch_samples): + """Test for for function to get data\ + of traits in dataset + """ + _expected_results = {'AT_DSAFDS': [ + 12, 14, 13, 23, 12, 14, 13, 23, 12, 14, 13, 23]} + database = mock.Mock() + sample_id = [1, 2, 7, 3, 22, 8] + mock_divide_into_chunks.return_value = [ + [1, 2, 7], [3, 22, 8], [5, 22, 333]] + mock_fetch_samples.return_value = ("AT_DSAFDS", 12, 14, 13, 23) + results = get_traits_data(sample_id, database, "HC_M2", "Publish") + + self.assertEqual({}, dict(results)) + + def test_divide_into_chunks(self): + """Test for dividing a list into given number of\ + chunks for example + """ + results = divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 3) + + expected_results = [[1, 2, 7], [3, 22, 8], [5, 22, 333]] + + self.assertEqual(results, expected_results) diff --git a/tests/unit/computations/test_trait.py b/tests/unit/computations/test_trait.py new file mode 100644 index 0000000..feb97c6 --- /dev/null +++ b/tests/unit/computations/test_trait.py @@ -0,0 +1,84 @@ +"""Module contains tests for creating traits""" +from unittest import TestCase +from unittest import mock + +from gn3.computations.traits import fetch_trait +from gn3.computations.traits import get_trait_sample_data +from gn3.computations.traits import get_trait_info_data + + +class TestTrait(TestCase): + """Class contains tests for creating traits""" + + @mock.patch("gn3.computations.traits.get_trait_sample_data") + def test_fetch_trait(self, get_sample_data): + """Test for creating/fetching trait""" + + expected_sample_data = { + "A/Y": 12.3, + "WQC": 11.1 + } + + database = mock.Mock() + + get_sample_data.return_value = expected_sample_data + + expected_trait = { + "trait_name": "AXFDSF_AT", + "dataset": None, + "trait_data": expected_sample_data + } + results = fetch_trait(dataset=None, + trait_name="AXFDSF_AT", + database=database) + + self.assertEqual(results, expected_trait) + self.assertEqual(get_sample_data.call_count, 1) + + @mock.patch("gn3.computations.traits.retrieve_trait_sample_data") + def test_get_trait_sample_data(self, mock_retrieve_sample_data): + """Test for getting sample data from either\ + the trait's dataset or form redis + """ + + trait_dataset = mock.Mock() + dataset_trait_sample_data = [ + ('129S1/SvImJ', 7.433, None, None, '129S1/SvImJ'), + ('A/J', 7.596, None, None, 'A/J'), + ('AKR/J', 7.774, None, None, 'AKR/J'), + ('B6D2F1', 7.707, None, None, 'B6D2F1')] + mock_retrieve_sample_data.return_value = dataset_trait_sample_data + + trait_name = "1426679_at" + + database = mock.Mock() + + results = get_trait_sample_data( + trait_dataset, trait_name, database) + + expected_results = { + "129S1/SvImJ": 7.433, + "A/J": 7.596, + "AKR/J": 7.774, + "B6D2F1": 7.707 + } + + self.assertEqual(results, expected_results) + + def test_get_trait_info_data(self): + """Test for getting info data related\ + to trait + """ + + results = get_trait_info_data( + trait_name="AXSF_AT", trait_dataset=mock.Mock(), database_instance=None) + expected_trait_info = { + "description": "", + "trait_display_name": "", + "abbreviation": "", + "chr": "", + "mb": "", + "locus": "" + } + + self.assertEqual(results, expected_trait_info) |