diff --git a/shared/datastore/tests/unit/get_test.py b/shared/datastore/tests/unit/get_test.py new file mode 100644 index 0000000..017132d --- /dev/null +++ b/shared/datastore/tests/unit/get_test.py @@ -0,0 +1,83 @@ +"""Definition of unittests for get function.""" + +import random +import string +import unittest +from io import BytesIO +from unittest.mock import Mock + +from minio import Minio + +from shared.datastore import get + + +class TestGet(unittest.TestCase): + + def setUp(self): + # mock minio client + self.client = Mock(spec=Minio) + # set bucket name + self.bucket_name = 'test-bucket' + # set object name + self.object_name = ''.join( + random.choices( + string.ascii_uppercase + string.digits, + k=24, + ), + ) + # 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): + with self.assertRaises(AssertionError): + get( + client='not-minio-type', + 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): + get( + client=self.client, + bucket_name=0.0, + object_name=self.object_name, + ) + + def test_should_fail_on_wrong_input_length_bucket_name(self): + with self.assertRaises(AssertionError): + get( + client=self.client, + bucket_name='', + object_name=self.object_name, + ) + + def test_should_fail_on_wrong_input_type_object_name(self): + with self.assertRaises(AssertionError): + get( + client=self.client, + bucket_name=self.bucket_name, + object_name=0.0, + ) + + def test_should_fail_on_wrong_input_length_object_name(self): + with self.assertRaises(AssertionError): + get( + client=self.client, + bucket_name=self.bucket_name, + object_name='', + ) + + 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, + )