python-boto3/tests/functional/test_resource.py

56 lines
1.9 KiB
Python
Raw Normal View History

2015-11-27 23:25:33 +01:00
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
2021-09-22 18:34:33 +02:00
# https://aws.amazon.com/apache2.0/
2015-11-27 23:25:33 +01:00
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
2021-10-04 19:51:32 +02:00
import pytest
2015-11-27 23:25:33 +01:00
import boto3
2016-05-22 04:03:29 +02:00
from boto3.exceptions import ResourceNotExistsError
2015-11-27 23:25:33 +01:00
import botocore.session
from tests import unittest
def identity(self, x):
return x
class TestResourceCustomization(unittest.TestCase):
def setUp(self):
self.botocore_session = botocore.session.get_session()
def add_new_method(self, name):
def handler(class_attributes, **kwargs):
class_attributes[name] = identity
return handler
def test_can_inject_method_onto_resource(self):
session = boto3.Session(botocore_session=self.botocore_session)
self.botocore_session.register('creating-resource-class.s3',
self.add_new_method(name='my_method'))
resource = session.resource('s3')
2021-10-04 19:51:32 +02:00
assert hasattr(resource, 'my_method')
assert resource.my_method('anything') == 'anything'
2016-05-22 04:03:29 +02:00
class TestSessionErrorMessages(unittest.TestCase):
def test_has_good_error_message_when_no_resource(self):
bad_resource_name = 'doesnotexist'
2021-10-04 19:51:32 +02:00
err_regex = f'{bad_resource_name}.*resource does not exist.'
with pytest.raises(ResourceNotExistsError, match=err_regex):
2016-05-22 04:03:29 +02:00
boto3.resource(bad_resource_name)
class TestGetAvailableSubresources(unittest.TestCase):
def test_s3_available_subresources_exists(self):
s3 = boto3.resource('s3')
2021-10-04 19:51:32 +02:00
assert hasattr(s3, 'get_available_subresources')