From f9d23c5bd43612621764e1a1682e5ed874f9c77f Mon Sep 17 00:00:00 2001 From: brian Date: Sat, 16 Nov 2024 18:50:28 +0000 Subject: [PATCH] added type-specific unittests --- shared/datastore/tests/unit/get_image_test.py | 109 +++++++++--------- shared/datastore/tests/unit/get_model_test.py | 107 ++++++++--------- shared/datastore/tests/unit/put_image_test.py | 41 +++++++ shared/datastore/tests/unit/put_model_test.py | 40 +++++++ 4 files changed, 190 insertions(+), 107 deletions(-) create mode 100644 shared/datastore/tests/unit/put_image_test.py create mode 100644 shared/datastore/tests/unit/put_model_test.py diff --git a/shared/datastore/tests/unit/get_image_test.py b/shared/datastore/tests/unit/get_image_test.py index 4322838..ab72324 100644 --- a/shared/datastore/tests/unit/get_image_test.py +++ b/shared/datastore/tests/unit/get_image_test.py @@ -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, + ) diff --git a/shared/datastore/tests/unit/get_model_test.py b/shared/datastore/tests/unit/get_model_test.py index c2c132f..0ba1f76 100644 --- a/shared/datastore/tests/unit/get_model_test.py +++ b/shared/datastore/tests/unit/get_model_test.py @@ -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, + ) diff --git a/shared/datastore/tests/unit/put_image_test.py b/shared/datastore/tests/unit/put_image_test.py new file mode 100644 index 0000000..e971478 --- /dev/null +++ b/shared/datastore/tests/unit/put_image_test.py @@ -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, + ) diff --git a/shared/datastore/tests/unit/put_model_test.py b/shared/datastore/tests/unit/put_model_test.py new file mode 100644 index 0000000..790e839 --- /dev/null +++ b/shared/datastore/tests/unit/put_model_test.py @@ -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, + )