diff options
author | Alexander Kabui | 2021-03-30 18:12:58 +0300 |
---|---|---|
committer | Alexander Kabui | 2021-03-30 18:12:58 +0300 |
commit | e6f10522833cbd75441766e5b8656b3f5925d6d7 (patch) | |
tree | 99ad19a80f3a22307164c4d6e0450e527a060640 | |
parent | 5151987063eab58b10a2dd8e831ec036df217531 (diff) | |
download | genenetwork3-e6f10522833cbd75441766e5b8656b3f5925d6d7.tar.gz |
initial commit for creating trait and datasets
-rw-r--r-- | gn3/computations/traits.py | 59 | ||||
-rw-r--r-- | tests/integration/test_traits.py | 70 | ||||
-rw-r--r-- | tests/unit/computations/test_trait.py | 81 |
3 files changed, 210 insertions, 0 deletions
diff --git a/gn3/computations/traits.py b/gn3/computations/traits.py new file mode 100644 index 0000000..7386745 --- /dev/null +++ b/gn3/computations/traits.py @@ -0,0 +1,59 @@ +"""module contains all operating related to traits""" + + +def compute_sum(rhs_val: int, lhs_val: int) -> int: + """initial function for trait module""" + return rhs_val + lhs_val + + +def fetch_trait(dataset, trait_name: str) -> dict: + """this method creates a trait by\ + fetching required data given the\ + dataset and trait_name""" + + created_trait = { + "dataset": dataset, + "trait_name": trait_name + } + + trait_data = get_trait_sample_data(dataset, trait_name) + + created_trait["trait_data"] = trait_data + + return created_trait + + +def get_trait_sample_data(trait_dataset, trait_name) -> dict: + """first try to fetch the traits sample data from redis if that\ + try to fetch from the traits dataset redis is only used for\ + temp dataset type which is not used in this case """ + + sample_results = trait_dataset.retrieve_sample_data(trait_name) + + trait_data = {} + + for (name, sample_value, _variance, _numcase, _name2) in sample_results: + + trait_data[name] = sample_value + return trait_data + + +def get_trait_info_data(trait_dataset, + trait_name: str, + database_instance, + get_qtl_info: bool = False) -> dict: + """given a dataset and trait_name return a dict containing all info\ + regarding the get trait""" + + _temp_var_holder = (trait_dataset, trait_name, + database_instance, get_qtl_info) + trait_info_data = { + "description": "", + "chr": "", + "locus": "", + "mb": "", + "abbreviation": "", + "trait_display_name": "" + + } + return trait_info_data diff --git a/tests/integration/test_traits.py b/tests/integration/test_traits.py new file mode 100644 index 0000000..2f3433a --- /dev/null +++ b/tests/integration/test_traits.py @@ -0,0 +1,70 @@ +"""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() + + def test_home(self): + """test for initial endpoint for trait""" + results = self.app.get("/api/trait", follow_redirects=True) + + self.assertEqual(200, results.status_code) + + @mock.patch("gn3.api.traits.fetch_trait") + def test_create_trait(self, mock_fetch_trait): + """test the endpoint for creating traits\ + endpoint requires trait name and dataset name""" + + trait_results = { + "dataset": None, + "trait_name": "1449593_at", + "trait_data": {} + + } + 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(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_trait.py b/tests/unit/computations/test_trait.py new file mode 100644 index 0000000..20f0546 --- /dev/null +++ b/tests/unit/computations/test_trait.py @@ -0,0 +1,81 @@ +"""module contains tests for creating traits""" +from unittest import TestCase +from unittest import mock + +from gn3.computations.traits import compute_sum +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""" + + def test_sum(self): + """initial faling tests""" + results = compute_sum(2, 5) + self.assertEqual(results, 7) + + @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 + } + + 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") + + self.assertEqual(results, expected_trait) + get_sample_data.assert_called_once_with(None, "AXFDSF_AT") + + def test_get_trait_sample_data(self): + """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')] + trait_dataset.retrieve_sample_data.return_value = dataset_trait_sample_data + + trait_name = "1426679_at" + + results = get_trait_sample_data( + trait_dataset, trait_name) + + 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) |