File: test_utils.py

package info (click to toggle)
python-boto3 1.26.27%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,880 kB
  • sloc: python: 12,629; makefile: 128
file content (63 lines) | stat: -rw-r--r-- 2,191 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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
#
# https://aws.amazon.com/apache2.0/
#
# 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

import pytest

from boto3 import utils
from tests import mock, unittest


class FakeModule:
    @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'
            )
            assert lazy_function(a=1, b=2) == {'a': 1, 'b': 2}

    def test_import_module(self):
        module = utils.import_module('boto3.s3.transfer')
        assert module.__name__ == 'boto3.s3.transfer'
        assert isinstance(module, types.ModuleType)

    def test_inject_attributes_with_no_shadowing(self):
        class_attributes = {}
        utils.inject_attribute(class_attributes, 'foo', 'bar')
        assert class_attributes['foo'] == 'bar'

    def test_shadowing_existing_var_raises_exception(self):
        class_attributes = {'foo': 'preexisting'}
        with pytest.raises(RuntimeError):
            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'
        )
        assert not session.get_waiter_model.called
        waiter_model.get_waiter('Foo')
        assert session.get_waiter_model.called
        session.get_waiter_model.return_value.get_waiter.assert_called_with(
            'Foo'
        )