From ea9315da2333fc327616f94093e2603b8532e53a Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Mon, 5 Sep 2022 15:20:52 +0300 Subject: 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. --- wqflask/tests/unit/base/test_trait.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'wqflask/tests/unit/base') 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( -- cgit v1.2.3