Skip to content

Commit 08da750

Browse files
committed
Add type hints
1 parent 311ea9f commit 08da750

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

python_test_example/flask_app/app_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from unittest import TestCase, main
3-
from unittest.mock import patch
3+
from unittest.mock import MagicMock, patch
44

55
print('In app_test')
66

@@ -39,7 +39,7 @@ def test_predict_data(self):
3939
self.assertIsInstance(json.loads(response.data)['result'], float)
4040

4141
@patch('flask_app.service.Service.check_model', return_value=False)
42-
def test_predict_503(self, mock):
42+
def test_predict_503(self, mock: MagicMock):
4343
response = self.client.post('/predict')
4444
self.assertEqual(response.status_code, 503)
4545

python_test_example/flask_app/service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def __init__(self):
1010
self.model = load_model()
1111
print(f'self.model: {self.model}')
1212

13-
def predict(self, target: str):
13+
def predict(self, target: str) -> float:
1414
print('Call predict in service')
1515
return randint(0, 1000) / 1000.0
1616

17-
def check_model(self):
17+
def check_model(self) -> bool:
1818
if self.model:
1919
return True
2020
return False

python_test_example/mock/my_class.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import requests
22

33
class MyClass:
4-
def fetch_json(self, url: str):
4+
def fetch_json(self, url: str) -> dict:
55
response = requests.get(url)
66
return response.json()

python_test_example/mock/my_class_test.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from unittest import TestCase, main, mock
1+
from unittest import TestCase, main
2+
from unittest.mock import MagicMock, call, patch
23

34
from mock.my_class import MyClass
45

@@ -19,8 +20,8 @@ def json(self):
1920

2021
return MockResponse(None, 404)
2122

22-
@mock.patch('requests.get', side_effect=mocked_requests_get)
23-
def test_fetch_json(self, mock_get):
23+
@patch('requests.get', side_effect=mocked_requests_get)
24+
def test_fetch_json(self, mock_get: MagicMock):
2425
my_class = MyClass()
2526

2627
json_data = my_class.fetch_json('http://example.com/test.json')
@@ -31,10 +32,10 @@ def test_fetch_json(self, mock_get):
3132
self.assertIsNone(json_data)
3233

3334
self.assertIn(
34-
mock.call('http://example.com/test.json'), mock_get.call_args_list
35+
call('http://example.com/test.json'), mock_get.call_args_list
3536
)
3637
self.assertIn(
37-
mock.call('http://example.com/another_test.json'), mock_get.call_args_list
38+
call('http://example.com/another_test.json'), mock_get.call_args_list
3839
)
3940

4041
self.assertEqual(len(mock_get.call_args_list), 3)

0 commit comments

Comments
 (0)