Skip to content
Snippets Groups Projects
Commit 23b7ef2c authored by Georges Racinet's avatar Georges Racinet
Browse files

testing: more robust fake contexts

This will force us to assess the appearance of any new method
of `grpc.ServicerContext`, hence avoiding some cases where our unit
tests might become tautological and useless.
parent 1ef13bab
No related branches found
No related tags found
1 merge request!134Feature flags subsystem
# Copyright 2023 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later
# TODO bring full coverage directly from here, because there might
# be points in time where it won't be used at all by gRPC methods
import grpc
class FakeServicerContext(grpc.ServicerContext):
"""A base class for fake servicer contexts in tests.
This base class allow testing harness to implement only a few methods,
while still going through the ABC checking that all abstract methods are
implemented.
This can detect API changes in the base class. For an example, assume
that main code relies on the `invocation_metadata` method,
and corresponding unit test provides a mock implementation. If the method
is renamed in :class:`ServicerContext` as `incoming_metadata`, the
mock implementation suddenly will not implement all abstract methods
and the test run will fail, alerting us of the needed adaptation.
This is heavy-handed, but still a bit better than not doing any checking.
Of course most of HGItaly is covered by integration tests such as
in `hgitaly.service.tests`, but some might temporarily not be
(concrete case being the feature flags system at a time where there
is no relevant Gitaly feature flag for HGitaly), and hence requires
full coverage in *unit* tests.
"""
def fake_impl(self):
"""To make ABC consider an abstract method to be implemented."""
raise NotImplementedError() # pragma no cover
abort = fake_impl
abort_with_status = fake_impl
add_callback = fake_impl
auth_context = fake_impl
cancel = fake_impl
invocation_metadata = fake_impl
is_active = fake_impl
peer = fake_impl
peer_identities = fake_impl
peer_identity_key = fake_impl
send_initial_metadata = fake_impl
set_code = fake_impl
set_details = fake_impl
set_trailing_metadata = fake_impl
time_remaining = fake_impl
......@@ -22,6 +22,7 @@
LocalRepoWrapper,
)
from ..testing.context import FakeServicerContext
from ..servicer import HGitalyServicer
from ..stub.shared_pb2 import (
Repository,
......@@ -30,7 +31,7 @@
from ..stub.repository_pb2_grpc import RepositoryServiceStub
class FakeContext:
class FakeContext(FakeServicerContext):
def set_code(self, code):
self.code = code
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment