|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import typing |
4 | | -from collections.abc import Sequence |
| 4 | +from collections.abc import Hashable, Mapping, Sequence |
5 | 5 | from dataclasses import dataclass |
6 | | -from typing import Optional, Protocol |
| 6 | +from typing import Annotated, NewType, Optional, Protocol |
7 | 7 |
|
8 | 8 | import pytest |
9 | | -from wireup import Injected, create_sync_container, inject_from_container, injectable |
| 9 | +from wireup import Inject, Injected, create_sync_container, inject_from_container, injectable |
10 | 10 | from wireup._annotations import InjectableDeclaration |
11 | 11 | from wireup.errors import UnknownServiceRequestedError, WireupError |
12 | 12 | from wireup.ioc.registry import ContainerRegistry |
@@ -178,3 +178,190 @@ class Consumer: |
178 | 178 | match=r"uses typing\.Sequence\[.*Cache.*\], but Wireup collection injection requires collections\.abc\.Sequence\[.*Cache.*\]", # noqa: E501 |
179 | 179 | ): |
180 | 180 | create_sync_container(injectables=[MemoryCache, RedisCache, Consumer]) |
| 181 | + |
| 182 | + |
| 183 | +def test_injects_collection_mapping_for_as_type() -> None: |
| 184 | + container = create_sync_container(injectables=[MemoryCache, RedisCache]) |
| 185 | + |
| 186 | + @inject_from_container(container) |
| 187 | + def handler(caches: Injected[Mapping[Hashable, Cache]]) -> dict[Hashable, str]: |
| 188 | + return {key: cache.source() for key, cache in caches.items()} |
| 189 | + |
| 190 | + assert handler() == {None: "memory", "redis": "redis"} |
| 191 | + assert container.get(Mapping[Hashable, Cache]) is container.get(Mapping[Hashable, Cache]) |
| 192 | + |
| 193 | + |
| 194 | +def test_injects_collection_mapping_for_single_implementation() -> None: |
| 195 | + container = create_sync_container(injectables=[MemoryCache]) |
| 196 | + res = container.get(Mapping[Hashable, Cache]) |
| 197 | + |
| 198 | + assert isinstance(res, dict) |
| 199 | + assert list(res.keys()) == [None] |
| 200 | + assert res[None].source() == "memory" |
| 201 | + |
| 202 | + |
| 203 | +def test_default_impl_appears_under_none_key() -> None: |
| 204 | + container = create_sync_container(injectables=[MemoryCache]) |
| 205 | + res = container.get(Mapping[Hashable, Cache]) |
| 206 | + |
| 207 | + assert res[None].source() == "memory" |
| 208 | + |
| 209 | + |
| 210 | +def test_mapping_works_with_only_qualified_impls() -> None: |
| 211 | + @injectable(as_type=Cache, qualifier="memory") |
| 212 | + class QualifiedMemoryCache: |
| 213 | + def source(self) -> str: |
| 214 | + return "memory" |
| 215 | + |
| 216 | + container = create_sync_container(injectables=[QualifiedMemoryCache, RedisCache]) |
| 217 | + res = container.get(Mapping[Hashable, Cache]) |
| 218 | + |
| 219 | + assert set(res.keys()) == {"memory", "redis"} |
| 220 | + assert None not in res |
| 221 | + |
| 222 | + |
| 223 | +def test_mapping_supports_non_string_qualifiers() -> None: |
| 224 | + @injectable(as_type=Cache, qualifier=0) |
| 225 | + class ZeroCache: |
| 226 | + def source(self) -> str: |
| 227 | + return "zero" |
| 228 | + |
| 229 | + @injectable(as_type=Cache, qualifier=1) |
| 230 | + class OneCache: |
| 231 | + def source(self) -> str: |
| 232 | + return "one" |
| 233 | + |
| 234 | + container = create_sync_container(injectables=[ZeroCache, OneCache]) |
| 235 | + res = container.get(Mapping[Hashable, Cache]) |
| 236 | + |
| 237 | + assert res[0].source() == "zero" |
| 238 | + assert res[1].source() == "one" |
| 239 | + |
| 240 | + |
| 241 | +def test_mapping_lifetime_is_smallest_member_lifetime() -> None: |
| 242 | + @injectable(as_type=Cache, qualifier="default") |
| 243 | + class DefaultCache: |
| 244 | + def source(self) -> str: |
| 245 | + return "default" |
| 246 | + |
| 247 | + @injectable(as_type=Cache, qualifier="scoped", lifetime="scoped") |
| 248 | + class ScopedCache: |
| 249 | + def source(self) -> str: |
| 250 | + return "scoped" |
| 251 | + |
| 252 | + @injectable |
| 253 | + @dataclass |
| 254 | + class Consumer: |
| 255 | + caches: Mapping[Hashable, Cache] |
| 256 | + |
| 257 | + with pytest.raises( |
| 258 | + WireupError, |
| 259 | + match="depends on an injectable with a 'scoped' lifetime which is not supported", |
| 260 | + ): |
| 261 | + create_sync_container(injectables=[DefaultCache, ScopedCache, Consumer]) |
| 262 | + |
| 263 | + |
| 264 | +def test_can_override_mapping_directly() -> None: |
| 265 | + container = create_sync_container(injectables=[MemoryCache, RedisCache]) |
| 266 | + override_map = {"override": RedisCache()} |
| 267 | + |
| 268 | + assert set(container.get(Mapping[Hashable, Cache]).keys()) == {None, "redis"} |
| 269 | + with container.override.injectable(Mapping[Hashable, Cache], new=override_map): |
| 270 | + assert container.get(Mapping[Hashable, Cache]) is override_map |
| 271 | + assert set(container.get(Mapping[Hashable, Cache]).keys()) == {None, "redis"} |
| 272 | + |
| 273 | + |
| 274 | +def test_explicit_mapping_registration_takes_precedence_and_warns() -> None: |
| 275 | + mapping = {"custom": RedisCache()} |
| 276 | + |
| 277 | + @injectable |
| 278 | + def make_custom_mapping() -> Mapping[Hashable, Cache]: |
| 279 | + return mapping |
| 280 | + |
| 281 | + with pytest.warns(FutureWarning, match=r"Mapping\[Hashable, T\] is reserved for Wireup collection injection"): |
| 282 | + container = create_sync_container(injectables=[MemoryCache, make_custom_mapping]) |
| 283 | + |
| 284 | + assert container.get(Mapping[Hashable, Cache]) is mapping |
| 285 | + |
| 286 | + |
| 287 | +def test_injects_collection_mapping_when_registration_key_comes_from_factory_return_type() -> None: |
| 288 | + @injectable |
| 289 | + def make_default_cache() -> Cache: |
| 290 | + return MemoryCache() |
| 291 | + |
| 292 | + @injectable(qualifier="redis") |
| 293 | + def make_redis_cache() -> Cache: |
| 294 | + return RedisCache() |
| 295 | + |
| 296 | + container = create_sync_container(injectables=[make_default_cache, make_redis_cache]) |
| 297 | + res = container.get(Mapping[Hashable, Cache]) |
| 298 | + |
| 299 | + assert isinstance(res, dict) |
| 300 | + assert {key: cache.source() for key, cache in res.items()} == {None: "memory", "redis": "redis"} |
| 301 | + |
| 302 | + |
| 303 | +def test_internal_extend_does_not_create_nested_mapping_collections() -> None: |
| 304 | + registry = ContainerRegistry(impls=[InjectableDeclaration(obj=MemoryCache, lifetime="singleton")]) |
| 305 | + |
| 306 | + @injectable |
| 307 | + class ExtraService: |
| 308 | + pass |
| 309 | + |
| 310 | + registry.extend(impls=[InjectableDeclaration(obj=ExtraService, lifetime="singleton")]) |
| 311 | + |
| 312 | + assert Mapping[Hashable, Mapping[Hashable, MemoryCache]] not in registry.impls |
| 313 | + |
| 314 | + |
| 315 | +def test_mixed_real_and_optional_compat_registrations_only_include_real_members_in_mapping() -> None: |
| 316 | + @injectable |
| 317 | + def make_optional_default_cache() -> Cache | None: |
| 318 | + return None |
| 319 | + |
| 320 | + @injectable(qualifier="redis") |
| 321 | + def make_redis_cache() -> Cache: |
| 322 | + return RedisCache() |
| 323 | + |
| 324 | + container = create_sync_container(injectables=[make_optional_default_cache, make_redis_cache]) |
| 325 | + |
| 326 | + assert container.get(Mapping[Hashable, Cache]) == {"redis": container.get(Cache, qualifier="redis")} |
| 327 | + |
| 328 | + |
| 329 | +def test_typing_mapping_is_not_registered() -> None: |
| 330 | + container = create_sync_container(injectables=[MemoryCache, RedisCache]) |
| 331 | + |
| 332 | + with pytest.raises( |
| 333 | + UnknownServiceRequestedError, |
| 334 | + match=r"Wireup collection injection uses collections\.abc\.Mapping\[.*Cache.*\], not typing\.Mapping\[.*Cache.*\]", # noqa: E501 |
| 335 | + ): |
| 336 | + container.get(typing.Mapping[str, Cache]) |
| 337 | + |
| 338 | + |
| 339 | +def test_typing_mapping_dependency_raises_helpful_error() -> None: |
| 340 | + @injectable |
| 341 | + @dataclass |
| 342 | + class Consumer: |
| 343 | + caches: typing.Mapping[str, Cache] |
| 344 | + |
| 345 | + with pytest.raises( |
| 346 | + WireupError, |
| 347 | + match=r"uses typing\.Mapping\[.*Cache.*\], but Wireup collection injection requires collections\.abc\.Mapping\[.*Cache.*\]", # noqa: E501 |
| 348 | + ): |
| 349 | + create_sync_container(injectables=[MemoryCache, RedisCache, Consumer]) |
| 350 | + |
| 351 | + |
| 352 | +CacheMap = NewType("CacheMap", Mapping[Hashable, Cache]) |
| 353 | + |
| 354 | + |
| 355 | +def test_newtype_over_mapping_registers_as_distinct_collection() -> None: |
| 356 | + @injectable |
| 357 | + def make_cache_map( |
| 358 | + default: Cache, |
| 359 | + redis: Annotated[Cache, Inject(qualifier="redis")], |
| 360 | + ) -> CacheMap: |
| 361 | + return CacheMap({None: default, "redis": redis}) |
| 362 | + |
| 363 | + container = create_sync_container(injectables=[MemoryCache, RedisCache, make_cache_map]) |
| 364 | + res = container.get(CacheMap) |
| 365 | + |
| 366 | + assert isinstance(res, dict) |
| 367 | + assert {key: cache.source() for key, cache in res.items()} == {None: "memory", "redis": "redis"} |
0 commit comments