Skip to content

Commit 506cf42

Browse files
committed
test: add tests for ContextRepo
1 parent 45ee890 commit 506cf42

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

tests/utils/context/test_main.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1+
from typing import Any
2+
13
import pytest
24
from fast_depends import ValidationError
35

4-
from faststream import Context, ContextRepo
6+
from faststream import Context
7+
from faststream._internal.context import ContextRepo
58
from faststream._internal.utils import apply_types
69

710

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+
843
def test_context_getattr(context: ContextRepo) -> None:
944
a = 1000
1045
context.set_global("key", a)

0 commit comments

Comments
 (0)