Skip to content

Commit

Permalink
Merge pull request #521 from nolar/cause-factory-in-tests
Browse files Browse the repository at this point in the history
Get rid of mocks for causes in the tests of registries
  • Loading branch information
nolar authored Aug 29, 2020
2 parents 65a33e3 + 91875a4 commit 49b342b
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 319 deletions.
82 changes: 79 additions & 3 deletions tests/registries/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import logging

import pytest

from kopf import ActivityRegistry
from kopf import OperatorRegistry
from kopf import ResourceWatchingRegistry, ResourceChangingRegistry
from kopf import SimpleRegistry, GlobalRegistry # deprecated, but tested
from kopf.reactor.causation import ActivityCause, ResourceCause, ResourceWatchingCause, ResourceChangingCause
from kopf.reactor.registries import ResourceRegistry, ActivityRegistry, OperatorRegistry
from kopf.reactor.registries import ResourceWatchingRegistry, ResourceChangingRegistry
from kopf.structs.bodies import Body
from kopf.structs.containers import Memo
from kopf.structs.diffs import Diff, DiffItem
from kopf.structs.handlers import HandlerId, ResourceChangingHandler
from kopf.structs.patches import Patch


@pytest.fixture(params=[
Expand Down Expand Up @@ -54,3 +60,73 @@ def parent_fn(**_):
initial=None, deleted=None, requires_finalizer=None,
reason=None, field=None,
)


@pytest.fixture()
def cause_factory(resource):
"""
A simplified factory of causes.
It assumes most of the parameters to be unused defaults, which is sufficient
for testing. Some parameters are of improper types (e.g. Nones), others are
converted from built-in types to proper types, the rest are passed as is.
The cause class is selected based on the passed ``cls``, which is either
directly the cause class to use, or the registry class. For the latter
case, the best matching cause class is used (hard-coded mapping). Classes
are used here as equivalents of enums, in order not to create actual enums.
All is done for simplicity of testing. This factory is not supposed to be
used outside of Kopf's own tests, is not packaged, is not delivered, and
is not available to the users.
"""
def make_cause(
cls=ResourceChangingCause,
*,
resource=resource,
type=None,
raw=None,
body=None,
diff=(),
reason='some-reason',
initial=False,
activity=None,
settings=None,
):
if cls is ActivityCause or cls is ActivityRegistry:
return ActivityCause(
logger=logging.getLogger('kopf.test.fake.logger'),
activity=activity,
settings=settings,
)
if cls is ResourceCause or cls is ResourceRegistry or cls is SimpleRegistry:
return ResourceCause(
logger=logging.getLogger('kopf.test.fake.logger'),
resource=resource,
patch=Patch(),
memo=Memo(),
body=Body(body if body is not None else {}),
)
if cls is ResourceWatchingCause or cls is ResourceWatchingRegistry:
return ResourceWatchingCause(
logger=logging.getLogger('kopf.test.fake.logger'),
resource=resource,
patch=Patch(),
memo=Memo(),
body=Body(body if body is not None else {}),
type=type,
raw=raw,
)
if cls is ResourceChangingCause or cls is ResourceChangingRegistry:
return ResourceChangingCause(
logger=logging.getLogger('kopf.test.fake.logger'),
resource=resource,
patch=Patch(),
memo=Memo(),
body=Body(body if body is not None else {}),
diff=Diff(DiffItem(*d) for d in diff),
initial=initial,
reason=reason,
)
raise TypeError(f"Cause/registry type {cls} is not supported by this fixture.")
return make_cause
40 changes: 20 additions & 20 deletions tests/registries/legacy-1/test_legacy1_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from kopf.structs.resources import Resource


def test_on_create_minimal(mocker):
def test_on_create_minimal(cause_factory):
registry = kopf.get_default_registry()
resource = Resource('group', 'version', 'plural')
cause = mocker.MagicMock(resource=resource, reason=Reason.CREATE)
cause = cause_factory(resource=resource, reason=Reason.CREATE)

@kopf.on.create('group', 'version', 'plural')
def fn(**_):
Expand All @@ -31,10 +31,10 @@ def fn(**_):
assert handlers[0].when is None


def test_on_update_minimal(mocker):
def test_on_update_minimal(cause_factory):
registry = kopf.get_default_registry()
resource = Resource('group', 'version', 'plural')
cause = mocker.MagicMock(resource=resource, reason=Reason.UPDATE)
cause = cause_factory(resource=resource, reason=Reason.UPDATE)

@kopf.on.update('group', 'version', 'plural')
def fn(**_):
Expand All @@ -53,10 +53,10 @@ def fn(**_):
assert handlers[0].when is None


def test_on_delete_minimal(mocker):
def test_on_delete_minimal(cause_factory):
registry = kopf.get_default_registry()
resource = Resource('group', 'version', 'plural')
cause = mocker.MagicMock(resource=resource, reason=Reason.DELETE)
cause = cause_factory(resource=resource, reason=Reason.DELETE)

@kopf.on.delete('group', 'version', 'plural')
def fn(**_):
Expand All @@ -75,11 +75,11 @@ def fn(**_):
assert handlers[0].when is None


def test_on_field_minimal(mocker):
def test_on_field_minimal(cause_factory):
registry = kopf.get_default_registry()
resource = Resource('group', 'version', 'plural')
diff = [('op', ('field', 'subfield'), 'old', 'new')]
cause = mocker.MagicMock(resource=resource, reason=Reason.UPDATE, diff=diff)
cause = cause_factory(resource=resource, reason=Reason.UPDATE, diff=diff)

@kopf.on.field('group', 'version', 'plural', 'field.subfield')
def fn(**_):
Expand All @@ -105,10 +105,10 @@ def fn(**_):
pass


def test_on_create_with_all_kwargs(mocker):
def test_on_create_with_all_kwargs(mocker, cause_factory):
registry = GlobalRegistry()
resource = Resource('group', 'version', 'plural')
cause = mocker.MagicMock(resource=resource, reason=Reason.CREATE)
cause = cause_factory(resource=resource, reason=Reason.CREATE)
mocker.patch('kopf.reactor.registries.match', return_value=True)

when = lambda **_: False
Expand All @@ -134,10 +134,10 @@ def fn(**_):
assert handlers[0].annotations == {'someanno': 'somevalue'}
assert handlers[0].when == when

def test_on_update_with_all_kwargs(mocker):
def test_on_update_with_all_kwargs(mocker, cause_factory):
registry = GlobalRegistry()
resource = Resource('group', 'version', 'plural')
cause = mocker.MagicMock(resource=resource, reason=Reason.UPDATE)
cause = cause_factory(resource=resource, reason=Reason.UPDATE)
mocker.patch('kopf.reactor.registries.match', return_value=True)

when = lambda **_: False
Expand Down Expand Up @@ -168,10 +168,10 @@ def fn(**_):
pytest.param(True, id='optional'),
pytest.param(False, id='mandatory'),
])
def test_on_delete_with_all_kwargs(mocker, optional):
def test_on_delete_with_all_kwargs(mocker, optional, cause_factory):
registry = GlobalRegistry()
resource = Resource('group', 'version', 'plural')
cause = mocker.MagicMock(resource=resource, reason=Reason.DELETE)
cause = cause_factory(resource=resource, reason=Reason.DELETE)
mocker.patch('kopf.reactor.registries.match', return_value=True)

when = lambda **_: False
Expand All @@ -198,11 +198,11 @@ def fn(**_):
assert handlers[0].when == when


def test_on_field_with_all_kwargs(mocker):
def test_on_field_with_all_kwargs(mocker, cause_factory):
registry = GlobalRegistry()
resource = Resource('group', 'version', 'plural')
diff = [('op', ('field', 'subfield'), 'old', 'new')]
cause = mocker.MagicMock(resource=resource, reason=Reason.UPDATE, diff=diff)
cause = cause_factory(resource=resource, reason=Reason.UPDATE, diff=diff)
mocker.patch('kopf.reactor.registries.match', return_value=True)

when = lambda **_: False
Expand Down Expand Up @@ -245,8 +245,8 @@ def fn(**_):
pass


def test_subhandler_declaratively(mocker, parent_handler):
cause = mocker.MagicMock(reason=Reason.UPDATE, diff=None)
def test_subhandler_declaratively(parent_handler, cause_factory):
cause = cause_factory(reason=Reason.UPDATE)

registry = SimpleRegistry()
subregistry_var.set(registry)
Expand All @@ -263,8 +263,8 @@ def fn(**_):
assert handlers[0].fn is fn


def test_subhandler_imperatively(mocker, parent_handler):
cause = mocker.MagicMock(reason=Reason.UPDATE, diff=None)
def test_subhandler_imperatively(parent_handler, cause_factory):
cause = cause_factory(reason=Reason.UPDATE)

registry = SimpleRegistry()
subregistry_var.set(registry)
Expand Down
Loading

0 comments on commit 49b342b

Please sign in to comment.