|
| 1 | +from typing import Any |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from fast_depends import ValidationError |
3 | 5 |
|
4 | | -from faststream import Context, ContextRepo |
| 6 | +from faststream import Context |
| 7 | +from faststream._internal.context import ContextRepo |
5 | 8 | from faststream._internal.utils import apply_types |
6 | 9 |
|
7 | 10 |
|
| 11 | +@pytest.mark.parametrize( |
| 12 | + ("initial", "expected_context"), |
| 13 | + ( |
| 14 | + pytest.param(None, {}, id="without initial"), |
| 15 | + pytest.param({"value": 42}, {"value": 42}, id="basic value"), |
| 16 | + pytest.param({"context": "sus"}, {}, id="sus context"), |
| 17 | + ), |
| 18 | +) |
| 19 | +def test_context_repo_constructor( |
| 20 | + initial: dict[str, Any] | None, |
| 21 | + expected_context: dict[str, Any], |
| 22 | +) -> None: |
| 23 | + repo = ContextRepo(initial) |
| 24 | + repo_context = repo.context |
| 25 | + |
| 26 | + assert repo_context.get("context") is repo |
| 27 | + repo_context.pop("context") |
| 28 | + assert repo_context == expected_context |
| 29 | + |
| 30 | + |
| 31 | +def test_context_repo_merge_global(): |
| 32 | + repo_1 = ContextRepo({"value_1": 1, "value_2": 2}) |
| 33 | + repo_2 = ContextRepo({"value_2": 3, "value_3": 4}) |
| 34 | + |
| 35 | + repo_1.merge_global(repo_2) |
| 36 | + |
| 37 | + assert repo_1.get("value_1") == 1 |
| 38 | + assert repo_1.get("value_2") == 3 |
| 39 | + assert repo_1.get("value_3") == 4 |
| 40 | + assert repo_1.get("context") is repo_1 |
| 41 | + |
| 42 | + |
8 | 43 | def test_context_getattr(context: ContextRepo) -> None: |
9 | 44 | a = 1000 |
10 | 45 | context.set_global("key", a) |
|
0 commit comments