Skip to content

Commit 0426de6

Browse files
Make FakeServicerContext Inherit from grpcio ServicerContext
1 parent aeebd79 commit 0426de6

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

django_grpc_testtools/context.py

+70-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from typing import NoReturn
22
from grpc import RpcError, StatusCode
33
from collections.abc import Sequence, Mapping
4+
from grpc import ServicerContext
45

56
MetadataType = Sequence[tuple[str, str]]
67

78

8-
class FakeServicerContext:
9+
class FakeServicerContext(ServicerContext):
910
"""
1011
Implementation of basic RpcContext methods that stores data
1112
for validation in tests
@@ -50,4 +51,71 @@ def set_invocation_metadata(self, items: MetadataType) -> None:
5051
"""
5152
Helper to emulate request metadata
5253
"""
53-
self._invocation_metadata = items
54+
self._invocation_metadata = items
55+
56+
def set_code(self, code: StatusCode) -> None:
57+
self.abort_status = code
58+
59+
def set_details(self, details: str) -> None:
60+
"""Sets the value to be used as detail string upon RPC completion.
61+
62+
This method need not be called by method implementations if they have
63+
no details to transmit.
64+
65+
Args:
66+
details: A UTF-8-encodable string to be sent to the client upon
67+
termination of the RPC.
68+
"""
69+
self.abort_message = details
70+
71+
def code(self) -> StatusCode:
72+
"""Accesses the value to be used as status code upon RPC completion.
73+
74+
Returns:
75+
The StatusCode value for the RPC.
76+
"""
77+
return self.abort_status
78+
79+
def details(self) -> str:
80+
"""Accesses the value to be used as detail string upon RPC completion.
81+
82+
Returns:
83+
The details string of the RPC.
84+
"""
85+
return self.abort_message
86+
87+
def peer(self):
88+
raise NotImplementedError()
89+
90+
def peer_identities(self):
91+
raise NotImplementedError()
92+
93+
def peer_identity_key(self):
94+
raise NotImplementedError()
95+
96+
def auth_context(self):
97+
raise NotImplementedError()
98+
99+
def set_compression(self, compression):
100+
raise NotImplementedError()
101+
102+
def send_initial_metadata(self, initial_metadata):
103+
raise NotImplementedError()
104+
105+
def abort_with_status(self, status):
106+
raise NotImplementedError()
107+
108+
def disable_next_message_compression(self):
109+
raise NotImplementedError()
110+
111+
def is_active(self):
112+
raise NotImplementedError()
113+
114+
def time_remaining(self):
115+
raise NotImplementedError()
116+
117+
def cancel(self):
118+
raise NotImplementedError()
119+
120+
def add_callback(self, callback):
121+
raise NotImplementedError()

tests/test_fake_servicer_context.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from grpc import RpcError, StatusCode
1+
from grpc import RpcError, StatusCode, ServicerContext
22
import pytest
33

44
from django_grpc_testtools.context import FakeServicerContext
55

66

77
def test_fake_servicer_context():
88
context = FakeServicerContext()
9+
assert isinstance(context, ServicerContext)
910
with pytest.raises(RpcError):
1011
context.abort(StatusCode.UNAVAILABLE, 'test')

0 commit comments

Comments
 (0)