File: test_functional.py

package info (click to toggle)
python-inject 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: python: 1,039; makefile: 28; sh: 5
file content (118 lines) | stat: -rw-r--r-- 3,590 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from unittest import TestCase

import inject
from inject import autoparams, ConstructorTypeError


class TestFunctional(TestCase):
    def tearDown(self):
        inject.clear()

    def test(self):
        class Config(object):
            def __init__(self, greeting):
                self.greeting = greeting

        class Cache(object):
            config = inject.attr(Config)

            def load_greeting(self):
                return self.config.greeting

        class User(object):
            cache = inject.attr(Cache)

            def __init__(self, name):
                self.name = name

            def greet(self):
                return '%s, %s' % (self.cache.load_greeting(), self.name)

        def config(binder):
            binder.bind(Config, Config('Hello'))

        inject.configure(config)

        user = User('John Doe')
        greeting = user.greet()
        assert greeting == 'Hello, John Doe'

    def test_class_with_restricted_bool_casting(self):
        class DataFrame(object):
            def __nonzero__(self):
                """Python 2"""
                raise NotImplementedError('Casting to boolean is not allowed')

            def __bool__(self):
                """Python 3"""
                raise NotImplementedError('Casting to boolean is not allowed')

        def create_data_frame():
            return DataFrame()

        def config(binder):
            binder.bind_to_constructor(DataFrame, create_data_frame)

        inject.configure(config)

        assert type(inject.instance(DataFrame)) is DataFrame
        # There should not be an error when we fetch created instance
        assert type(inject.instance(DataFrame)) is DataFrame

    def test_class_support_in_autoparams_programmaticaly(self):
        class AnotherClass:
            pass

        class SomeClass:
            def __init__(self, another_object: AnotherClass):
                self.another_object = another_object

        def config(binder):
            binder.bind_to_constructor(SomeClass, autoparams()(SomeClass))
            binder.bind_to_constructor(AnotherClass, autoparams()(AnotherClass))

        inject.configure(config)

        some_object = inject.instance(SomeClass)
        assert type(some_object) is SomeClass
        assert type(some_object.another_object) is AnotherClass

    def test_error_message(self):
        class SomeClass:
            def __init__(self, missing_arg):
                self._missing_arg = missing_arg

        class AnotherClass(SomeClass):
            pass

        def create_some_class(missing_arg):
            return AnotherClass(missing_arg)

        def config(binder):
            binder.bind_to_constructor(SomeClass, autoparams()(SomeClass))
            binder.bind_to_constructor(AnotherClass, create_some_class)

        inject.configure()

        # covers case when no constructor provided
        try:
            inject.instance(SomeClass)
        except ConstructorTypeError as err:
            assert 'SomeClass' in str(err)
            assert 'missing_arg' in str(err)

        inject.clear_and_configure(config)

        # covers case with provided constructor
        try:
            inject.instance(SomeClass)
        except ConstructorTypeError as err:
            assert 'SomeClass' in str(err)
            assert 'missing_arg' in str(err)

        try:
            inject.instance(AnotherClass)
        except TypeError as err:
            assert not isinstance(err, ConstructorTypeError)
            assert 'create_some_class' in str(err)
            assert 'missing_arg' in str(err)