def test__init__(self): importer = S3ModuleImporter(name='name', modules=[], service_address=self.service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) self.assertEqual(self.service_address, importer.service_address) self.assertEqual( importer.s3, mock_s3_resource('s3', aws_access_key_id=self.access_key, aws_secret_access_key=self.secret_key, endpoint_url=self.service_address, config=CONFIG)) importer = S3ModuleImporter( name='name', modules=[], service_address=self.service_address_without_slash, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) self.assertEqual(self.service_address, importer.service_address) self.assertEqual( importer.s3, mock_s3_resource('s3', aws_access_key_id=self.access_key, aws_secret_access_key=self.secret_key, endpoint_url=self.service_address, config=CONFIG)) self.assertRaises(ValueError, S3ModuleImporter, name='name', modules=[], service_address=self.not_secure_service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) importer = S3ModuleImporter( name='name', modules=[], service_address=self.not_secure_service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key, secure_only=False) self.assertEqual(self.not_secure_service_address, importer.service_address) self.assertEqual( importer.s3, mock_s3_resource('s3', aws_access_key_id=self.access_key, aws_secret_access_key=self.secret_key, endpoint_url=self.not_secure_service_address, config=CONFIG))
def test__fetch_obj_content__use_cache(self): importer = S3ModuleImporter(name='name', modules=[], service_address=self.service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) first_obj_content = 'first_obj_content' second_obj_content = 'second_obj_content' with patch(UTILS_IMPORTER_S3__GET_S3_OBJ_CONTENT, MagicMock(return_value=first_obj_content)): self.assertEqual(importer._fetch_obj_content(self.module_key), first_obj_content) self.assertEqual(importer.obj_cache[self.module_key], first_obj_content) with patch(UTILS_IMPORTER_S3__GET_S3_OBJ_CONTENT, MagicMock(return_value=second_obj_content)): self.assertEqual(importer._fetch_obj_content(self.module_key), first_obj_content) self.assertEqual(importer.obj_cache[self.module_key], first_obj_content) with patch(UTILS_IMPORTER_S3__GET_S3_OBJ_CONTENT, MagicMock(return_value=None)): self.assertIsNone(importer._fetch_obj_content(self.package_key)) self.assertIsNone(importer.obj_cache[self.package_key]) with patch(UTILS_IMPORTER_S3__GET_S3_OBJ_CONTENT, MagicMock(return_value=first_obj_content)): self.assertIsNone(importer._fetch_obj_content(self.package_key)) self.assertIsNone(importer.obj_cache[self.package_key])
def test_get_path(self): importer = S3ModuleImporter(name='name', modules=[], service_address=self.service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) self.assertEqual(importer.get_path(self.fullname), ['https://test-s3-address/bucket/module1/module2/module3'])
def test__obj_key(self): importer = S3ModuleImporter(name='name', modules=[], service_address=self.service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) self.assertEqual('module1/module2/module3/__init__.py', importer._obj_key(self.fullname, is_pkg=True)) self.assertEqual('module1/module2/module3.py', importer._obj_key(self.fullname, is_pkg=False))
def test_get_source__fetch_none(self): importer = S3ModuleImporter(name='name', modules=[], service_address=self.service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) self.assertRaises(ImportError, importer.get_source, self.fullname) importer._fetch_obj_content.assert_called_once_with(importer._obj_key(self.fullname, is_pkg=IS_PACKAGE_RETURN))
def test_get_source(self): importer = S3ModuleImporter(name='name', modules=[], service_address=self.service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) self.assertEqual(_FETCH_OBJ_CONTENT_RETURN, importer.get_source(self.fullname)) importer._fetch_obj_content.assert_called_once_with(importer._obj_key(self.fullname, is_pkg=IS_PACKAGE_RETURN))
def test_get_code(self): expect_code = compile(GET_SOURCE_RETURN, GET_FILE_RETURN, 'exec') importer = S3ModuleImporter(name='name', modules=[], service_address=self.service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) self.assertEqual(expect_code, importer.get_code(self.fullname))
def test_is_package(self): importer = S3ModuleImporter(name='name', modules=[], service_address=self.service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) with patch(UTILS_IMPORTER_S3__FETCH_OBJ_CONTENT, MagicMock(return_value='')): self.assertTrue(importer.is_package('a.b.c')) with patch(UTILS_IMPORTER_S3__FETCH_OBJ_CONTENT, MagicMock(return_value=None)): self.assertFalse(importer.is_package('a.b.c'))
def test_get_file(self): importer = S3ModuleImporter(name='name', modules=[], service_address=self.service_address, bucket=self.bucket, access_key=self.access_key, secret_key=self.secret_key) with patch(UTILS_IMPORTER_S3_IS_PACKAGE, MagicMock(return_value=False)): self.assertEqual(importer.get_file(self.fullname), self.module_url) with patch(UTILS_IMPORTER_S3_IS_PACKAGE, MagicMock(return_value=True)): self.assertEqual(importer.get_file(self.fullname), self.package_url)