added unittests for base functions
This commit is contained in:
@@ -30,4 +30,4 @@ class TestConnectMinio(unittest.TestCase):
|
|||||||
self.tearDown()
|
self.tearDown()
|
||||||
# run test
|
# run test
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
connect_minio()
|
_ = connect_minio()
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
"""Definition of unittests for delete function."""
|
"""Definition of unittests for delete function."""
|
||||||
|
|
||||||
import random
|
|
||||||
import string
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import Mock
|
from unittest.mock import Mock
|
||||||
|
|
||||||
@@ -13,22 +11,19 @@ from shared.datastore import delete
|
|||||||
class TestDelete(unittest.TestCase):
|
class TestDelete(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# mock minio client
|
# set relevant variables
|
||||||
self.client = Mock(spec=Minio)
|
self.client = Mock(spec=Minio)
|
||||||
# set bucket name
|
|
||||||
self.bucket_name = 'test-bucket'
|
self.bucket_name = 'test-bucket'
|
||||||
# set object name
|
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||||
self.object_name = ''.join(
|
# set bad arguments
|
||||||
random.choices(
|
self.bad_minio_client = 'not-minio-type'
|
||||||
string.ascii_uppercase + string.digits,
|
self.bad_string = float(0.0)
|
||||||
k=24,
|
self.len_0_string = ''
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_should_fail_on_wrong_input_type_client(self):
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
delete(
|
delete(
|
||||||
client='not-minio-type',
|
client=self.bad_minio_client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
@@ -37,7 +32,7 @@ class TestDelete(unittest.TestCase):
|
|||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
delete(
|
delete(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=0.0,
|
bucket_name=self.bad_string,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -45,7 +40,7 @@ class TestDelete(unittest.TestCase):
|
|||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
delete(
|
delete(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name='',
|
bucket_name=self.len_0_string,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,7 +49,7 @@ class TestDelete(unittest.TestCase):
|
|||||||
delete(
|
delete(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name=0.0,
|
object_name=self.bad_string,
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_should_fail_on_wrong_input_length_object_name(self):
|
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||||
@@ -62,16 +57,5 @@ class TestDelete(unittest.TestCase):
|
|||||||
delete(
|
delete(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name='',
|
object_name=self.len_0_string,
|
||||||
)
|
|
||||||
|
|
||||||
def test_should_call_client__remove_object(self):
|
|
||||||
delete(
|
|
||||||
client=self.client,
|
|
||||||
bucket_name=self.bucket_name,
|
|
||||||
object_name=self.object_name,
|
|
||||||
)
|
|
||||||
self.client.remove_object.assert_called_with(
|
|
||||||
bucket_name=self.bucket_name,
|
|
||||||
object_name=self.object_name,
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
"""Definition of unittests for get function."""
|
"""Definition of unittests for get function."""
|
||||||
|
|
||||||
import random
|
|
||||||
import string
|
|
||||||
import unittest
|
import unittest
|
||||||
from io import BytesIO
|
from unittest.mock import MagicMock
|
||||||
from unittest.mock import Mock
|
|
||||||
|
|
||||||
from minio import Minio
|
from minio import Minio
|
||||||
|
|
||||||
@@ -12,29 +9,20 @@ from shared.datastore import get
|
|||||||
|
|
||||||
|
|
||||||
class TestGet(unittest.TestCase):
|
class TestGet(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# mock minio client
|
# set relevant variables
|
||||||
self.client = Mock(spec=Minio)
|
self.client = MagicMock(spec=Minio)
|
||||||
# set bucket name
|
|
||||||
self.bucket_name = 'test-bucket'
|
self.bucket_name = 'test-bucket'
|
||||||
# set object name
|
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||||
self.object_name = ''.join(
|
# set bad arguments
|
||||||
random.choices(
|
self.bad_minio_client = 'not-minio-type'
|
||||||
string.ascii_uppercase + string.digits,
|
self.bad_string = float(0.0)
|
||||||
k=24,
|
self.len_0_string = ''
|
||||||
),
|
|
||||||
)
|
|
||||||
# mock response object
|
|
||||||
self.client.get_object.return_value.status = 200
|
|
||||||
buffer = BytesIO(random.randbytes(2**20))
|
|
||||||
self.client.return_value.get_object = buffer
|
|
||||||
self.client.get_object.return_value.read = buffer.getvalue()
|
|
||||||
|
|
||||||
def test_should_fail_on_wrong_input_type_client(self):
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
get(
|
get(
|
||||||
client='not-minio-type',
|
client=self.bad_minio_client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
@@ -43,7 +31,7 @@ class TestGet(unittest.TestCase):
|
|||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
get(
|
get(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=0.0,
|
bucket_name=self.bad_string,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -51,7 +39,7 @@ class TestGet(unittest.TestCase):
|
|||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
get(
|
get(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name='',
|
bucket_name=self.len_0_string,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -60,7 +48,7 @@ class TestGet(unittest.TestCase):
|
|||||||
get(
|
get(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name=0.0,
|
object_name=self.bad_string,
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_should_fail_on_wrong_input_length_object_name(self):
|
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||||
@@ -68,16 +56,5 @@ class TestGet(unittest.TestCase):
|
|||||||
get(
|
get(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name='',
|
object_name=self.len_0_string,
|
||||||
)
|
|
||||||
|
|
||||||
def test_should_call_client__get_object(self):
|
|
||||||
get(
|
|
||||||
client=self.client,
|
|
||||||
bucket_name=self.bucket_name,
|
|
||||||
object_name=self.object_name,
|
|
||||||
)
|
|
||||||
self.client.get_object.assert_called_with(
|
|
||||||
bucket_name=self.bucket_name,
|
|
||||||
object_name=self.object_name,
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
"""Definition of unittests for put function."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from io import BytesIO
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from minio import Minio
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from shared.datastore import put
|
||||||
|
|
||||||
|
|
||||||
|
class TestPut(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
# set relevant variables
|
||||||
|
self.client = MagicMock(spec=Minio)
|
||||||
|
self.bucket_name = 'test-bucket'
|
||||||
|
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||||
|
self.image = Image.new(mode='RGB', size=(480, 480))
|
||||||
|
self.buffer = BytesIO()
|
||||||
|
self.image.save(self.buffer, 'png')
|
||||||
|
# set bad arguments
|
||||||
|
self.bad_minio_client = 'not-minio-type'
|
||||||
|
self.bad_string = float(0.0)
|
||||||
|
self.bad_buffer = 'not-buffer-type'
|
||||||
|
self.len_0_string = ''
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.bad_minio_client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.bucket_name,
|
||||||
|
object_name=self.object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_buffer(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.bad_minio_client,
|
||||||
|
buffer=self.bad_buffer,
|
||||||
|
bucket_name=self.bucket_name,
|
||||||
|
object_name=self.object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_bucket_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.bad_string,
|
||||||
|
object_name=self.object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_length_bucket_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.len_0_string,
|
||||||
|
object_name=self.object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_object_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.bucket_name,
|
||||||
|
object_name=self.bad_string,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.bucket_name,
|
||||||
|
object_name=self.len_0_string,
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user