added type-specific unittests

This commit is contained in:
brian
2024-11-16 18:50:28 +00:00
parent 3fa79faa9e
commit f9d23c5bd4
4 changed files with 190 additions and 107 deletions
+55 -54
View File
@@ -1,62 +1,63 @@
# """Definition of unittests for get_image function.""" """Definition of unittests for get_image function."""
# import os import os
# import unittest import unittest
# from unittest.mock import Mock from unittest.mock import MagicMock
# from minio import Minio
# import random
# import string
# from PIL import Image
# from io import BytesIO
# 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): def setUp(self):
# # mock minio client # set relevant variables
# self.client = Mock(spec=Minio) self.client = MagicMock(spec=Minio)
# # mock response object self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
# image = Image.new(mode='RGB', size=(480,480)) self.env_var_map = {
# buffer = BytesIO() 'MINIO_BUCKET_NAME': 'test-bucket',
# image.save(buffer, 'png') }
# self.client.get_object.return_value.data = buffer.getvalue() # set bad arguments
# # set object name self.bad_client = 'not-minio-type'
# self.object_name = ''.join( self.bad_string = float(0.0)
# random.choices( self.len_0_string = ''
# string.ascii_uppercase + string.digits, # populate env
# k=24 for key, val in self.env_var_map.items():
# ) os.environ[key] = val
# )
# # 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 tearDown(self): def tearDown(self):
# # clean env # clean env
# for key in self.env_var_map: for key in self.env_var_map:
# _ = os.environ.pop(key, default=None) _ = os.environ.pop(key, default=None)
# def test_should_fail_when_env_not_set(self): def test_should_fail_when_env_not_set(self):
# # ensure env not set # ensure env not set
# self.tearDown() self.tearDown()
# # run test # run test
# with self.assertRaises(AssertionError): with self.assertRaises(AssertionError):
# get_image( get_image(
# client=self.client, client=self.client,
# object_name=self.object_name object_name=self.object_name,
# ) )
# def test_should_call_client__get_object(self): def test_should_fail_on_wrong_input_type_client(self):
# get_image( with self.assertRaises(AssertionError):
# client=self.client, get_image(
# object_name=self.object_name, client=self.bad_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_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,
)
+54 -53
View File
@@ -1,62 +1,63 @@
# """Definition of unittest for get_model function.""" """Definition of unittest for get_model function."""
# import os import os
# import unittest import unittest
# from unittest.mock import Mock from unittest.mock import MagicMock
# from minio import Minio
# import random
# import string
# 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): def setUp(self):
# # mock minio client # set relevant variables
# self.client = Mock(spec=Minio) self.client = MagicMock(spec=Minio)
# # mock response object self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
# image = Image.new(mode='RGB', size=(480,480)) self.env_var_map = {
# buffer = BytesIO() 'MINIO_BUCKET_NAME': 'test-bucket',
# image.save(buffer, 'png') }
# self.client.get_object.return_value.data = buffer.getvalue() # set bad arguments
# # set object name self.bad_client = 'not-minio-type'
# self.object_name = ''.join( self.bad_string = float(0.0)
# random.choices( self.len_0_string = ''
# string.ascii_uppercase + string.digits, # populate env
# k=24 for key, val in self.env_var_map.items():
# ) os.environ[key] = val
# )
# # 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 tearDown(self): def tearDown(self):
# # clean env # clean env
# for key in self.env_var_map: for key in self.env_var_map:
# _ = os.environ.pop(key, default=None) _ = os.environ.pop(key, default=None)
# def test_should_fail_when_env_not_set(self): def test_should_fail_when_env_not_set(self):
# # ensure env not set # ensure env not set
# self.tearDown() self.tearDown()
# # run test # run test
# with self.assertRaises(AssertionError): with self.assertRaises(AssertionError):
# get_image( get_model(
# client=self.client, client=self.client,
# object_name=self.object_name object_name=self.object_name,
# ) )
# def test_should_call_client__get_object(self): def test_should_fail_on_wrong_input_type_client(self):
# get_image( with self.assertRaises(AssertionError):
# client=self.client, get_model(
# object_name=self.object_name, client=self.bad_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_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,
)