|
| 1 | +import dataclasses |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from modern_di import Container, Group, Scope, providers |
| 6 | +from modern_di.exceptions import ProviderScopeFrozenError, ValidationFailedError |
| 7 | + |
| 8 | + |
| 9 | +class _Session: ... |
| 10 | + |
| 11 | + |
| 12 | +@dataclasses.dataclass(kw_only=True, slots=True, frozen=True) |
| 13 | +class _Request: |
| 14 | + user: str |
| 15 | + |
| 16 | + |
| 17 | +class _Handler: |
| 18 | + def __init__(self, session: _Session) -> None: |
| 19 | + self.session = session |
| 20 | + |
| 21 | + |
| 22 | +def _sessions_of_two_requests(container: Container) -> tuple[_Session, _Session]: |
| 23 | + with container.build_child_container(scope=Scope.REQUEST) as first: |
| 24 | + a = first.resolve(_Handler).session |
| 25 | + with container.build_child_container(scope=Scope.REQUEST) as second: |
| 26 | + b = second.resolve(_Handler).session |
| 27 | + return a, b |
| 28 | + |
| 29 | + |
| 30 | +def test_inline_cached_factory_takes_group_default_scope() -> None: |
| 31 | + class Dependencies(Group, scope=Scope.REQUEST): |
| 32 | + handler = providers.Factory(_Handler, kwargs={"session": providers.Factory(_Session, cache=True)}) |
| 33 | + |
| 34 | + a, b = _sessions_of_two_requests(Container(groups=[Dependencies])) |
| 35 | + |
| 36 | + assert a is not b |
| 37 | + |
| 38 | + |
| 39 | +def test_inline_cached_factory_takes_explicit_parent_scope() -> None: |
| 40 | + class Dependencies(Group): |
| 41 | + handler = providers.Factory( |
| 42 | + _Handler, scope=Scope.REQUEST, kwargs={"session": providers.Factory(_Session, cache=True)} |
| 43 | + ) |
| 44 | + |
| 45 | + a, b = _sessions_of_two_requests(Container(groups=[Dependencies])) |
| 46 | + |
| 47 | + assert a is not b |
| 48 | + |
| 49 | + |
| 50 | +def test_inline_factory_over_request_source_validates_and_resolves() -> None: |
| 51 | + class Dependencies(Group, scope=Scope.REQUEST): |
| 52 | + request = providers.ContextProvider(_Request) |
| 53 | + user_name = providers.Factory( |
| 54 | + str, kwargs={"object": providers.Factory(lambda r: r.user, kwargs={"r": request})} |
| 55 | + ) |
| 56 | + |
| 57 | + container = Container(groups=[Dependencies]) |
| 58 | + container.validate() |
| 59 | + |
| 60 | + with container.build_child_container(scope=Scope.REQUEST, context={_Request: _Request(user="alice")}) as child: |
| 61 | + assert child.resolve_provider(Dependencies.user_name) == "alice" |
| 62 | + |
| 63 | + |
| 64 | +def test_inline_context_provider_reads_request_context() -> None: |
| 65 | + class Dependencies(Group, scope=Scope.REQUEST): |
| 66 | + user = providers.Factory(lambda r: r.user, kwargs={"r": providers.ContextProvider(_Request)}) |
| 67 | + |
| 68 | + container = Container(groups=[Dependencies]) |
| 69 | + |
| 70 | + with container.build_child_container(scope=Scope.REQUEST, context={_Request: _Request(user="alice")}) as child: |
| 71 | + assert child.resolve_provider(Dependencies.user) == "alice" |
| 72 | + |
| 73 | + |
| 74 | +class _Engine: |
| 75 | + def __init__(self, dsn: str) -> None: |
| 76 | + self.dsn = dsn |
| 77 | + |
| 78 | + |
| 79 | +class _DbConnection: |
| 80 | + def __init__(self, dsn: str) -> None: |
| 81 | + self.dsn = dsn |
| 82 | + |
| 83 | + |
| 84 | +def test_inline_provider_shared_by_app_and_request_parents_takes_app_scope() -> None: |
| 85 | + dsn = providers.Factory(lambda: "postgres://") |
| 86 | + |
| 87 | + class Dependencies(Group): |
| 88 | + db_connection = providers.Factory(_DbConnection, scope=Scope.REQUEST, kwargs={"dsn": dsn}) |
| 89 | + engine = providers.Factory(_Engine, kwargs={"dsn": dsn}, cache=True) |
| 90 | + |
| 91 | + container = Container(groups=[Dependencies]) |
| 92 | + container.validate() |
| 93 | + |
| 94 | + assert dsn.scope is Scope.APP |
| 95 | + with container.build_child_container(scope=Scope.REQUEST) as child: |
| 96 | + assert child.resolve(_DbConnection).dsn == child.resolve(_Engine).dsn == "postgres://" |
| 97 | + |
| 98 | + |
| 99 | +def test_inline_provider_scope_is_fixed_at_first_registration() -> None: |
| 100 | + dsn = providers.Factory(lambda: "postgres://") |
| 101 | + |
| 102 | + class Dependencies(Group): |
| 103 | + db_connection = providers.Factory(_DbConnection, scope=Scope.REQUEST, kwargs={"dsn": dsn}) |
| 104 | + |
| 105 | + container = Container(groups=[Dependencies]) |
| 106 | + container.add_providers(providers.Factory(_Engine, kwargs={"dsn": dsn}, cache=True)) |
| 107 | + |
| 108 | + assert dsn.scope is Scope.REQUEST |
| 109 | + with pytest.raises(ValidationFailedError, match="InvalidScopeDependencyError"): |
| 110 | + container.validate() |
| 111 | + |
| 112 | + |
| 113 | +def test_inline_provider_with_explicit_scope_keeps_it() -> None: |
| 114 | + class Dependencies(Group, scope=Scope.REQUEST): |
| 115 | + handler = providers.Factory( |
| 116 | + _Handler, kwargs={"session": providers.Factory(_Session, scope=Scope.APP, cache=True)} |
| 117 | + ) |
| 118 | + |
| 119 | + a, b = _sessions_of_two_requests(Container(groups=[Dependencies])) |
| 120 | + |
| 121 | + assert a is b |
| 122 | + |
| 123 | + |
| 124 | +def test_class_attribute_of_another_group_keeps_its_scope() -> None: |
| 125 | + class Shared(Group): |
| 126 | + session = providers.Factory(_Session, cache=True) |
| 127 | + |
| 128 | + class Dependencies(Group, scope=Scope.REQUEST): |
| 129 | + handler = providers.Factory(_Handler, kwargs={"session": Shared.session}) |
| 130 | + |
| 131 | + a, b = _sessions_of_two_requests(Container(groups=[Shared, Dependencies])) |
| 132 | + |
| 133 | + assert Shared.session.scope is Scope.APP |
| 134 | + assert a is b |
| 135 | + |
| 136 | + |
| 137 | +class _Outer: |
| 138 | + def __init__(self, handler: _Handler) -> None: |
| 139 | + self.handler = handler |
| 140 | + |
| 141 | + |
| 142 | +def test_provider_inline_in_an_inline_provider_takes_the_scope_down_the_chain() -> None: |
| 143 | + class Dependencies(Group, scope=Scope.REQUEST): |
| 144 | + outer = providers.Factory( |
| 145 | + _Outer, |
| 146 | + kwargs={ |
| 147 | + "handler": providers.Factory(_Handler, kwargs={"session": providers.Factory(_Session, cache=True)}) |
| 148 | + }, |
| 149 | + ) |
| 150 | + |
| 151 | + container = Container(groups=[Dependencies]) |
| 152 | + with container.build_child_container(scope=Scope.REQUEST) as first: |
| 153 | + a = first.resolve(_Outer).handler.session |
| 154 | + with container.build_child_container(scope=Scope.REQUEST) as second: |
| 155 | + b = second.resolve(_Outer).handler.session |
| 156 | + |
| 157 | + assert a is not b |
| 158 | + |
| 159 | + |
| 160 | +def test_group_cannot_change_scope_of_registered_inline_provider() -> None: |
| 161 | + session = providers.Factory(_Session, cache=True) |
| 162 | + |
| 163 | + class Dependencies(Group, scope=Scope.REQUEST): |
| 164 | + handler = providers.Factory(_Handler, kwargs={"session": session}) |
| 165 | + |
| 166 | + Container(groups=[Dependencies]) |
| 167 | + |
| 168 | + with pytest.raises(ProviderScopeFrozenError, match="Later"): |
| 169 | + |
| 170 | + class Later(Group, scope=Scope.SESSION): |
| 171 | + shared_session = session |
| 172 | + |
| 173 | + assert session.scope is Scope.REQUEST |
| 174 | + |
| 175 | + |
| 176 | +def test_inline_source_of_inline_derived_provider_takes_parent_scope() -> None: |
| 177 | + class Dependencies(Group, scope=Scope.REQUEST): |
| 178 | + handler = providers.Factory( |
| 179 | + _Handler, kwargs={"session": providers.Factory(_Session, cache=True).derive(lambda s: s)} |
| 180 | + ) |
| 181 | + |
| 182 | + a, b = _sessions_of_two_requests(Container(groups=[Dependencies])) |
| 183 | + |
| 184 | + assert a is not b |
0 commit comments