added type-specific unittests
This commit is contained in:
@@ -1,62 +1,63 @@
|
||||
# """Definition of unittests for get_image function."""
|
||||
"""Definition of unittests for get_image function."""
|
||||
|
||||
# import os
|
||||
# import unittest
|
||||
# from unittest.mock import Mock
|
||||
# from minio import Minio
|
||||
# import random
|
||||
# import string
|
||||
# from PIL import Image
|
||||
# from io import BytesIO
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
# from shared.datastore import get_image
|
||||
from minio import Minio
|
||||
|
||||
from shared.datastore import get_image
|
||||
|
||||
|
||||
# class TestGetImage(unittest.TestCase):
|
||||
class TestGetImage(unittest.TestCase):
|
||||
|
||||
# def setUp(self):
|
||||
# # mock minio client
|
||||
# self.client = Mock(spec=Minio)
|
||||
# # mock response object
|
||||
# image = Image.new(mode='RGB', size=(480,480))
|
||||
# buffer = BytesIO()
|
||||
# image.save(buffer, 'png')
|
||||
# self.client.get_object.return_value.data = buffer.getvalue()
|
||||
# # set object name
|
||||
# self.object_name = ''.join(
|
||||
# random.choices(
|
||||
# string.ascii_uppercase + string.digits,
|
||||
# k=24
|
||||
# )
|
||||
# )
|
||||
# # populate env
|
||||
# self.env_var_map = {
|
||||
# 'MINIO_BUCKET_NAME': 'test-bucket'
|
||||
# }
|
||||
# for key, val in self.env_var_map.items():
|
||||
# os.environ[key] = val
|
||||
def setUp(self):
|
||||
# set relevant variables
|
||||
self.client = MagicMock(spec=Minio)
|
||||
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||
self.env_var_map = {
|
||||
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||
}
|
||||
# set bad arguments
|
||||
self.bad_client = 'not-minio-type'
|
||||
self.bad_string = float(0.0)
|
||||
self.len_0_string = ''
|
||||
# populate env
|
||||
for key, val in self.env_var_map.items():
|
||||
os.environ[key] = val
|
||||
|
||||
# def tearDown(self):
|
||||
# # clean env
|
||||
# for key in self.env_var_map:
|
||||
# _ = os.environ.pop(key, default=None)
|
||||
def tearDown(self):
|
||||
# clean env
|
||||
for key in self.env_var_map:
|
||||
_ = os.environ.pop(key, default=None)
|
||||
|
||||
# def test_should_fail_when_env_not_set(self):
|
||||
# # ensure env not set
|
||||
# self.tearDown()
|
||||
# # run test
|
||||
# with self.assertRaises(AssertionError):
|
||||
# get_image(
|
||||
# client=self.client,
|
||||
# object_name=self.object_name
|
||||
# )
|
||||
def test_should_fail_when_env_not_set(self):
|
||||
# ensure env not set
|
||||
self.tearDown()
|
||||
# run test
|
||||
with self.assertRaises(AssertionError):
|
||||
get_image(
|
||||
client=self.client,
|
||||
object_name=self.object_name,
|
||||
)
|
||||
|
||||
# def test_should_call_client__get_object(self):
|
||||
# get_image(
|
||||
# client=self.client,
|
||||
# object_name=self.object_name,
|
||||
# )
|
||||
# self.client.get_object.assert_called_with(
|
||||
# bucket_name=self.env_var_map['MINIO_BUCKET_NAME'],
|
||||
# object_name=f'images/{self.object_name}',
|
||||
# )
|
||||
def test_should_fail_on_wrong_input_type_client(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
get_image(
|
||||
client=self.bad_client,
|
||||
object_name=self.object_name,
|
||||
)
|
||||
|
||||
def test_should_fail_on_wrong_input_type_object_name(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
get_image(
|
||||
client=self.client,
|
||||
object_name=self.bad_string,
|
||||
)
|
||||
|
||||
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
get_image(
|
||||
client=self.client,
|
||||
object_name=self.len_0_string,
|
||||
)
|
||||
|
||||
@@ -1,62 +1,63 @@
|
||||
# """Definition of unittest for get_model function."""
|
||||
"""Definition of unittest for get_model function."""
|
||||
|
||||
# import os
|
||||
# import unittest
|
||||
# from unittest.mock import Mock
|
||||
# from minio import Minio
|
||||
# import random
|
||||
# import string
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
# from io import BytesIO
|
||||
from minio import Minio
|
||||
|
||||
# from shared.datastore import get_model
|
||||
from shared.datastore import get_model
|
||||
|
||||
|
||||
# class TestGetModel(unittest.TestCase):
|
||||
class TestGetModel(unittest.TestCase):
|
||||
|
||||
# def setUp(self):
|
||||
# # mock minio client
|
||||
# self.client = Mock(spec=Minio)
|
||||
# # mock response object
|
||||
# image = Image.new(mode='RGB', size=(480,480))
|
||||
# buffer = BytesIO()
|
||||
# image.save(buffer, 'png')
|
||||
# self.client.get_object.return_value.data = buffer.getvalue()
|
||||
# # set object name
|
||||
# self.object_name = ''.join(
|
||||
# random.choices(
|
||||
# string.ascii_uppercase + string.digits,
|
||||
# k=24
|
||||
# )
|
||||
# )
|
||||
# # populate env
|
||||
# self.env_var_map = {
|
||||
# 'MINIO_BUCKET_NAME': 'test-bucket'
|
||||
# }
|
||||
# for key, val in self.env_var_map.items():
|
||||
# os.environ[key] = val
|
||||
def setUp(self):
|
||||
# set relevant variables
|
||||
self.client = MagicMock(spec=Minio)
|
||||
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||
self.env_var_map = {
|
||||
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||
}
|
||||
# set bad arguments
|
||||
self.bad_client = 'not-minio-type'
|
||||
self.bad_string = float(0.0)
|
||||
self.len_0_string = ''
|
||||
# populate env
|
||||
for key, val in self.env_var_map.items():
|
||||
os.environ[key] = val
|
||||
|
||||
# def tearDown(self):
|
||||
# # clean env
|
||||
# for key in self.env_var_map:
|
||||
# _ = os.environ.pop(key, default=None)
|
||||
def tearDown(self):
|
||||
# clean env
|
||||
for key in self.env_var_map:
|
||||
_ = os.environ.pop(key, default=None)
|
||||
|
||||
# def test_should_fail_when_env_not_set(self):
|
||||
# # ensure env not set
|
||||
# self.tearDown()
|
||||
# # run test
|
||||
# with self.assertRaises(AssertionError):
|
||||
# get_image(
|
||||
# client=self.client,
|
||||
# object_name=self.object_name
|
||||
# )
|
||||
def test_should_fail_when_env_not_set(self):
|
||||
# ensure env not set
|
||||
self.tearDown()
|
||||
# run test
|
||||
with self.assertRaises(AssertionError):
|
||||
get_model(
|
||||
client=self.client,
|
||||
object_name=self.object_name,
|
||||
)
|
||||
|
||||
# def test_should_call_client__get_object(self):
|
||||
# get_image(
|
||||
# client=self.client,
|
||||
# object_name=self.object_name,
|
||||
# )
|
||||
# self.client.get_object.assert_called_with(
|
||||
# bucket_name=self.env_var_map['MINIO_BUCKET_NAME'],
|
||||
# object_name=f'images/{self.object_name}',
|
||||
# )
|
||||
def test_should_fail_on_wrong_input_type_client(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
get_model(
|
||||
client=self.bad_client,
|
||||
object_name=self.object_name,
|
||||
)
|
||||
|
||||
def test_should_fail_on_wrong_input_type_object_name(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
get_model(
|
||||
client=self.client,
|
||||
object_name=self.bad_string,
|
||||
)
|
||||
|
||||
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
get_model(
|
||||
client=self.client,
|
||||
object_name=self.len_0_string,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Definition of unittests for put_image function."""
|
||||
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from minio import Minio
|
||||
from PIL import Image
|
||||
|
||||
from shared.datastore import put_image
|
||||
|
||||
|
||||
class TestPutImage(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
# set relevant variables
|
||||
self.client = MagicMock(spec=Minio)
|
||||
self.image = Image.new(mode='RGB', size=(480, 480))
|
||||
self.env_var_map = {
|
||||
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||
}
|
||||
# set bad arguments
|
||||
self.bad_client = 'not-minio-type'
|
||||
self.bad_image = 'not-image-type'
|
||||
# populate env
|
||||
for key, val in self.env_var_map.items():
|
||||
os.environ[key] = val
|
||||
|
||||
def test_should_fail_on_wrong_input_type_client(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
put_image(
|
||||
client=self.bad_client,
|
||||
image=self.image,
|
||||
)
|
||||
|
||||
def test_should_fail_on_wrong_input_type_image(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
put_image(
|
||||
client=self.client,
|
||||
image=self.bad_image,
|
||||
)
|
||||
@@ -0,0 +1,40 @@
|
||||
"""Definition of unittests for put_model function."""
|
||||
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from minio import Minio
|
||||
from torch.nn import Module
|
||||
|
||||
from shared.datastore import put_model
|
||||
|
||||
|
||||
class TestPutModel(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# set relevant variables
|
||||
self.client = MagicMock(spec=Minio)
|
||||
self.model = MagicMock(spec=Module)
|
||||
self.env_var_map = {
|
||||
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||
}
|
||||
# set bad arguments
|
||||
self.bad_client = 'not-minio-type'
|
||||
self.bad_model = 'not-image-type'
|
||||
# populate env
|
||||
for key, val in self.env_var_map.items():
|
||||
os.environ[key] = val
|
||||
|
||||
def test_should_fail_on_wrong_input_type_client(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
put_model(
|
||||
client=self.bad_client,
|
||||
model=self.model,
|
||||
)
|
||||
|
||||
def test_should_fail_on_wrong_input_type_model(self):
|
||||
with self.assertRaises(AssertionError):
|
||||
put_model(
|
||||
client=self.client,
|
||||
model=self.bad_model,
|
||||
)
|
||||
Reference in New Issue
Block a user