From 98e9726405df3cce81356534335259a446b0c458 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 23 Mar 2023 18:41:38 +0300 Subject: tests: Set up fixture and mock out config variable --- tests/unit/test_db_utils.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'tests/unit/test_db_utils.py') diff --git a/tests/unit/test_db_utils.py b/tests/unit/test_db_utils.py index 68f80a0..a319692 100644 --- a/tests/unit/test_db_utils.py +++ b/tests/unit/test_db_utils.py @@ -6,9 +6,14 @@ from types import SimpleNamespace import pytest +import gn3 from gn3.db_utils import database_connector from gn3.db_utils import parse_db_url +@pytest.fixture(scope="class") +def setup_app(request, fxtr_app): + """Setup the fixtures for the class.""" + request.cls.app = fxtr_app class TestDatabase(TestCase): """class contains testd for db connection functions""" @@ -31,10 +36,14 @@ class TestDatabase(TestCase): results, SimpleNamespace, "database not created successfully") @pytest.mark.unit_test - @mock.patch("gn3.db_utils.SQL_URI", - "mysql://username:4321@localhost/test") + @pytest.mark.usefixtures("setup_app") def test_parse_db_url(self): """test for parsing db_uri env variable""" - results = parse_db_url() - expected_results = ("localhost", "username", "4321", "test", None) - self.assertEqual(results, expected_results) + print(self.__dict__) + with self.app.app_context(), mock.patch.dict(# pylint: disable=[no-member] + gn3.db_utils.current_app.config, + {"SQL_URI": "mysql://username:4321@localhost/test"}, + clear=True): + results = parse_db_url() + expected_results = ("localhost", "username", "4321", "test", None) + self.assertEqual(results, expected_results) -- cgit v1.2.3