From 579ec56e488db19e7cd489b906ae6da7cd453239 Mon Sep 17 00:00:00 2001 From: zsloan Date: Sat, 22 May 2021 22:31:51 +0000 Subject: Rewrote test_rqtl_mapping.py, though haven't done tests for all functions yet --- .../wqflask/marker_regression/test_rqtl_mapping.py | 76 ++++++++++++---------- 1 file changed, 40 insertions(+), 36 deletions(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index 91d2c587..5c679c05 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -1,42 +1,46 @@ import unittest from unittest import mock -from wqflask import app -from wqflask.marker_regression.rqtl_mapping import get_trait_data_type -from wqflask.marker_regression.rqtl_mapping import sanitize_rqtl_phenotype -from wqflask.marker_regression.rqtl_mapping import sanitize_rqtl_names +from wqflask.marker_regression.rqtl_mapping import run_rqtl +class AttributeSetter: + def __init__(self, obj): + for key, val in obj.items(): + setattr(self, key, val) + +class MockGroup(AttributeSetter): + def get_samplelist(self): + return None class TestRqtlMapping(unittest.TestCase): - def setUp(self): - self.app_context = app.app_context() - self.app_context.push() - - def tearDown(self): - self.app_context.pop() - - @mock.patch("wqflask.marker_regression.rqtl_mapping.g") - @mock.patch("wqflask.marker_regression.rqtl_mapping.logger") - def test_get_trait_data(self, mock_logger, mock_db): - """test for getting trait data_type return True""" - query_value = """SELECT value FROM TraitMetadata WHERE type='trait_data_type'""" - mock_db.db.execute.return_value.fetchone.return_value = [ - """{"type":"trait_data_type","name":"T1","traid_id":"fer434f"}"""] - results = get_trait_data_type("traid_id") - mock_db.db.execute.assert_called_with(query_value) - self.assertEqual(results, "fer434f") - - def test_sanitize_rqtl_phenotype(self): - """test for sanitizing rqtl phenotype""" - vals = ['f', "x", "r", "x", "x"] - results = sanitize_rqtl_phenotype(vals) - expected_phenotype_string = 'c(f,NA,r,NA,NA)' - - self.assertEqual(results, expected_phenotype_string) - - def test_sanitize_rqtl_names(self): - """test for sanitzing rqtl names""" - vals = ['f', "x", "r", "x", "x"] - expected_sanitized_name = "c('f',NA,'r',NA,NA)" - results = sanitize_rqtl_names(vals) - self.assertEqual(expected_sanitized_name, results) + @mock.patch("wqflask.marker_regression.rqtl_mapping.process_rqtl_results") + @mock.patch("wqflask.marker_regression.rqtl_mapping.process_perm_results") + @mock.patch("wqflask.marker_regression.rqtl_mapping.requests.post") + @mock.patch("wqflask.marker_regression.rqtl_mapping.locate") + @mock.patch("wqflask.marker_regression.gemma_mapping.write_phenotype_file") + def test_run_rqtl_with_perm(self, mock_write_pheno_file, mock_locate, mock_post, mock_process_perm, mock_process_rqtl): + """Test for run_rqtl with permutations > 0""" + dataset_group = MockGroup( + {"name": "GP1", "genofile": "file_geno"}) + + dataset = AttributeSetter({"group": dataset_group}) + + mock_write_pheno_file.return_value = "pheno_filename" + mock_locate.return_value = "geno_filename" + + mock_post.return_value = "output_filename" + + mock_process_perm.return_value = [[], 3, 4] + mock_process_rqtl.return_value = [] + + results = run_rqtl(trait_name="the_trait", vals=[], samples=[], + dataset=dataset, mapping_scale="cM", model="normal", method="hk", + num_perm=5, perm_strata_list=[], do_control="false", control_marker="", + manhattan_plot=True, cofactors="") + + mock_write_pheno_file.assert_called_once() + mock_locate.assert_called_once() + mock_post.assert_called_once() + mock_process_perm.assert_called_once() + mock_process_rqtl.assert_called_once() + self.assertEqual(results, ([], 3, 4, [])) \ No newline at end of file -- cgit v1.2.3 From 1ec1219eae75f1cf9a1b20ea479ea6e2934eaa68 Mon Sep 17 00:00:00 2001 From: zsloan Date: Sat, 22 May 2021 22:43:58 +0000 Subject: Fixed a couple broken tests --- wqflask/tests/unit/wqflask/api/test_mapping.py | 2 +- wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/api/test_mapping.py b/wqflask/tests/unit/wqflask/api/test_mapping.py index b094294a..159c982b 100644 --- a/wqflask/tests/unit/wqflask/api/test_mapping.py +++ b/wqflask/tests/unit/wqflask/api/test_mapping.py @@ -58,7 +58,7 @@ class TestMapping(unittest.TestCase): self.assertEqual(results_2, expected_results) - @mock.patch("wqflask.api.mapping.rqtl_mapping.run_rqtl_geno") + @mock.patch("wqflask.api.mapping.rqtl_mapping.run_rqtl") @mock.patch("wqflask.api.mapping.gemma_mapping.run_gemma") @mock.patch("wqflask.api.mapping.initialize_parameters") @mock.patch("wqflask.api.mapping.retrieve_sample_data") diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index 5c679c05..626869b8 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -17,7 +17,7 @@ class TestRqtlMapping(unittest.TestCase): @mock.patch("wqflask.marker_regression.rqtl_mapping.process_perm_results") @mock.patch("wqflask.marker_regression.rqtl_mapping.requests.post") @mock.patch("wqflask.marker_regression.rqtl_mapping.locate") - @mock.patch("wqflask.marker_regression.gemma_mapping.write_phenotype_file") + @mock.patch("wqflask.marker_regression.rqtl_mapping.write_phenotype_file") def test_run_rqtl_with_perm(self, mock_write_pheno_file, mock_locate, mock_post, mock_process_perm, mock_process_rqtl): """Test for run_rqtl with permutations > 0""" dataset_group = MockGroup( -- cgit v1.2.3 From cf9f0bef4454f97270bcacd16080f9d7f6bbb5e6 Mon Sep 17 00:00:00 2001 From: zsloan Date: Sat, 22 May 2021 22:51:19 +0000 Subject: Fixed the mocked return_value for requests.post to fix broken test --- wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index 626869b8..6853c021 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -28,7 +28,8 @@ class TestRqtlMapping(unittest.TestCase): mock_write_pheno_file.return_value = "pheno_filename" mock_locate.return_value = "geno_filename" - mock_post.return_value = "output_filename" + mock_post.return_value = {"output_file": "output_filename", + "rqtl_cmd": "the_command"} mock_process_perm.return_value = [[], 3, 4] mock_process_rqtl.return_value = [] -- cgit v1.2.3 From 2c2e1dc3d4be635dbf73aab380e22acca6a2014b Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 27 May 2021 20:18:36 +0000 Subject: Improved test_rqtl_mapping.py with Bonface's recommendation of uses dataclasses --- .../wqflask/marker_regression/test_rqtl_mapping.py | 23 ++++++++++------------ 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index 6853c021..bd97b2d2 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -2,17 +2,17 @@ import unittest from unittest import mock from wqflask.marker_regression.rqtl_mapping import run_rqtl -class AttributeSetter: - def __init__(self, obj): - for key, val in obj.items(): - setattr(self, key, val) +@dataclass +class MockGroup: + name: str, + genofile: str -class MockGroup(AttributeSetter): - def get_samplelist(self): - return None +@dataclass +class MockDataset: + group: MockGroup class TestRqtlMapping(unittest.TestCase): - + """Tests for functions in rqtl_mapping.py""" @mock.patch("wqflask.marker_regression.rqtl_mapping.process_rqtl_results") @mock.patch("wqflask.marker_regression.rqtl_mapping.process_perm_results") @mock.patch("wqflask.marker_regression.rqtl_mapping.requests.post") @@ -22,15 +22,12 @@ class TestRqtlMapping(unittest.TestCase): """Test for run_rqtl with permutations > 0""" dataset_group = MockGroup( {"name": "GP1", "genofile": "file_geno"}) - - dataset = AttributeSetter({"group": dataset_group}) + dataset = MockDataset(dataset_group) mock_write_pheno_file.return_value = "pheno_filename" mock_locate.return_value = "geno_filename" - mock_post.return_value = {"output_file": "output_filename", "rqtl_cmd": "the_command"} - mock_process_perm.return_value = [[], 3, 4] mock_process_rqtl.return_value = [] @@ -44,4 +41,4 @@ class TestRqtlMapping(unittest.TestCase): mock_post.assert_called_once() mock_process_perm.assert_called_once() mock_process_rqtl.assert_called_once() - self.assertEqual(results, ([], 3, 4, [])) \ No newline at end of file + self.assertEqual(results, ([], 3, 4, [])) -- cgit v1.2.3 From 37941d2724aaf22c1aeca18a77e4c17248c5b7bc Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 27 May 2021 20:27:26 +0000 Subject: Updated test_run_mapping.py to account for attribute keys being checked as lowercase, though not sure if this will fully fix the test --- .../unit/wqflask/marker_regression/test_run_mapping.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_run_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_run_mapping.py index 78cd3be9..c220a072 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_run_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_run_mapping.py @@ -229,20 +229,20 @@ class TestRunMapping(unittest.TestCase): used_samples = ["S1", "S2"] sample_list = AttributeSetter({"sample_attribute_values": { "S1": { - "C1": "c1_value", - "C2": "c2_value", - "W1": "w1_value" + "c1": "c1_value", + "c2": "c2_value", + "w1": "w1_value" }, "S2": { - "W1": "w2_value", - "W2": "w2_value" + "w1": "w2_value", + "w2": "w2_value" }, "S3": { - "C1": "c1_value", - "C2": "c2_value" + "c1": "c1_value", + "c2": "c2_value" }, -- cgit v1.2.3 From 09768ed9c255cdd10561eeba28bf3752747bf378 Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 27 May 2021 20:29:57 +0000 Subject: Removed parts of test_rqtl_mapping.py referring to process_perm_results and process_rqtl_results since that functionality was moved to GN3 --- .../tests/unit/wqflask/marker_regression/test_rqtl_mapping.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index bd97b2d2..00a05d2d 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -13,12 +13,10 @@ class MockDataset: class TestRqtlMapping(unittest.TestCase): """Tests for functions in rqtl_mapping.py""" - @mock.patch("wqflask.marker_regression.rqtl_mapping.process_rqtl_results") - @mock.patch("wqflask.marker_regression.rqtl_mapping.process_perm_results") @mock.patch("wqflask.marker_regression.rqtl_mapping.requests.post") @mock.patch("wqflask.marker_regression.rqtl_mapping.locate") @mock.patch("wqflask.marker_regression.rqtl_mapping.write_phenotype_file") - def test_run_rqtl_with_perm(self, mock_write_pheno_file, mock_locate, mock_post, mock_process_perm, mock_process_rqtl): + def test_run_rqtl_with_perm(self, mock_write_pheno_file, mock_locate, mock_post): """Test for run_rqtl with permutations > 0""" dataset_group = MockGroup( {"name": "GP1", "genofile": "file_geno"}) @@ -28,8 +26,6 @@ class TestRqtlMapping(unittest.TestCase): mock_locate.return_value = "geno_filename" mock_post.return_value = {"output_file": "output_filename", "rqtl_cmd": "the_command"} - mock_process_perm.return_value = [[], 3, 4] - mock_process_rqtl.return_value = [] results = run_rqtl(trait_name="the_trait", vals=[], samples=[], dataset=dataset, mapping_scale="cM", model="normal", method="hk", @@ -39,6 +35,5 @@ class TestRqtlMapping(unittest.TestCase): mock_write_pheno_file.assert_called_once() mock_locate.assert_called_once() mock_post.assert_called_once() - mock_process_perm.assert_called_once() - mock_process_rqtl.assert_called_once() + self.assertEqual(results, ([], 3, 4, [])) -- cgit v1.2.3 From 0cc4376462b7f136e59d99dba1975d6023326126 Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 27 May 2021 20:32:58 +0000 Subject: Fixed syntax mistake when creating dataclass in test_rqtl_mapping.py --- wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index 00a05d2d..31a2c9e4 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -4,7 +4,7 @@ from wqflask.marker_regression.rqtl_mapping import run_rqtl @dataclass class MockGroup: - name: str, + name: str genofile: str @dataclass -- cgit v1.2.3 From 97df46292e8322f01466d36b1e1e3ecdad64e5c0 Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 27 May 2021 20:50:03 +0000 Subject: Forgot to import dataclass --- wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index 31a2c9e4..ff451230 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -1,5 +1,7 @@ import unittest from unittest import mock +from dataclasses import dataclass + from wqflask.marker_regression.rqtl_mapping import run_rqtl @dataclass -- cgit v1.2.3 From 767ff7e97a751f164da610b2f9c536b6660ec420 Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 27 May 2021 20:53:22 +0000 Subject: Fixed way MockGroup was initialized --- wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index ff451230..a7f708e1 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -20,8 +20,7 @@ class TestRqtlMapping(unittest.TestCase): @mock.patch("wqflask.marker_regression.rqtl_mapping.write_phenotype_file") def test_run_rqtl_with_perm(self, mock_write_pheno_file, mock_locate, mock_post): """Test for run_rqtl with permutations > 0""" - dataset_group = MockGroup( - {"name": "GP1", "genofile": "file_geno"}) + dataset_group = MockGroup("GP1", "file_geno") dataset = MockDataset(dataset_group) mock_write_pheno_file.return_value = "pheno_filename" -- cgit v1.2.3 From f69eb61c5f58b008233f7bee0e551fe9151c7724 Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 27 May 2021 21:09:18 +0000 Subject: Change test_rqtl_mapping.py to account for full results being returned from the GN3 request --- wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index a7f708e1..9a5162ae 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -25,8 +25,11 @@ class TestRqtlMapping(unittest.TestCase): mock_write_pheno_file.return_value = "pheno_filename" mock_locate.return_value = "geno_filename" - mock_post.return_value = {"output_file": "output_filename", - "rqtl_cmd": "the_command"} + mock_post.return_value = Mock(ok=True) + mock_post.return_value.json.return_value = {"perm_results": [], + "suggestive": 3, + "significant": 4, + "results" : []} results = run_rqtl(trait_name="the_trait", vals=[], samples=[], dataset=dataset, mapping_scale="cM", model="normal", method="hk", -- cgit v1.2.3 From e78c84aa10849465e0272daa6d94bfbd3419b072 Mon Sep 17 00:00:00 2001 From: zsloan Date: Thu, 27 May 2021 21:11:16 +0000 Subject: Fix the way Mock is initialized --- wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wqflask/tests/unit') diff --git a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py index 9a5162ae..9d13e943 100644 --- a/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py +++ b/wqflask/tests/unit/wqflask/marker_regression/test_rqtl_mapping.py @@ -25,7 +25,7 @@ class TestRqtlMapping(unittest.TestCase): mock_write_pheno_file.return_value = "pheno_filename" mock_locate.return_value = "geno_filename" - mock_post.return_value = Mock(ok=True) + mock_post.return_value = mock.Mock(ok=True) mock_post.return_value.json.return_value = {"perm_results": [], "suggestive": 3, "significant": 4, -- cgit v1.2.3