forked from nameko/nameko
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_contextdata.py
More file actions
71 lines (50 loc) · 1.92 KB
/
Copy pathtest_contextdata.py
File metadata and controls
71 lines (50 loc) · 1.92 KB
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
import pytest
from mock import Mock
from nameko.constants import (
AUTH_TOKEN_CONTEXT_KEY, LANGUAGE_CONTEXT_KEY, USER_AGENT_CONTEXT_KEY,
USER_ID_CONTEXT_KEY
)
from nameko.containers import ServiceContainer, WorkerContext
from nameko.contextdata import (
AuthToken, ContextDataProvider, Language, UserAgent, UserId
)
from nameko.testing.utils import get_extension
CUSTOM_CONTEXT_KEY = "custom"
class CustomValue(ContextDataProvider):
context_key = CUSTOM_CONTEXT_KEY
class Service(object):
name = "service"
# builtin context data dependencies
auth_token = AuthToken()
language = Language()
user_id = UserId()
user_agent = UserAgent()
# custom context data dependency
custom_value = CustomValue()
@pytest.fixture
def container():
return ServiceContainer(Service, {})
def test_get_custom_context_value(container):
dependency = get_extension(
container, ContextDataProvider, attr_name="custom_value")
worker_ctx = WorkerContext(
container, "service", Mock(), data={CUSTOM_CONTEXT_KEY: "hello"})
assert dependency.get_dependency(worker_ctx) == "hello"
def test_get_unset_value(container):
dependency = get_extension(
container, ContextDataProvider, attr_name="custom_value")
worker_ctx = WorkerContext(
container, "service", Mock(), data={})
assert dependency.get_dependency(worker_ctx) is None
@pytest.mark.parametrize('attr_name, context_key', [
('auth_token', AUTH_TOKEN_CONTEXT_KEY),
('language', LANGUAGE_CONTEXT_KEY),
('user_id', USER_ID_CONTEXT_KEY),
('user_agent', USER_AGENT_CONTEXT_KEY),
])
def test_get_builtin_dependencies(attr_name, context_key, container):
dependency = get_extension(
container, ContextDataProvider, attr_name=attr_name)
worker_ctx = WorkerContext(
container, "service", Mock(), data={context_key: 'value'})
assert dependency.get_dependency(worker_ctx) == "value"