|
| 1 | +import json |
| 2 | +from unittest import TestCase, main |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +print('In app_test') |
| 6 | + |
| 7 | +class AppTestCase(TestCase): |
| 8 | + def setUp(self): |
| 9 | + print('Call setUp in AppTestCase') |
| 10 | + |
| 11 | + self.load_model_patcher = patch('flask_app.model.load_model') |
| 12 | + self.load_model_m = self.load_model_patcher.start() |
| 13 | + self.load_model_m.return_value = 'test' |
| 14 | + |
| 15 | + from flask_app.app import app |
| 16 | + self.client = app.test_client() |
| 17 | + |
| 18 | + def tearDown(self): |
| 19 | + self.load_model_patcher.stop() |
| 20 | + |
| 21 | + def test_index(self): |
| 22 | + response = self.client.get('/') |
| 23 | + self.assertEqual(response.status_code, 200) |
| 24 | + |
| 25 | + def test_index_content(self): |
| 26 | + response = self.client.get('/') |
| 27 | + self.assertEqual(response.content_type, 'application/json') |
| 28 | + |
| 29 | + def test_predict(self): |
| 30 | + response = self.client.post('/predict') |
| 31 | + self.assertEqual(response.status_code, 200) |
| 32 | + |
| 33 | + def test_predict_content(self): |
| 34 | + response = self.client.post('/predict') |
| 35 | + self.assertEqual(response.content_type, 'application/json') |
| 36 | + |
| 37 | + def test_predict_data(self): |
| 38 | + response = self.client.post('/predict') |
| 39 | + self.assertIsInstance(json.loads(response.data)['result'], float) |
| 40 | + |
| 41 | + @patch('flask_app.service.Service.check_model', return_value=False) |
| 42 | + def test_predict_503(self, mock: MagicMock): |
| 43 | + response = self.client.post('/predict') |
| 44 | + self.assertEqual(response.status_code, 503) |
| 45 | + |
| 46 | +if __name__ == '__main__': |
| 47 | + main() |
0 commit comments