diff --git a/src/blueprints/root.py b/src/blueprints/root.py index 4f966db..18a6cd9 100644 --- a/src/blueprints/root.py +++ b/src/blueprints/root.py @@ -5,7 +5,7 @@ from flask import Blueprint from sqlalchemy import text from src.classes.health_check import HealthCheck -from src.clients.postgres_client import PostgresClient +from src.clients.postgres_client import PostgresClient, postgres_is_enabled from src.utils.handle_error import handle_error from src.utils.logger import logger @@ -25,8 +25,7 @@ def home(): @handle_error def health_check(): # If we're not using postgres, just return Ok - pg_host = os.environ.get("PGHOST", None) - if pg_host is None: + if not postgres_is_enabled(): return HealthCheck(200, "Ok").to_dict() # Make sure we're connected to the database and can run queries pg_client = PostgresClient() diff --git a/tests/blueprints/test_root.py b/tests/blueprints/test_root.py index e0a85fe..3754748 100644 --- a/tests/blueprints/test_root.py +++ b/tests/blueprints/test_root.py @@ -1,3 +1,4 @@ +import os from src.utils.logger import logger from tests.mocks.mock_blueprint import MockBlueprint from tests.mocks.mock_postgres_client import MockPostgresClient @@ -17,6 +18,7 @@ def test_home(mocker): def test_health_check(mocker): + os.environ["PGHOST"] = "localhost" mocker.patch("flask.Blueprint", new=MockBlueprint) mock_pg = MockPostgresClient() @@ -34,10 +36,11 @@ def text_side_effect(query: str): response = health_check() - assert mock_text.called_once_with("SELECT count(datid) FROM pg_stat_activity;") + mock_text.assert_called_once_with("SELECT count(datid) FROM pg_stat_activity;") assert mock_pg.db.session.queries == ["SELECT count(datid) FROM pg_stat_activity;"] info_spy.assert_called_once_with("response: %s", [(1,)]) assert response == {"status": 200, "message": "Ok"} mocker.resetall() + del os.environ["PGHOST"] diff --git a/tests/clients/test_guard_client.py b/tests/clients/test_guard_client.py index bf345f3..7c28e2b 100644 --- a/tests/clients/test_guard_client.py +++ b/tests/clients/test_guard_client.py @@ -6,7 +6,6 @@ from src.clients.pg_guard_client import PGGuardClient from src.models.guard_item import GuardItem from src.models.guard_item_audit import GuardItemAudit -from src.clients.guard_client import GuardClient from tests.mocks.mock_postgres_client import MockPostgresClient from tests.mocks.mock_guard_client import MockGuardStruct, MockRailspec from unittest.mock import call @@ -14,7 +13,7 @@ def test_init(mocker): mock_pg_client = MockPostgresClient() - mocker.patch("src.clients.guard_client.PostgresClient", return_value=mock_pg_client) + mocker.patch("src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client) pg_guard_client = PGGuardClient() # mem_guard_client = MemoryGuardClient() @@ -28,7 +27,7 @@ class TestGetGuard: def test_get_latest(self, mocker): mock_pg_client = MockPostgresClient() mocker.patch( - "src.clients.guard_client.PostgresClient", return_value=mock_pg_client + "src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client ) query_spy = mocker.spy(mock_pg_client.db.session, "query") @@ -38,25 +37,25 @@ def test_get_latest(self, mocker): mock_first.return_value = latest_guard mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) mock_from_guard_item.return_value = latest_guard - guard_client = GuardClient() + guard_client = PGGuardClient() result = guard_client.get_guard("guard") query_spy.assert_called_once_with(GuardItem) filter_by_spy.assert_called_once_with(name="guard") assert mock_first.call_count == 1 - assert mock_from_guard_item.called_once_with(latest_guard) + mock_from_guard_item.assert_called_once_with(latest_guard) assert result == latest_guard def test_with_as_of_date(self, mocker): mock_pg_client = MockPostgresClient() mocker.patch( - "src.clients.guard_client.PostgresClient", return_value=mock_pg_client + "src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client ) query_spy = mocker.spy(mock_pg_client.db.session, "query") @@ -69,11 +68,11 @@ def test_with_as_of_date(self, mocker): mock_first.side_effect = [latest_guard, previous_guard] mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) mock_from_guard_item.return_value = previous_guard - guard_client = GuardClient() + guard_client = PGGuardClient() result = guard_client.get_guard("guard", as_of_date="2024-03-06") @@ -94,23 +93,23 @@ def test_with_as_of_date(self, mocker): assert replaced_on_order_exp.compare(order_by_spy_call) assert mock_first.call_count == 2 - assert mock_from_guard_item.called_once_with(previous_guard) + mock_from_guard_item.assert_called_once_with(previous_guard) assert result == previous_guard def test_raises_not_found(self, mocker): mock_pg_client = MockPostgresClient() mocker.patch( - "src.clients.guard_client.PostgresClient", return_value=mock_pg_client + "src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client ) mock_first = mocker.patch.object(mock_pg_client.db.session, "first") mock_first.return_value = None mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) - guard_client = GuardClient() + guard_client = PGGuardClient() with pytest.raises(HttpError) as exc_info: guard_client.get_guard("guard") @@ -126,7 +125,7 @@ def test_raises_not_found(self, mocker): def test_get_guard_item(mocker): mock_pg_client = MockPostgresClient() - mocker.patch("src.clients.guard_client.PostgresClient", return_value=mock_pg_client) + mocker.patch("src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client) query_spy = mocker.spy(mock_pg_client.db.session, "query") filter_by_spy = mocker.spy(mock_pg_client.db.session, "filter_by") @@ -134,7 +133,7 @@ def test_get_guard_item(mocker): latest_guard = MockGuardStruct("latest") mock_first.return_value = latest_guard - guard_client = GuardClient() + guard_client = PGGuardClient() result = guard_client.get_guard_item("guard") @@ -147,7 +146,7 @@ def test_get_guard_item(mocker): def test_get_guards(mocker): mock_pg_client = MockPostgresClient() - mocker.patch("src.clients.guard_client.PostgresClient", return_value=mock_pg_client) + mocker.patch("src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client) query_spy = mocker.spy(mock_pg_client.db.session, "query") mock_all = mocker.patch.object(mock_pg_client.db.session, "all") @@ -157,11 +156,11 @@ def test_get_guards(mocker): mock_all.return_value = guards mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) mock_from_guard_item.side_effect = [guard_one, guard_two] - guard_client = GuardClient() + guard_client = PGGuardClient() result = guard_client.get_guards() @@ -179,18 +178,18 @@ def test_create_guard(mocker): mock_guard = MockGuardStruct() mock_pg_client = MockPostgresClient() mock_guard_struct_init_spy = mocker.spy(MockGuardStruct, "__init__") - mocker.patch("src.clients.guard_client.PostgresClient", return_value=mock_pg_client) - mocker.patch("src.clients.guard_client.GuardItem", new=MockGuardStruct) + mocker.patch("src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client) + mocker.patch("src.clients.pg_guard_client.GuardItem", new=MockGuardStruct) add_spy = mocker.spy(mock_pg_client.db.session, "add") commit_spy = mocker.spy(mock_pg_client.db.session, "commit") mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) mock_from_guard_item.return_value = mock_guard - guard_client = GuardClient() + guard_client = PGGuardClient() result = guard_client.create_guard(mock_guard) @@ -219,19 +218,19 @@ def test_raises_not_found(self, mocker): mock_guard = MockGuardStruct() mock_pg_client = MockPostgresClient() mocker.patch( - "src.clients.guard_client.PostgresClient", return_value=mock_pg_client + "src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client ) mock_get_guard_item = mocker.patch( - "src.clients.guard_client.GuardClient.get_guard_item" + "src.clients.pg_guard_client.PGGuardClient.get_guard_item" ) mock_get_guard_item.return_value = None commit_spy = mocker.spy(mock_pg_client.db.session, "commit") mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) - guard_client = GuardClient() + guard_client = PGGuardClient() with pytest.raises(HttpError) as exc_info: guard_client.update_guard("mock-guard", mock_guard) @@ -251,21 +250,21 @@ def test_updates_guard_item(self, mocker): updated_guard = MockGuardStruct(num_reasks=2) mock_pg_client = MockPostgresClient() mocker.patch( - "src.clients.guard_client.PostgresClient", return_value=mock_pg_client + "src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client ) mock_get_guard_item = mocker.patch( - "src.clients.guard_client.GuardClient.get_guard_item" + "src.clients.pg_guard_client.PGGuardClient.get_guard_item" ) mock_get_guard_item.return_value = old_guard to_dict_spy = mocker.spy(updated_guard.railspec, "to_dict") commit_spy = mocker.spy(mock_pg_client.db.session, "commit") mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) mock_from_guard_item.return_value = updated_guard - guard_client = GuardClient() + guard_client = PGGuardClient() result = guard_client.update_guard("mock-guard", updated_guard) @@ -287,30 +286,30 @@ def test_guard_doesnt_exist_yet(self, mocker): new_guard = MockGuardStruct() mock_pg_client = MockPostgresClient() mocker.patch( - "src.clients.guard_client.PostgresClient", return_value=mock_pg_client + "src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client ) mock_get_guard_item = mocker.patch( - "src.clients.guard_client.GuardClient.get_guard_item" + "src.clients.pg_guard_client.PGGuardClient.get_guard_item" ) mock_get_guard_item.return_value = None commit_spy = mocker.spy(mock_pg_client.db.session, "commit") mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) mock_create_guard = mocker.patch( - "src.clients.guard_client.GuardClient.create_guard" + "src.clients.pg_guard_client.PGGuardClient.create_guard" ) mock_create_guard.return_value = new_guard - guard_client = GuardClient() + guard_client = PGGuardClient() result = guard_client.upsert_guard("mock-guard", input_guard) - assert mock_get_guard_item.called_once_with("mock-guard") + mock_get_guard_item.assert_called_once_with("mock-guard") assert commit_spy.call_count == 0 assert mock_from_guard_item.call_count == 0 - assert mock_create_guard.called_once_with(input_guard) + mock_create_guard.assert_called_once_with(input_guard) assert result == new_guard @@ -319,21 +318,21 @@ def test_guard_already_exists(self, mocker): updated_guard = MockGuardStruct(num_reasks=2, description="updated description") mock_pg_client = MockPostgresClient() mocker.patch( - "src.clients.guard_client.PostgresClient", return_value=mock_pg_client + "src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client ) mock_get_guard_item = mocker.patch( - "src.clients.guard_client.GuardClient.get_guard_item" + "src.clients.pg_guard_client.PGGuardClient.get_guard_item" ) mock_get_guard_item.return_value = old_guard to_dict_spy = mocker.spy(updated_guard.railspec, "to_dict") commit_spy = mocker.spy(mock_pg_client.db.session, "commit") mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) mock_from_guard_item.return_value = updated_guard - guard_client = GuardClient() + guard_client = PGGuardClient() result = guard_client.upsert_guard("mock-guard", updated_guard) @@ -353,19 +352,19 @@ class TestDeleteGuard: def test_raises_not_found(self, mocker): mock_pg_client = MockPostgresClient() mocker.patch( - "src.clients.guard_client.PostgresClient", return_value=mock_pg_client + "src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client ) mock_get_guard_item = mocker.patch( - "src.clients.guard_client.GuardClient.get_guard_item" + "src.clients.pg_guard_client.PGGuardClient.get_guard_item" ) mock_get_guard_item.return_value = None commit_spy = mocker.spy(mock_pg_client.db.session, "commit") mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) - guard_client = GuardClient() + guard_client = PGGuardClient() with pytest.raises(HttpError) as exc_info: guard_client.delete_guard("mock-guard") @@ -384,21 +383,21 @@ def test_deletes_guard_item(self, mocker): old_guard = MockGuardStruct() mock_pg_client = MockPostgresClient() mocker.patch( - "src.clients.guard_client.PostgresClient", return_value=mock_pg_client + "src.clients.pg_guard_client.PostgresClient", return_value=mock_pg_client ) mock_get_guard_item = mocker.patch( - "src.clients.guard_client.GuardClient.get_guard_item" + "src.clients.pg_guard_client.PGGuardClient.get_guard_item" ) mock_get_guard_item.return_value = old_guard delete_spy = mocker.spy(mock_pg_client.db.session, "delete") commit_spy = mocker.spy(mock_pg_client.db.session, "commit") mock_from_guard_item = mocker.patch( - "src.clients.guard_client.GuardStruct.from_guard_item" + "src.clients.pg_guard_client.GuardStruct.from_guard_item" ) mock_from_guard_item.return_value = old_guard - guard_client = GuardClient() + guard_client = PGGuardClient() result = guard_client.delete_guard("mock-guard")