python-boto3/tests/unit/test_utils.py

61 lines
2.1 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.
import types
2021-09-22 18:34:33 +02:00
from tests import mock, unittest
2015-11-27 23:25:33 +01:00
2021-10-04 19:51:32 +02:00
import pytest
2015-11-27 23:25:33 +01:00
from boto3 import utils
class FakeModule(object):
@staticmethod
def entry_point(**kwargs):
return kwargs
class TestUtils(unittest.TestCase):
def test_lazy_call(self):
with mock.patch('boto3.utils.import_module') as importer:
importer.return_value = FakeModule
lazy_function = utils.lazy_call(
'fakemodule.FakeModule.entry_point')
2021-10-04 19:51:32 +02:00
assert lazy_function(a=1, b=2) == {'a': 1, 'b': 2}
2015-11-27 23:25:33 +01:00
def test_import_module(self):
module = utils.import_module('boto3.s3.transfer')
2021-10-04 19:51:32 +02:00
assert module.__name__ == 'boto3.s3.transfer'
assert isinstance(module, types.ModuleType)
2015-11-27 23:25:33 +01:00
def test_inject_attributes_with_no_shadowing(self):
class_attributes = {}
utils.inject_attribute(class_attributes, 'foo', 'bar')
2021-10-04 19:51:32 +02:00
assert class_attributes['foo'] == 'bar'
2015-11-27 23:25:33 +01:00
def test_shadowing_existing_var_raises_exception(self):
class_attributes = {'foo': 'preexisting'}
2021-10-04 19:51:32 +02:00
with pytest.raises(RuntimeError):
2015-11-27 23:25:33 +01:00
utils.inject_attribute(class_attributes, 'foo', 'bar')
class TestLazyLoadedWaiterModel(unittest.TestCase):
def test_get_waiter_model_is_lazy(self):
session = mock.Mock()
waiter_model = utils.LazyLoadedWaiterModel(
session, 'myservice', '2014-01-01')
2021-10-04 19:51:32 +02:00
assert not session.get_waiter_model.called
2015-11-27 23:25:33 +01:00
waiter_model.get_waiter('Foo')
2021-10-04 19:51:32 +02:00
assert session.get_waiter_model.called
2015-11-27 23:25:33 +01:00
session.get_waiter_model.return_value.get_waiter.assert_called_with(
'Foo')