File size: 6,621 Bytes
459b8f5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
import os
import unittest
from io import BytesIO
from unittest import mock
import app.image_classifier_app as ui_app
from PIL import Image
path_tests = os.path.dirname(os.path.abspath(__file__))
class TestMLService(unittest.TestCase):
# π‘ NOTE Run tests with: python tests/test_image_classifier_app.py
def setUp(self):
self.token = "dummy_token"
self.image_file = Image.open(path_tests + "/dog.jpeg")
self.uploaded_file = mock.MagicMock(spec=BytesIO)
self.uploaded_file.getvalue.return_value = BytesIO()
self.uploaded_file.name = "dog.jpeg"
self.image_file.save(self.uploaded_file, format="JPEG")
self.headers = {"Authorization": f"Bearer {self.token}"}
# python3 -m unittest -vvv tests.test_model
def test_login_success(self):
# π‘ NOTE Run test with: python -m unittest -vvv tests.test_image_classifier_app.TestMLService.test_login_success
expected_token = "dummy_token"
response_data = {"access_token": expected_token}
with mock.patch("requests.post") as mock_post:
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = response_data
token = ui_app.login("username", "password")
headers = {
"accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
}
self.assertEqual(token, expected_token)
mock_post.assert_called_once_with(
ui_app.API_BASE_URL + "/login",
headers=headers,
data={
"grant_type": "",
"username": "username",
"password": "password",
"scope": "",
"client_id": "",
"client_secret": "",
},
)
def test_login_failure(self):
# π‘ NOTE Run test with: python -m unittest -vvv tests.test_image_classifier_app.TestMLService.test_login_failure
with mock.patch("requests.post") as mock_post:
mock_post.return_value.status_code = 401
token = ui_app.login("username", "password")
headers = {
"accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
}
self.assertIsNone(token)
mock_post.assert_called_once_with(
ui_app.API_BASE_URL + "/login",
headers=headers,
data={
"grant_type": "",
"username": "username",
"password": "password",
"scope": "",
"client_id": "",
"client_secret": "",
},
)
def test_predict_success(self):
# π‘ NOTE Run test with: python -m unittest -vvv tests.test_image_classifier_app.TestMLService.test_predict_success
expected_response = {"prediction": "Eskimo_dog", "score": 0.9346}
with mock.patch("requests.post") as mock_post:
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = expected_response
response = ui_app.predict(self.token, self.uploaded_file)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), expected_response)
mock_post.assert_called_once_with(
ui_app.API_BASE_URL + "/model/predict",
files={
"file": (self.uploaded_file.name, self.uploaded_file.getvalue())
},
headers=self.headers,
)
def test_predict_failure(self):
# π‘ NOTE Run test with: python -m unittest -vvv tests.test_image_classifier_app.TestMLService.test_predict_failure
with mock.patch("requests.post") as mock_post:
mock_post.return_value.status_code = 500
response = ui_app.predict(self.token, self.uploaded_file)
self.assertEqual(response.status_code, 500)
mock_post.assert_called_once_with(
ui_app.API_BASE_URL + "/model/predict",
files={
"file": (self.uploaded_file.name, self.uploaded_file.getvalue())
},
headers=self.headers,
)
def test_send_feedback_success(self):
# π‘ NOTE Run test with: python -m unittest -vvv tests.test_image_classifier_app.TestMLService.test_send_feedback_success
expected_response = {"status": "success"}
feedback = "This is a feedback"
score = 0.9346
prediction = "Eskimo_dog"
image_file_name = "dog.jpeg"
with mock.patch("requests.post") as mock_post:
mock_post.return_value.status_code = 201
mock_post.return_value.json.return_value = expected_response
response = ui_app.send_feedback(
self.token, feedback, score, prediction, image_file_name
)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json(), expected_response)
mock_post.assert_called_once_with(
ui_app.API_BASE_URL + "/feedback",
json={
"feedback": feedback,
"score": score,
"predicted_class": prediction,
"image_file_name": image_file_name,
},
headers=self.headers,
)
def test_send_feedback_failure(self):
# π‘ NOTE Run test with: python -m unittest -vvv tests.test_image_classifier_app.TestMLService.test_send_feedback_failure
feedback = "This is a feedback"
score = 0.9346
prediction = "Eskimo_dog"
image_file_name = "dog.jpeg"
with mock.patch("requests.post") as mock_post:
mock_post.return_value.status_code = 500
response = ui_app.send_feedback(
self.token, feedback, score, prediction, image_file_name
)
self.assertEqual(response.status_code, 500)
mock_post.assert_called_once_with(
ui_app.API_BASE_URL + "/feedback",
json={
"feedback": feedback,
"score": score,
"predicted_class": prediction,
"image_file_name": image_file_name,
},
headers=self.headers,
)
if __name__ == "__main__":
unittest.main(verbosity=2)
|