Skip to content

Commit

Permalink
fix tets
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebCourier committed May 31, 2024
1 parent a0325b5 commit 5b7ba42
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
5 changes: 2 additions & 3 deletions src/blueprints/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion tests/blueprints/test_root.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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"]
97 changes: 48 additions & 49 deletions tests/clients/test_guard_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
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


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()
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")

Expand All @@ -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")
Expand All @@ -126,15 +125,15 @@ 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")
mock_first = mocker.patch.object(mock_pg_client.db.session, "first")
latest_guard = MockGuardStruct("latest")
mock_first.return_value = latest_guard

guard_client = GuardClient()
guard_client = PGGuardClient()

result = guard_client.get_guard_item("guard")

Expand All @@ -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")
Expand 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()

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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

Expand All @@ -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)

Expand All @@ -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")
Expand All @@ -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")

Expand Down

0 comments on commit 5b7ba42

Please sign in to comment.