Skip to content

Commit ab0cf74

Browse files
committed
fix: Correct mock paths and config handling in health tests
1 parent 9d756eb commit ab0cf74

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

tests/test_health/test_controllers.py

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
class TestHealthEndpoints(BaseTestCase):
1010
"""Test health check endpoints."""
1111

12-
def test_health_endpoint_returns_200_when_healthy(self):
12+
@mock.patch('mod_health.controllers.check_config')
13+
@mock.patch('mod_health.controllers.check_database')
14+
def test_health_endpoint_returns_200_when_healthy(self, mock_db, mock_config):
1315
"""Test that /health returns 200 when all checks pass."""
16+
mock_db.return_value = {'status': 'ok'}
17+
mock_config.return_value = {'status': 'ok'}
18+
1419
response = self.app.test_client().get('/health')
1520
self.assertEqual(response.status_code, 200)
1621

@@ -21,27 +26,33 @@ def test_health_endpoint_returns_200_when_healthy(self):
2126
self.assertEqual(data['checks']['database']['status'], 'ok')
2227
self.assertEqual(data['checks']['config']['status'], 'ok')
2328

24-
def test_health_endpoint_returns_503_when_database_fails(self):
29+
@mock.patch('mod_health.controllers.check_config')
30+
@mock.patch('mod_health.controllers.check_database')
31+
def test_health_endpoint_returns_503_when_database_fails(self, mock_db, mock_config):
2532
"""Test that /health returns 503 when database check fails."""
26-
with mock.patch('mod_health.controllers.check_database') as mock_db:
27-
mock_db.return_value = {'status': 'error', 'message': 'Connection failed'}
28-
response = self.app.test_client().get('/health')
29-
self.assertEqual(response.status_code, 503)
33+
mock_db.return_value = {'status': 'error', 'message': 'Connection failed'}
34+
mock_config.return_value = {'status': 'ok'}
35+
36+
response = self.app.test_client().get('/health')
37+
self.assertEqual(response.status_code, 503)
3038

31-
data = json.loads(response.data)
32-
self.assertEqual(data['status'], 'unhealthy')
33-
self.assertEqual(data['checks']['database']['status'], 'error')
39+
data = json.loads(response.data)
40+
self.assertEqual(data['status'], 'unhealthy')
41+
self.assertEqual(data['checks']['database']['status'], 'error')
3442

35-
def test_health_endpoint_returns_503_when_config_fails(self):
43+
@mock.patch('mod_health.controllers.check_config')
44+
@mock.patch('mod_health.controllers.check_database')
45+
def test_health_endpoint_returns_503_when_config_fails(self, mock_db, mock_config):
3646
"""Test that /health returns 503 when config check fails."""
37-
with mock.patch('mod_health.controllers.check_config') as mock_config:
38-
mock_config.return_value = {'status': 'error', 'message': 'Missing keys'}
39-
response = self.app.test_client().get('/health')
40-
self.assertEqual(response.status_code, 503)
47+
mock_db.return_value = {'status': 'ok'}
48+
mock_config.return_value = {'status': 'error', 'message': 'Missing keys'}
4149

42-
data = json.loads(response.data)
43-
self.assertEqual(data['status'], 'unhealthy')
44-
self.assertEqual(data['checks']['config']['status'], 'error')
50+
response = self.app.test_client().get('/health')
51+
self.assertEqual(response.status_code, 503)
52+
53+
data = json.loads(response.data)
54+
self.assertEqual(data['status'], 'unhealthy')
55+
self.assertEqual(data['checks']['config']['status'], 'error')
4556

4657
def test_liveness_endpoint_returns_200(self):
4758
"""Test that /health/live always returns 200."""
@@ -52,23 +63,31 @@ def test_liveness_endpoint_returns_200(self):
5263
self.assertEqual(data['status'], 'alive')
5364
self.assertIn('timestamp', data)
5465

55-
def test_readiness_endpoint_returns_200_when_healthy(self):
66+
@mock.patch('mod_health.controllers.check_config')
67+
@mock.patch('mod_health.controllers.check_database')
68+
def test_readiness_endpoint_returns_200_when_healthy(self, mock_db, mock_config):
5669
"""Test that /health/ready returns 200 when healthy."""
70+
mock_db.return_value = {'status': 'ok'}
71+
mock_config.return_value = {'status': 'ok'}
72+
5773
response = self.app.test_client().get('/health/ready')
5874
self.assertEqual(response.status_code, 200)
5975

6076
data = json.loads(response.data)
6177
self.assertEqual(data['status'], 'healthy')
6278

63-
def test_readiness_endpoint_returns_503_when_unhealthy(self):
79+
@mock.patch('mod_health.controllers.check_config')
80+
@mock.patch('mod_health.controllers.check_database')
81+
def test_readiness_endpoint_returns_503_when_unhealthy(self, mock_db, mock_config):
6482
"""Test that /health/ready returns 503 when unhealthy."""
65-
with mock.patch('mod_health.controllers.check_database') as mock_db:
66-
mock_db.return_value = {'status': 'error', 'message': 'Connection failed'}
67-
response = self.app.test_client().get('/health/ready')
68-
self.assertEqual(response.status_code, 503)
83+
mock_db.return_value = {'status': 'error', 'message': 'Connection failed'}
84+
mock_config.return_value = {'status': 'ok'}
6985

70-
data = json.loads(response.data)
71-
self.assertEqual(data['status'], 'unhealthy')
86+
response = self.app.test_client().get('/health/ready')
87+
self.assertEqual(response.status_code, 503)
88+
89+
data = json.loads(response.data)
90+
self.assertEqual(data['status'], 'unhealthy')
7291

7392

7493
class TestHealthCheckFunctions(BaseTestCase):
@@ -85,7 +104,8 @@ def test_check_database_failure(self):
85104
"""Test check_database returns error when database fails."""
86105
from mod_health.controllers import check_database
87106
with self.app.app_context():
88-
with mock.patch('mod_health.controllers.create_session') as mock_session:
107+
# Mock at the source module where it's imported from
108+
with mock.patch('database.create_session') as mock_session:
89109
mock_session.side_effect = Exception('Connection refused')
90110
result = check_database()
91111
self.assertEqual(result['status'], 'error')
@@ -95,18 +115,17 @@ def test_check_config_success(self):
95115
"""Test check_config returns ok when config is complete."""
96116
from mod_health.controllers import check_config
97117
with self.app.app_context():
118+
# Set required config values for test
119+
self.app.config['GITHUB_TOKEN'] = 'test_token'
98120
result = check_config()
99121
self.assertEqual(result['status'], 'ok')
100122

101123
def test_check_config_missing_keys(self):
102124
"""Test check_config returns error when keys are missing."""
103125
from mod_health.controllers import check_config
104126
with self.app.app_context():
105-
# Temporarily remove a required key
106-
original_value = self.app.config.get('GITHUB_TOKEN')
127+
# Ensure GITHUB_TOKEN is empty to trigger error
107128
self.app.config['GITHUB_TOKEN'] = ''
108129
result = check_config()
109130
self.assertEqual(result['status'], 'error')
110131
self.assertIn('GITHUB_TOKEN', result['message'])
111-
# Restore
112-
self.app.config['GITHUB_TOKEN'] = original_value

0 commit comments

Comments
 (0)