aboutsummaryrefslogtreecommitdiff
path: root/wqflask/tests
diff options
context:
space:
mode:
authorBonfaceKilz2022-03-17 16:11:10 +0300
committerBonfaceKilz2022-03-17 16:24:30 +0300
commitd742153d57e3f755781553fcbc32ad96295e7192 (patch)
treea9293ae8cd73e56318fc565315b0e10e1c517ba1 /wqflask/tests
parenteca135dd8dbde3ee36c4258d4b02d636722998bd (diff)
downloadgenenetwork2-d742153d57e3f755781553fcbc32ad96295e7192.tar.gz
Replace "g.db" object with a proper database connection
* wqflask/tests/unit/wqflask/api/test_correlation.py: Use proper database connection instead of the db connection attached at "g.db". * wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py: Ditto. * wqflask/wqflask/api/correlation.py: Ditto. * wqflask/wqflask/snp_browser/snp_browser.py: Ditto.
Diffstat (limited to 'wqflask/tests')
-rw-r--r--wqflask/tests/unit/wqflask/api/test_correlation.py69
-rw-r--r--wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py132
2 files changed, 113 insertions, 88 deletions
diff --git a/wqflask/tests/unit/wqflask/api/test_correlation.py b/wqflask/tests/unit/wqflask/api/test_correlation.py
index 1089a36f..57986e80 100644
--- a/wqflask/tests/unit/wqflask/api/test_correlation.py
+++ b/wqflask/tests/unit/wqflask/api/test_correlation.py
@@ -52,28 +52,28 @@ class TestCorrelations(unittest.TestCase):
self.assertEqual(corr_params_results, expected_results)
- @mock.patch("wqflask.api.correlation.g")
+ @mock.patch("wqflask.api.correlation.database_connection")
def test_convert_to_mouse_gene_id(self, mock_db):
-
- results = convert_to_mouse_gene_id(species="Other", gene_id="")
- self.assertEqual(results, None)
-
- rat_species_results = convert_to_mouse_gene_id(
- species="rat", gene_id="GH1")
-
- mock_db.db.execute.return_value.fetchone.side_effect = [
- AttributeSetter({"mouse": "MG-1"}), AttributeSetter({"mouse": "MG-2"})]
-
- self.assertEqual(convert_to_mouse_gene_id(
- species="mouse", gene_id="MG-4"), "MG-4")
- self.assertEqual(convert_to_mouse_gene_id(
- species="rat", gene_id="R1"), "MG-1")
- self.assertEqual(convert_to_mouse_gene_id(
- species="human", gene_id="H1"), "MG-2")
-
- @mock.patch("wqflask.api.correlation.g")
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
+ with conn.cursor() as cursor:
+ cursor.fetchone.side_effect = [
+ ("MG-1",), ("MG-2",)]
+
+ self.assertEqual(convert_to_mouse_gene_id(
+ species="Other", gene_id=""), None)
+ self.assertEqual(convert_to_mouse_gene_id(
+ species="mouse", gene_id="MG-4"), "MG-4")
+ self.assertEqual(convert_to_mouse_gene_id(
+ species="rat", gene_id="R1"), "MG-1")
+ self.assertEqual(convert_to_mouse_gene_id(
+ species="human", gene_id="H1"), "MG-2")
+
+ @mock.patch("wqflask.api.correlation.database_connection")
@mock.patch("wqflask.api.correlation.convert_to_mouse_gene_id")
- def test_do_literature_correlation_for_all_traits(self, mock_convert_to_mouse_geneid, mock_db):
+ def test_do_literature_correlation_for_all_traits(
+ self,
+ mock_convert_to_mouse_geneid, mock_db):
mock_convert_to_mouse_geneid.side_effect = [
"MG-1", "MG-2;", "MG-3", "MG-4"]
@@ -83,19 +83,22 @@ class TestCorrelations(unittest.TestCase):
"TT-3": "GH-3"
}
- mock_db.db.execute.return_value.fetchone.side_effect = [AttributeSetter(
- {"value": "V1"}), AttributeSetter({"value": "V2"}), AttributeSetter({"value": "V3"})]
-
- this_trait = AttributeSetter({"geneid": "GH-1"})
-
- target_dataset = AttributeSetter(
- {"group": AttributeSetter({"species": "rat"})})
- results = do_literature_correlation_for_all_traits(
- this_trait=this_trait, target_dataset=target_dataset, trait_geneid_dict=trait_geneid_dict, corr_params={})
-
- expected_results = {'TT-1': ['GH-1', 0],
- 'TT-2': ['GH-2', 'V1'], 'TT-3': ['GH-3', 'V2']}
- self.assertEqual(results, expected_results)
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
+ with conn.cursor() as cursor:
+ cursor.fetchone.side_effect = [
+ AttributeSetter({"value": "V1"}),
+ AttributeSetter({"value": "V2"}),
+ AttributeSetter({"value": "V3"})]
+ this_trait = AttributeSetter({"geneid": "GH-1"})
+ target_dataset = AttributeSetter(
+ {"group": AttributeSetter({"species": "rat"})})
+ results = do_literature_correlation_for_all_traits(
+ this_trait=this_trait, target_dataset=target_dataset,
+ trait_geneid_dict=trait_geneid_dict, corr_params={})
+ expected_results = {'TT-1': ['GH-1', 0],
+ 'TT-2': ['GH-2', 'V1'], 'TT-3': ['GH-3', 'V2']}
+ self.assertEqual(results, expected_results)
@mock.patch("wqflask.api.correlation.corr_result_helpers.normalize_values")
def test_get_sample_r_and_p_values(self, mock_normalize):
diff --git a/wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py b/wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py
index 89442c47..46a74231 100644
--- a/wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py
+++ b/wqflask/tests/unit/wqflask/snp_browser/test_snp_browser.py
@@ -40,66 +40,88 @@ class TestSnpBrowser(unittest.TestCase):
self.assertEqual(expected_results, results_with_snp)
self.assertEqual(expected_results_with_indel, results_with_indel)
- @mock.patch("wqflask.snp_browser.snp_browser.g")
+ @mock.patch("wqflask.snp_browser.snp_browser.database_connection")
def test_get_gene_id(self, mock_db):
- mock_db.db.execute.return_value.fetchone.return_value = "517d729f-aa13-4413-a885-40a3f7ff768a"
- db_query_value = """
- SELECT
- geneId
- FROM
- GeneList
- WHERE
- SpeciesId = c9c0f59e-1259-4cba-91e6-831ef1a99c83 AND geneSymbol = 'INSR'
- """
- results = get_gene_id(
- species_id="c9c0f59e-1259-4cba-91e6-831ef1a99c83", gene_name="INSR")
- mock_db.db.execute.assert_called_once_with(db_query_value)
- self.assertEqual(results, "517d729f-aa13-4413-a885-40a3f7ff768a")
+ db_query_value = ("SELECT geneId FROM GeneList WHERE "
+ "SpeciesId = %s AND geneSymbol = %s")
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
+ with conn.cursor() as cursor:
+ cursor.fetchone.return_value = (("517d729f-aa13-4413"
+ "-a885-40a3f7ff768a"),)
- @mock.patch("wqflask.snp_browser.snp_browser.g")
+ results = get_gene_id(
+ species_id="c9c0f59e-1259-4cba-91e6-831ef1a99c83",
+ gene_name="INSR")
+ cursor.execute.assert_called_once_with(
+ db_query_value,
+ ("c9c0f59e-1259-4cba-91e6-831ef1a99c83",
+ "INSR"))
+ self.assertEqual(results,
+ "517d729f-aa13-4413-a885-40a3f7ff768a")
+
+ @mock.patch("wqflask.snp_browser.snp_browser.database_connection")
def test_gene_id_name_dict(self, mock_db):
no_gene_names = []
- self.assertEqual("", get_gene_id_name_dict(
- species_id="fregb343bui43g4", gene_name_list=no_gene_names))
- gene_name_list = ["GH1", "GH2", "GH3"]
- mock_db.db.execute.return_value.fetchall.side_effect = [[], [("fsdf43-fseferger-f22", "GH1"), ("1sdf43-fsewferger-f22", "GH2"),
- ("fwdj43-fstferger-f22", "GH3")]]
- no_results = get_gene_id_name_dict(
- species_id="ret3-32rf32", gene_name_list=gene_name_list)
- results_found = get_gene_id_name_dict(
- species_id="ret3-32rf32", gene_name_list=gene_name_list)
- expected_found = {'GH1': 'fsdf43-fseferger-f22',
- 'GH2': '1sdf43-fsewferger-f22', 'GH3': 'fwdj43-fstferger-f22'}
- db_query_value = """
- SELECT
- geneId, geneSymbol
- FROM
- GeneList
- WHERE
- SpeciesId = ret3-32rf32 AND geneSymbol in ('GH1','GH2','GH3')
- """
- mock_db.db.execute.assert_called_with(db_query_value)
- self.assertEqual(results_found, expected_found)
- self.assertEqual(no_results, {})
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
+ with conn.cursor() as cursor:
+ cursor.fetchall.side_effect = [
+ [],
+ [("fsdf43-fseferger-f22", "GH1"),
+ ("1sdf43-fsewferger-f22", "GH2"),
+ ("fwdj43-fstferger-f22", "GH3")]]
+ self.assertEqual("", get_gene_id_name_dict(
+ species_id="fregb343bui43g4",
+ gene_name_list=no_gene_names))
+ gene_name_list = ["GH1", "GH2", "GH3"]
+ no_results = get_gene_id_name_dict(
+ species_id="ret3-32rf32", gene_name_list=gene_name_list)
+ results_found = get_gene_id_name_dict(
+ species_id="ret3-32rf32", gene_name_list=gene_name_list)
+ expected_found = {'GH1': 'fsdf43-fseferger-f22',
+ 'GH2': '1sdf43-fsewferger-f22',
+ 'GH3': 'fwdj43-fstferger-f22'}
+ db_query_value = (
+ "SELECT geneId, geneSymbol FROM GeneList WHERE "
+ "SpeciesId = %s AND geneSymbol in (%s, %s, %s)")
+ cursor.execute.assert_called_with(
+ db_query_value, ("ret3-32rf32", "GH1", "GH2", "GH3"))
+ self.assertEqual(results_found, expected_found)
+ self.assertEqual(no_results, {})
- @mock.patch("wqflask.snp_browser.snp_browser.g")
+ @mock.patch("wqflask.snp_browser.snp_browser.database_connection")
def test_check_if_in_gene(self, mock_db):
- mock_db.db.execute.return_value.fetchone.side_effect = [
- ("fsdf-232sdf-sdf", "GHA"), ""]
- results_found = check_if_in_gene(
- species_id="517d729f-aa13-4413-a885-40a3f7ff768a", chr="CH1", mb=12.09)
- db_query_value = """SELECT geneId, geneSymbol
- FROM GeneList
- WHERE SpeciesId = 517d729f-aa13-4413-a885-40a3f7ff768a AND chromosome = 'CH1' AND
- (txStart < 12.09 AND txEnd > 12.09); """
- gene_not_found = check_if_in_gene(
- species_id="517d729f-aa13-4413-a885-40a3f7ff768a", chr="CH1", mb=12.09)
- mock_db.db.execute.assert_called_with(db_query_value)
- self.assertEqual(gene_not_found, "")
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
+ with conn.cursor() as cursor:
+ cursor.fetchone.side_effect = [
+ ("fsdf-232sdf-sdf", "GHA"), ""]
+ results_found = check_if_in_gene(
+ species_id="517d729f-aa13-4413-a885-40a3f7ff768a",
+ chr_="CH1", mb=12.09)
+ self.assertEqual(results_found, ["fsdf-232sdf-sdf", "GHA"])
+ db_query_value = (
+ "SELECT geneId, geneSymbol FROM GeneList "
+ "WHERE SpeciesId = %s AND chromosome = %s "
+ "AND (txStart < %s AND txEnd > %s)")
+ gene_not_found = check_if_in_gene(
+ species_id="517d729f-aa13-4413-a885-40a3f7ff768a",
+ chr_="CH1", mb=12.09)
+ cursor.execute.assert_has_calls(
+ [mock.call(db_query_value,
+ ("517d729f-aa13-4413-a885-40a3f7ff768a",
+ "CH1", 12.09, 12.09)),
+ mock.call(db_query_value,
+ ("517d729f-aa13-4413-a885-40a3f7ff768a",
+ "CH1", 12.09, 12.09))])
+ self.assertEqual(gene_not_found, "")
- @mock.patch("wqflask.snp_browser.snp_browser.g")
+ @mock.patch("wqflask.snp_browser.snp_browser.database_connection")
def test_get_browser_sample_lists(self, mock_db):
- mock_db.db.execute.return_value.fetchall.return_value = []
-
- results = get_browser_sample_lists(species_id="12")
- self.assertEqual(results, {'mouse': [], 'rat': []})
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
+ with conn.cursor() as cursor:
+ cursor.execute.return_value.fetchall.return_value = []
+ results = get_browser_sample_lists(species_id="12")
+ self.assertEqual(results, {'mouse': [], 'rat': []})