"""Tests for healthcheck module.""" from unittest.mock import MagicMock import pytest from contracts import response from contracts.logic import healthcheck @pytest.fixture def fixture_response_ok(): """Create a response.Response fixture for generic ok.""" return response.Response(message=dict(status='ok'), status=200) @pytest.fixture def fixture_response_error(): """Create a response.Response fixture for generic error.""" return response.create_error_response( code='error_code', message='error_message', status=500 ) def test_status(monkeypatch, fixture_response_ok): """Test status.""" monkeypatch.setattr( healthcheck.response, 'create_status_ok_response', value=MagicMock(return_value=fixture_response_ok), ) assert healthcheck.status().message == dict(status='ok') assert healthcheck.response.create_status_ok_response.called def test_db_status_success(monkeypatch, fixture_response_ok): """Assert that database heartbeat returns expected response.""" monkeypatch.setattr( healthcheck.response, 'create_status_ok_response', value=MagicMock(return_value=fixture_response_ok), ) monkeypatch.setattr( healthcheck.healthcheck_model, 'check_db', value=MagicMock(return_value=True) ) success_response = healthcheck.db_status() assert healthcheck.healthcheck_model.check_db.called assert success_response.status == 200 assert success_response.message == dict(status='ok') def test_db_status_error(monkeypatch, fixture_response_error): """Assert that database heartbeat returns expected response.""" monkeypatch.setattr( healthcheck.response, 'create_error_response', value=MagicMock(return_value=fixture_response_error), ) monkeypatch.setattr( healthcheck.healthcheck_model, 'check_db', value=MagicMock(return_value=False) ) error_response = healthcheck.db_status() assert healthcheck.healthcheck_model.check_db.called assert error_response.status == 500 assert error_response.errors == dict(code='error_code', message='error_message')