aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMunyoki Kilyungi2022-09-05 15:20:52 +0300
committerBonfaceKilz2022-09-08 14:26:19 +0300
commitea9315da2333fc327616f94093e2603b8532e53a (patch)
tree9d0c619ed0112774a26801203ab04fab9083a223
parent4a3b8a0bbd7365eb56a77f6cb6252aaf799d5ecc (diff)
downloadgenenetwork2-ea9315da2333fc327616f94093e2603b8532e53a.tar.gz
Replace "g.db" mocks with "database_connection" mocks
* wqflask/tests/unit/base/test_trait.py: Delete "wqflask.app" import. (TestRetrieveTraitInfo.setUp): Delete it. (TestRetrieveTraitInfo.tearDown): Ditto. (TestRetrieveTraitInfo.test_retrieve_trait_info_with_empty_dataset): Replace "g" mock with "database_connection" mock. (test_retrieve_trait_info_with_empty_trait_info): Ditto. (test_retrieve_trait_info_with_non_empty_trait_info): Ditto. (test_retrieve_trait_info_utf8_parsing): Ditto. * wqflask/tests/wqflask/show_trait/test_show_trait.py (TestTraits): (test_get_nearest_marker): Ditto. (test_get_nearest_marker_empty_db): Ditto.
-rw-r--r--wqflask/tests/unit/base/test_trait.py28
-rw-r--r--wqflask/tests/wqflask/show_trait/test_show_trait.py68
2 files changed, 56 insertions, 40 deletions
diff --git a/wqflask/tests/unit/base/test_trait.py b/wqflask/tests/unit/base/test_trait.py
index 71fe0a44..e8c01ca9 100644
--- a/wqflask/tests/unit/base/test_trait.py
+++ b/wqflask/tests/unit/base/test_trait.py
@@ -3,7 +3,6 @@
import unittest
from unittest import mock
-from wqflask import app
from base.trait import GeneralTrait
from base.trait import retrieve_trait_info
@@ -32,25 +31,24 @@ class MockTrait(GeneralTrait):
class TestRetrieveTraitInfo(unittest.TestCase):
"""Tests for 'retrieve_trait_info'"""
-
- def setUp(self):
- self.app_context = app.app_context()
- self.app_context.push()
-
- def tearDown(self):
- self.app_context.pop()
-
- def test_retrieve_trait_info_with_empty_dataset(self):
+ @mock.patch('base.trait.database_connection')
+ def test_retrieve_trait_info_with_empty_dataset(self, mock_db):
"""Test that an exception is raised when dataset is empty"""
- with self.assertRaises(AssertionError):
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
+ with self.assertRaises(ValueError):
retrieve_trait_info(trait=mock.MagicMock(),
dataset={})
@mock.patch('base.trait.requests.get')
@mock.patch('base.trait.g', mock.Mock())
+ @mock.patch('base.trait.database_connection')
def test_retrieve_trait_info_with_empty_trait_info(self,
+ mock_db,
requests_mock):
"""Empty trait info"""
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
requests_mock.return_value = TestNilResponse()
with self.assertRaises(KeyError):
retrieve_trait_info(trait=mock.MagicMock(),
@@ -58,10 +56,14 @@ class TestRetrieveTraitInfo(unittest.TestCase):
@mock.patch('base.trait.requests.get')
@mock.patch('base.trait.g', mock.Mock())
+ @mock.patch('base.trait.database_connection')
def test_retrieve_trait_info_with_non_empty_trait_info(self,
+ mock_db,
requests_mock):
"""Test that attributes are set"""
mock_dataset = mock.MagicMock()
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
requests_mock.return_value = TestResponse()
type(mock_dataset).display_fields = mock.PropertyMock(
return_value=["a", "b", "c", "d"])
@@ -74,10 +76,14 @@ class TestRetrieveTraitInfo(unittest.TestCase):
@mock.patch('base.trait.requests.get')
@mock.patch('base.trait.g', mock.Mock())
+ @mock.patch('base.trait.database_connection')
def test_retrieve_trait_info_utf8_parsing(self,
+ mock_db,
requests_mock):
"""Test that utf-8 strings are parsed correctly"""
utf_8_string = "test_string"
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
mock_dataset = mock.MagicMock()
requests_mock.return_value = TestResponse()
type(mock_dataset).display_fields = mock.PropertyMock(
diff --git a/wqflask/tests/wqflask/show_trait/test_show_trait.py b/wqflask/tests/wqflask/show_trait/test_show_trait.py
index 5f4598f5..6a6b1587 100644
--- a/wqflask/tests/wqflask/show_trait/test_show_trait.py
+++ b/wqflask/tests/wqflask/show_trait/test_show_trait.py
@@ -143,39 +143,49 @@ class TestTraits(unittest.TestCase):
self.assertEqual(results, "this is a descriptionN/A")
self.assertEqual(results_no_unit, "Value")
- @mock.patch("wqflask.show_trait.show_trait.g")
+ @mock.patch("wqflask.show_trait.show_trait.database_connection")
def test_get_nearest_marker(self, mock_db):
"""test for getting nearest marker with non-empty db"""
-
- mock_db.db.execute.return_value.fetchall.return_value = [
- ["Geno1", "Geno2"], ["Geno3"]]
-
- trait = TraitObject({"locus_chr": "test_chr", "locus_mb": "test_mb"})
- group_name = TraitObject({"name": "group_name"})
- this_db = TraitObject({"group": group_name})
- results_with_item_db = get_nearest_marker(trait, this_db)
- called_with_value = """SELECT Geno.Name
- FROM Geno, GenoXRef, GenoFreeze
- WHERE Geno.Chr = 'test_chr' AND
- GenoXRef.GenoId = Geno.Id AND
- GenoFreeze.Id = GenoXRef.GenoFreezeId AND
- GenoFreeze.Name = 'group_nameGeno'
- ORDER BY ABS( Geno.Mb - test_mb) LIMIT 1"""
-
- mock_db.db.execute.assert_called_with(called_with_value)
-
- self.assertEqual(results_with_item_db, "Geno1")
-
- @mock.patch("wqflask.show_trait.show_trait.g")
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
+ with conn.cursor() as cursor:
+ cursor.fetchall.return_value = [
+ ["Geno1", "Geno2"], ["Geno3"]]
+
+ trait = TraitObject({
+ "locus_chr": "test_chr",
+ "locus_mb": "test_mb"
+ })
+ group_name = TraitObject({"name": "group_name"})
+ this_db = TraitObject({"group": group_name})
+ results_with_item_db = get_nearest_marker(trait, this_db)
+ cursor.execute.assert_called_with(
+ "SELECT Geno.Name FROM Geno, GenoXRef, "
+ "GenoFreeze WHERE Geno.Chr = %s "
+ "AND GenoXRef.GenoId = Geno.Id AND "
+ "GenoFreeze.Id = GenoXRef.GenoFreezeId "
+ "AND GenoFreeze.Name = %s "
+ "ORDER BY ABS( Geno.Mb - %s) LIMIT 1",
+ ('test_chr', 'group_nameGeno', 'test_mb'))
+
+ self.assertEqual(results_with_item_db, "Geno1")
+
+ @mock.patch("wqflask.show_trait.show_trait.database_connection")
def test_get_nearest_marker_empty_db(self, mock_db):
"""test for getting nearest marker with empty db"""
- mock_db.db.execute.return_value.fetchall.return_value = []
- trait = TraitObject({"locus_chr": "test_chr", "locus_mb": "test_mb"})
- group_name = TraitObject({"name": "group_name"})
- this_db = TraitObject({"group": group_name})
- results_empty_db = get_nearest_marker(trait, this_db)
- mock_db.db.execute.assert_called_once()
- self.assertEqual(results_empty_db, "")
+ conn = mock.MagicMock()
+ mock_db.return_value.__enter__.return_value = conn
+ with conn.cursor() as cursor:
+ cursor.fetchall.return_value = []
+ trait = TraitObject({
+ "locus_chr": "test_chr",
+ "locus_mb": "test_mb"
+ })
+ group_name = TraitObject({"name": "group_name"})
+ this_db = TraitObject({"group": group_name})
+ results_empty_db = get_nearest_marker(trait, this_db)
+ cursor.execute.assert_called_once()
+ self.assertEqual(results_empty_db, "")
@mock.patch("wqflask.show_trait.show_trait.get_scales_from_genofile")
def test_get_genotype_scales_with_genofile_is_list(self, mock_get_scales):