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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
import inject
from test import BaseTestInject
import inspect
import asyncio
class TestInjectParams(BaseTestInject):
def test_params(self):
@inject.params(val=int)
def test_func(val):
return val
inject.configure(lambda binder: binder.bind(int, 123))
assert test_func() == 123
assert test_func(321) == 321
assert test_func(val=42) == 42
def test_params_multi(self):
@inject.params(a='A', b='B', c='C')
def test_func(a, b, c):
return a, b, c
def config(binder):
binder.bind('A', 1)
binder.bind('B', 2)
binder.bind('C', 3)
inject.configure(config)
assert test_func() == (1, 2, 3)
assert test_func(10) == (10, 2, 3)
assert test_func(10, 20) == (10, 20, 3)
assert test_func(10, 20, 30) == (10, 20, 30)
assert test_func(a='a') == ('a', 2, 3)
assert test_func(b='b') == (1, 'b', 3)
assert test_func(c='c') == (1, 2, 'c')
assert test_func(a=10, c=30) == (10, 2, 30)
assert test_func(c=30, b=20, a=10) == (10, 20, 30)
assert test_func(10, b=20) == (10, 20, 3)
def test_params_with_defaults(self):
# note the inject overrides default parameters
@inject.params(b='B', c='C')
def test_func(a=1, b=None, c=300):
return a, b, c
def config(binder):
binder.bind('B', 2)
binder.bind('C', 3)
inject.configure(config)
assert test_func() == (1, 2, 3)
assert test_func(10) == (10, 2, 3)
assert test_func(10, 20) == (10, 20, 3)
assert test_func(10, 20, 30) == (10, 20, 30)
assert test_func(a='a') == ('a', 2, 3)
assert test_func(b='b') == (1, 'b', 3)
assert test_func(c='c') == (1, 2, 'c')
assert test_func(a=10, c=30) == (10, 2, 30)
assert test_func(c=30, b=20, a=10) == (10, 20, 30)
assert test_func(10, b=20) == (10, 20, 3)
def test_params_on_method(self):
class Test:
@inject.params(b='B', c='C')
def func(self, a=1, b=None, c=None):
return self, a, b, c
def config(binder):
binder.bind('B', 2)
binder.bind('C', 3)
inject.configure(config)
test = Test()
assert test.func() == (test, 1, 2, 3)
assert test.func(10) == (test, 10, 2, 3)
assert test.func(10, 20) == (test, 10, 20, 3)
assert test.func(10, 20, 30) == (test, 10, 20, 30)
assert test.func(a='a') == (test, 'a', 2, 3)
assert test.func(b='b') == (test, 1, 'b', 3)
assert test.func(c='c') == (test, 1, 2, 'c')
assert test.func(a=10, c=30) == (test, 10, 2, 30)
assert test.func(c=30, b=20, a=10) == (test, 10, 20, 30)
assert test.func(10, b=20) == (test, 10, 20, 3)
def test_params_on_classmethod(self):
class Test:
# note inject must be *before* classmethod!
@classmethod
@inject.params(b='B', c='C')
def func(cls, a=1, b=None, c=None):
return cls, a, b, c
def config(binder):
binder.bind('B', 2)
binder.bind('C', 3)
inject.configure(config)
assert Test.func() == (Test, 1, 2, 3)
assert Test.func(10) == (Test, 10, 2, 3)
assert Test.func(10, 20) == (Test, 10, 20, 3)
assert Test.func(10, 20, 30) == (Test, 10, 20, 30)
assert Test.func(a='a') == (Test, 'a', 2, 3)
assert Test.func(b='b') == (Test, 1, 'b', 3)
assert Test.func(c='c') == (Test, 1, 2, 'c')
assert Test.func(a=10, c=30) == (Test, 10, 2, 30)
assert Test.func(c=30, b=20, a=10) == (Test, 10, 20, 30)
assert Test.func(10, b=20) == (Test, 10, 20, 3)
def test_params_on_classmethod_ob_object(self):
class Test:
# note inject must be *before* classmethod!
@classmethod
@inject.params(b='B', c='C')
def func(cls, a=1, b=None, c=None):
return cls, a, b, c
def config(binder):
binder.bind('B', 2)
binder.bind('C', 3)
inject.configure(config)
test = Test
assert test.func() == (Test, 1, 2, 3)
assert test.func(10) == (Test, 10, 2, 3)
assert test.func(10, 20) == (Test, 10, 20, 3)
assert test.func(10, 20, 30) == (Test, 10, 20, 30)
assert test.func(a='a') == (Test, 'a', 2, 3)
assert test.func(b='b') == (Test, 1, 'b', 3)
assert test.func(c='c') == (Test, 1, 2, 'c')
assert test.func(a=10, c=30) == (Test, 10, 2, 30)
assert test.func(c=30, b=20, a=10) == (Test, 10, 20, 30)
assert test.func(10, b=20) == (Test, 10, 20, 3)
def test_async_params(self):
@inject.params(val=int)
async def test_func(val):
return val
inject.configure(lambda binder: binder.bind(int, 123))
assert inspect.iscoroutinefunction(test_func)
assert self.run_async(test_func()) == 123
assert self.run_async(test_func(321)) == 321
assert self.run_async(test_func(val=42)) == 42
|