diff --git a/tests/registries/conftest.py b/tests/registries/conftest.py index 8c0b8de0..693571ab 100644 --- a/tests/registries/conftest.py +++ b/tests/registries/conftest.py @@ -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=[ @@ -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 diff --git a/tests/registries/legacy-1/test_legacy1_decorators.py b/tests/registries/legacy-1/test_legacy1_decorators.py index 39eb0663..f661da92 100644 --- a/tests/registries/legacy-1/test_legacy1_decorators.py +++ b/tests/registries/legacy-1/test_legacy1_decorators.py @@ -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(**_): @@ -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(**_): @@ -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(**_): @@ -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(**_): @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/tests/registries/legacy-1/test_legacy1_handler_matching.py b/tests/registries/legacy-1/test_legacy1_handler_matching.py index 744bd607..5d674ce6 100644 --- a/tests/registries/legacy-1/test_legacy1_handler_matching.py +++ b/tests/registries/legacy-1/test_legacy1_handler_matching.py @@ -32,31 +32,28 @@ def register_fn(registry, resource): @pytest.fixture(params=[ - pytest.param(None, id='without-diff'), pytest.param([], id='with-empty-diff'), ]) -def cause_no_diff(request, resource): +def cause_no_diff(request, cause_factory): body = {'metadata': {'labels': {'somelabel': 'somevalue'}, 'annotations': {'someannotation': 'somevalue'}}} - return Mock(resource=resource, reason='some-reason', diff=request.param, body=body) + return cause_factory(diff=request.param, body=body) @pytest.fixture(params=[ pytest.param([('op', ('some-field',), 'old', 'new')], id='with-field-diff'), ]) -def cause_with_diff(resource): +def cause_with_diff(request, cause_factory): body = {'metadata': {'labels': {'somelabel': 'somevalue'}, 'annotations': {'someannotation': 'somevalue'}}} - diff = [('op', ('some-field',), 'old', 'new')] - return Mock(resource=resource, reason='some-reason', diff=diff, body=body) + return cause_factory(diff=request.param, body=body) @pytest.fixture(params=[ - pytest.param(None, id='without-diff'), pytest.param([], id='with-empty-diff'), pytest.param([('op', ('some-field',), 'old', 'new')], id='with-field-diff'), ]) -def cause_any_diff(resource, request): +def cause_any_diff(request, cause_factory): body = {'metadata': {'labels': {'somelabel': 'somevalue'}, 'annotations': {'someannotation': 'somevalue'}}} - return Mock(resource=resource, reason='some-reason', diff=request.param, body=body) + return cause_factory(diff=request.param, body=body) # @@ -88,8 +85,9 @@ def test_catchall_handlers_with_field_ignored(cause_no_diff, registry, register_ pytest.param({'somelabel': 'somevalue'}, id='with-label'), pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, id='with-extra-label'), ]) -def test_catchall_handlers_with_labels_satisfied(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_satisfied( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': 'somevalue'}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -101,8 +99,9 @@ def test_catchall_handlers_with_labels_satisfied(registry, register_fn, resource pytest.param({'somelabel': 'othervalue'}, id='with-other-value'), pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), ]) -def test_catchall_handlers_with_labels_not_satisfied(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_not_satisfied( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': 'somevalue'}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -113,8 +112,9 @@ def test_catchall_handlers_with_labels_not_satisfied(registry, register_fn, reso pytest.param({'somelabel': 'somevalue'}, id='with-label'), pytest.param({'somelabel': 'othervalue'}, id='with-other-value'), ]) -def test_catchall_handlers_with_labels_exist(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_exist( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': None}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -125,8 +125,9 @@ def test_catchall_handlers_with_labels_exist(registry, register_fn, resource, la pytest.param({}, id='without-label'), pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), ]) -def test_catchall_handlers_with_labels_not_exist(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_not_exist( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': None}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -140,8 +141,9 @@ def test_catchall_handlers_with_labels_not_exist(registry, register_fn, resource pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, id='with-extra-label'), ]) -def test_catchall_handlers_without_labels(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_without_labels( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels=None) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -152,8 +154,9 @@ def test_catchall_handlers_without_labels(registry, register_fn, resource, label pytest.param({'someannotation': 'somevalue'}, id='with-annotation'), pytest.param({'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-extra-annotation'), ]) -def test_catchall_handlers_with_annotations_satisfied(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_with_annotations_satisfied( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations={'someannotation': 'somevalue'}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -165,8 +168,9 @@ def test_catchall_handlers_with_annotations_satisfied(registry, register_fn, res pytest.param({'someannotation': 'othervalue'}, id='with-other-value'), pytest.param({'otherannotation': 'othervalue'}, id='with-other-annotation'), ]) -def test_catchall_handlers_with_annotations_not_satisfied(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_with_annotations_not_satisfied( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations={'someannotation': 'somevalue'}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -177,8 +181,9 @@ def test_catchall_handlers_with_annotations_not_satisfied(registry, register_fn, pytest.param({'someannotation': 'somevalue'}, id='with-annotation'), pytest.param({'someannotation': 'othervalue'}, id='with-other-value'), ]) -def test_catchall_handlers_with_annotations_exist(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_with_annotations_exist( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations={'someannotation': None}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -189,8 +194,9 @@ def test_catchall_handlers_with_annotations_exist(registry, register_fn, resourc pytest.param({}, id='without-annotation'), pytest.param({'otherannotation': 'othervalue'}, id='with-other-annotation'), ]) -def test_catchall_handlers_with_annotations_not_exist(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_with_annotations_not_exist( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations={'someannotation': None}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -204,8 +210,9 @@ def test_catchall_handlers_with_annotations_not_exist(registry, register_fn, res pytest.param({'otherannotation': 'othervalue'}, id='with-other-annotation'), pytest.param({'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-extra-annotation'), ]) -def test_catchall_handlers_without_annotations(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_without_annotations( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations=None) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -218,8 +225,9 @@ def test_catchall_handlers_without_annotations(registry, register_fn, resource, pytest.param({'somelabel': 'somevalue'}, {'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-label-extra-annotation'), pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, {'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-extra-label-extra-annotation'), ]) -def test_catchall_handlers_with_labels_and_annotations_satisfied(registry, register_fn, resource, labels, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels, 'annotations': annotations}}) +def test_catchall_handlers_with_labels_and_annotations_satisfied( + cause_factory, registry, register_fn, resource, labels, annotations): + cause = cause_factory(body={'metadata': {'labels': labels, 'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': 'somevalue'}, annotations={'someannotation': 'somevalue'}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) @@ -233,8 +241,9 @@ def test_catchall_handlers_with_labels_and_annotations_satisfied(registry, regis pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, id='with-extra-label'), ]) -def test_catchall_handlers_with_labels_and_annotations_not_satisfied(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_and_annotations_not_satisfied( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': 'somevalue'}, annotations={'someannotation': 'somevalue'}) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): handlers = registry.get_cause_handlers(cause) diff --git a/tests/registries/legacy-1/test_legacy1_id_detection.py b/tests/registries/legacy-1/test_legacy1_id_detection.py index e26fbda0..55dd87dc 100644 --- a/tests/registries/legacy-1/test_legacy1_id_detection.py +++ b/tests/registries/legacy-1/test_legacy1_id_detection.py @@ -71,14 +71,15 @@ def test_id_of_lambda(): assert fn_id.startswith(f'lambda:{__file__}:') -def test_with_no_hints(mocker): +def test_with_no_hints(mocker, cause_factory): get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id', return_value='some-id') + cause = cause_factory() registry = SimpleRegistry() with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"): registry.register(some_fn) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): - handlers = registry.get_cause_handlers(mocker.MagicMock()) + handlers = registry.get_cause_handlers(cause) assert get_fn_id.called @@ -88,14 +89,15 @@ def test_with_no_hints(mocker): @pytest.mark.skip("Prefixes are removed from the registries, even from the legacy ones.") -def test_with_prefix(mocker): +def test_with_prefix(mocker, cause_factory): get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id', return_value='some-id') + cause = cause_factory() registry = SimpleRegistry() with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"): registry.register(some_fn) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): - handlers = registry.get_cause_handlers(mocker.MagicMock()) + handlers = registry.get_cause_handlers(cause) assert get_fn_id.called @@ -104,15 +106,16 @@ def test_with_prefix(mocker): assert handlers[0].id == 'some-prefix/some-id' -def test_with_suffix(mocker, field): +def test_with_suffix(mocker, field, cause_factory): get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id', return_value='some-id') diff = [('add', ('some-field', 'sub-field'), 'old', 'new')] + cause = cause_factory(diff=diff) registry = SimpleRegistry() with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"): registry.register(some_fn, field=field) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): - handlers = registry.get_cause_handlers(mocker.MagicMock(diff=diff)) + handlers = registry.get_cause_handlers(cause) assert get_fn_id.called @@ -122,15 +125,16 @@ def test_with_suffix(mocker, field): @pytest.mark.skip("Prefixes are removed from the registries, even from the legacy ones.") -def test_with_prefix_and_suffix(mocker, field): +def test_with_prefix_and_suffix(mocker, field, cause_factory): get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id', return_value='some-id') diff = [('add', ('some-field', 'sub-field'), 'old', 'new')] + cause = cause_factory(diff=diff) registry = SimpleRegistry(prefix='some-prefix') with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"): registry.register(some_fn, field=field) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): - handlers = registry.get_cause_handlers(mocker.MagicMock(diff=diff)) + handlers = registry.get_cause_handlers(cause) assert get_fn_id.called @@ -140,15 +144,16 @@ def test_with_prefix_and_suffix(mocker, field): @pytest.mark.skip("Prefixes are removed from the registries, even from the legacy ones.") -def test_with_explicit_id_and_prefix_and_suffix(mocker, field): +def test_with_explicit_id_and_prefix_and_suffix(mocker, field, cause_factory): get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id', return_value='some-id') diff = [('add', ('some-field', 'sub-field'), 'old', 'new')] + cause = cause_factory(diff=diff) registry = SimpleRegistry(prefix='some-prefix') with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"): registry.register(some_fn, id='explicit-id', field=field) with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"): - handlers = registry.get_cause_handlers(mocker.MagicMock(diff=diff)) + handlers = registry.get_cause_handlers(cause) assert not get_fn_id.called diff --git a/tests/registries/legacy-1/test_legacy1_registering.py b/tests/registries/legacy-1/test_legacy1_registering.py index fe7fb4ab..0ca33c17 100644 --- a/tests/registries/legacy-1/test_legacy1_registering.py +++ b/tests/registries/legacy-1/test_legacy1_registering.py @@ -10,9 +10,9 @@ def some_fn(): pass -def test_simple_registry_via_iter(mocker): - cause = mocker.Mock(event=None, diff=None) +def test_simple_registry_via_iter(cause_factory): + cause = cause_factory() registry = SimpleRegistry() iterator = registry.iter_cause_handlers(cause) @@ -26,9 +26,9 @@ def test_simple_registry_via_iter(mocker): assert not handlers -def test_simple_registry_via_list(mocker): - cause = mocker.Mock(event=None, diff=None) +def test_simple_registry_via_list(cause_factory): + cause = cause_factory() registry = SimpleRegistry() with pytest.deprecated_call(match=r"use ResourceChangingRegistry.get_handlers\(\)"): handlers = registry.get_cause_handlers(cause) @@ -39,9 +39,9 @@ def test_simple_registry_via_list(mocker): assert not handlers -def test_simple_registry_with_minimal_signature(mocker): - cause = mocker.Mock(event=None, diff=None) +def test_simple_registry_with_minimal_signature(cause_factory): + cause = cause_factory() registry = SimpleRegistry() with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"): registry.register(some_fn) @@ -52,9 +52,9 @@ def test_simple_registry_with_minimal_signature(mocker): assert handlers[0].fn is some_fn -def test_global_registry_via_iter(mocker, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) +def test_global_registry_via_iter(cause_factory): + cause = cause_factory() registry = GlobalRegistry() iterator = registry.iter_cause_handlers(cause) @@ -68,9 +68,9 @@ def test_global_registry_via_iter(mocker, resource): assert not handlers -def test_global_registry_via_list(mocker, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) +def test_global_registry_via_list(cause_factory): + cause = cause_factory() registry = GlobalRegistry() with pytest.deprecated_call(match=r"use OperatorRegistry.get_resource_changing_handlers\(\)"): handlers = registry.get_cause_handlers(cause) @@ -81,9 +81,9 @@ def test_global_registry_via_list(mocker, resource): assert not handlers -def test_global_registry_with_minimal_signature(mocker, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) +def test_global_registry_with_minimal_signature(cause_factory, resource): + cause = cause_factory() registry = GlobalRegistry() with pytest.deprecated_call(match=r"use OperatorRegistry.register_resource_changing_handler\(\)"): registry.register_cause_handler(resource.group, resource.version, resource.plural, some_fn) diff --git a/tests/registries/legacy-2/test_legacy2_decorators.py b/tests/registries/legacy-2/test_legacy2_decorators.py index 47b2d539..20824196 100644 --- a/tests/registries/legacy-2/test_legacy2_decorators.py +++ b/tests/registries/legacy-2/test_legacy2_decorators.py @@ -67,10 +67,12 @@ def fn(**_): # Resume handlers are mixed-in into all resource-changing reactions with initial listing. @pytest.mark.parametrize('reason', HANDLER_REASONS) -def test_on_resume_minimal(mocker, reason): +def test_on_resume_minimal( + reason, cause_factory): + registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=False) + cause = cause_factory(resource=resource, reason=reason, initial=True) @kopf.on.resume('group', 'version', 'plural') def fn(**_): @@ -92,10 +94,12 @@ def fn(**_): assert handlers[0].when is None -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(**_): @@ -117,10 +121,12 @@ 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(**_): @@ -142,10 +148,12 @@ 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(**_): @@ -167,11 +175,13 @@ 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(**_): @@ -268,10 +278,12 @@ def fn(**_): # Resume handlers are mixed-in into all resource-changing reactions with initial listing. @pytest.mark.parametrize('reason', HANDLER_REASONS) -def test_on_resume_with_all_kwargs(mocker, reason): +def test_on_resume_with_all_kwargs( + mocker, reason, cause_factory): + registry = OperatorRegistry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=False) + cause = cause_factory(resource=resource, reason=reason, initial=True) mocker.patch('kopf.reactor.registries.match', return_value=True) when = lambda **_: False @@ -304,10 +316,12 @@ def fn(**_): assert handlers[0].when == when -def test_on_create_with_all_kwargs(mocker): +def test_on_create_with_all_kwargs( + mocker, cause_factory): + registry = OperatorRegistry() 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 @@ -338,10 +352,12 @@ def fn(**_): assert handlers[0].when == when -def test_on_update_with_all_kwargs(mocker): +def test_on_update_with_all_kwargs( + mocker, cause_factory): + registry = OperatorRegistry() 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 @@ -376,10 +392,12 @@ 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 = OperatorRegistry() 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 @@ -411,11 +429,13 @@ 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 = OperatorRegistry() 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 @@ -462,9 +482,10 @@ 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 = ResourceChangingRegistry() subregistry_var.set(registry) @@ -478,9 +499,10 @@ 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 = ResourceChangingRegistry() subregistry_var.set(registry) diff --git a/tests/registries/legacy-2/test_legacy2_handler_matching.py b/tests/registries/legacy-2/test_legacy2_handler_matching.py index eec002a5..d692aeff 100644 --- a/tests/registries/legacy-2/test_legacy2_handler_matching.py +++ b/tests/registries/legacy-2/test_legacy2_handler_matching.py @@ -30,31 +30,28 @@ def register_fn(registry, resource): @pytest.fixture(params=[ - pytest.param(None, id='without-diff'), pytest.param([], id='with-empty-diff'), ]) -def cause_no_diff(request, resource): +def cause_no_diff(request, cause_factory): body = {'metadata': {'labels': {'somelabel': 'somevalue'}, 'annotations': {'someannotation': 'somevalue'}}} - return Mock(resource=resource, reason='some-reason', diff=request.param, body=body) + return cause_factory(diff=request.param, body=body) @pytest.fixture(params=[ pytest.param([('op', ('some-field',), 'old', 'new')], id='with-field-diff'), ]) -def cause_with_diff(resource): +def cause_with_diff(request, cause_factory): body = {'metadata': {'labels': {'somelabel': 'somevalue'}, 'annotations': {'someannotation': 'somevalue'}}} - diff = [('op', ('some-field',), 'old', 'new')] - return Mock(resource=resource, reason='some-reason', diff=diff, body=body) + return cause_factory(diff=request.param, body=body) @pytest.fixture(params=[ - pytest.param(None, id='without-diff'), pytest.param([], id='with-empty-diff'), pytest.param([('op', ('some-field',), 'old', 'new')], id='with-field-diff'), ]) -def cause_any_diff(resource, request): +def cause_any_diff(request, cause_factory): body = {'metadata': {'labels': {'somelabel': 'somevalue'}, 'annotations': {'someannotation': 'somevalue'}}} - return Mock(resource=resource, reason='some-reason', diff=request.param, body=body) + return cause_factory(diff=request.param, body=body) # @@ -86,8 +83,9 @@ def test_catchall_handlers_with_field_ignored(cause_no_diff, registry, register_ pytest.param({'somelabel': 'somevalue'}, id='with-label'), pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, id='with-extra-label'), ]) -def test_catchall_handlers_with_labels_satisfied(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_satisfied( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': 'somevalue'}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -99,8 +97,9 @@ def test_catchall_handlers_with_labels_satisfied(registry, register_fn, resource pytest.param({'somelabel': 'othervalue'}, id='with-other-value'), pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), ]) -def test_catchall_handlers_with_labels_not_satisfied(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_not_satisfied( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': 'somevalue'}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -111,8 +110,9 @@ def test_catchall_handlers_with_labels_not_satisfied(registry, register_fn, reso pytest.param({'somelabel': 'somevalue'}, id='with-label'), pytest.param({'somelabel': 'othervalue'}, id='with-other-value'), ]) -def test_catchall_handlers_with_labels_exist(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_exist( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': None}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -123,8 +123,9 @@ def test_catchall_handlers_with_labels_exist(registry, register_fn, resource, la pytest.param({}, id='without-label'), pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), ]) -def test_catchall_handlers_with_labels_not_exist(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_not_exist( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': None}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -138,8 +139,9 @@ def test_catchall_handlers_with_labels_not_exist(registry, register_fn, resource pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, id='with-extra-label'), ]) -def test_catchall_handlers_without_labels(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_without_labels( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels=None) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -150,8 +152,9 @@ def test_catchall_handlers_without_labels(registry, register_fn, resource, label pytest.param({'someannotation': 'somevalue'}, id='with-annotation'), pytest.param({'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-extra-annotation'), ]) -def test_catchall_handlers_with_annotations_satisfied(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_with_annotations_satisfied( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations={'someannotation': 'somevalue'}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -163,8 +166,9 @@ def test_catchall_handlers_with_annotations_satisfied(registry, register_fn, res pytest.param({'someannotation': 'othervalue'}, id='with-other-value'), pytest.param({'otherannotation': 'othervalue'}, id='with-other-annotation'), ]) -def test_catchall_handlers_with_annotations_not_satisfied(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_with_annotations_not_satisfied( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations={'someannotation': 'somevalue'}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -175,8 +179,9 @@ def test_catchall_handlers_with_annotations_not_satisfied(registry, register_fn, pytest.param({'someannotation': 'somevalue'}, id='with-annotation'), pytest.param({'someannotation': 'othervalue'}, id='with-other-value'), ]) -def test_catchall_handlers_with_annotations_exist(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_with_annotations_exist( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations={'someannotation': None}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -187,8 +192,9 @@ def test_catchall_handlers_with_annotations_exist(registry, register_fn, resourc pytest.param({}, id='without-annotation'), pytest.param({'otherannotation': 'othervalue'}, id='with-other-annotation'), ]) -def test_catchall_handlers_with_annotations_not_exist(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_with_annotations_not_exist( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations={'someannotation': None}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -202,8 +208,9 @@ def test_catchall_handlers_with_annotations_not_exist(registry, register_fn, res pytest.param({'otherannotation': 'othervalue'}, id='with-other-annotation'), pytest.param({'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-extra-annotation'), ]) -def test_catchall_handlers_without_annotations(registry, register_fn, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) +def test_catchall_handlers_without_annotations( + cause_factory, registry, register_fn, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, annotations=None) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -216,8 +223,9 @@ def test_catchall_handlers_without_annotations(registry, register_fn, resource, pytest.param({'somelabel': 'somevalue'}, {'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-label-extra-annotation'), pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, {'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-extra-label-extra-annotation'), ]) -def test_catchall_handlers_with_labels_and_annotations_satisfied(registry, register_fn, resource, labels, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels, 'annotations': annotations}}) +def test_catchall_handlers_with_labels_and_annotations_satisfied( + cause_factory, registry, register_fn, resource, labels, annotations): + cause = cause_factory(body={'metadata': {'labels': labels, 'annotations': annotations}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': 'somevalue'}, annotations={'someannotation': 'somevalue'}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -231,8 +239,9 @@ def test_catchall_handlers_with_labels_and_annotations_satisfied(registry, regis pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, id='with-extra-label'), ]) -def test_catchall_handlers_with_labels_and_annotations_not_satisfied(registry, register_fn, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) +def test_catchall_handlers_with_labels_and_annotations_not_satisfied( + cause_factory, registry, register_fn, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) register_fn(some_fn, reason=None, field=None, labels={'somelabel': 'somevalue'}, annotations={'someannotation': 'somevalue'}) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -244,17 +253,9 @@ def test_catchall_handlers_with_labels_and_annotations_not_satisfied(registry, r pytest.param(lambda body=None, **_: body['spec']['name'] == 'test', id='with-when'), pytest.param(lambda **_: True, id='with-other-when'), ]) -def test_catchall_handlers_with_when_match(registry, register_fn, resource, when): - cause = ResourceChangingCause( - resource=resource, - reason='some-reason', - diff=None, - body=Body({'spec': {'name': 'test'}}), - logger=None, - patch=None, - memo=None, - initial=None - ) +def test_catchall_handlers_with_when_match( + cause_factory, registry, register_fn, resource, when): + cause = cause_factory(body={'spec': {'name': 'test'}}) register_fn(some_fn, reason=None, field=None, when=when) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -265,17 +266,9 @@ def test_catchall_handlers_with_when_match(registry, register_fn, resource, when pytest.param(lambda body=None, **_: body['spec']['name'] != "test", id='with-when'), pytest.param(lambda **_: False, id='with-other-when'), ]) -def test_catchall_handlers_with_when_not_match(registry, register_fn, resource, when): - cause = ResourceChangingCause( - resource=resource, - reason='some-reason', - diff=None, - body=Body({'spec': {'name': 'test'}}), - logger=None, - patch=None, - memo=None, - initial=None - ) +def test_catchall_handlers_with_when_not_match( + cause_factory, registry, register_fn, resource, when): + cause = cause_factory(body={'spec': {'name': 'test'}}) register_fn(some_fn, reason=None, field=None, when=when) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -339,14 +332,14 @@ def test_relevant_handlers_with_annotations_not_satisfied(cause_any_diff, regist def test_relevant_handlers_with_filter_satisfied(cause_any_diff, registry, register_fn): - register_fn(some_fn, reason='some-reason', when=lambda *_: True) + register_fn(some_fn, reason='some-reason', when=lambda **_: True) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause_any_diff) assert handlers def test_relevant_handlers_with_filter_not_satisfied(cause_any_diff, registry, register_fn): - register_fn(some_fn, reason='some-reason', when=lambda *_: False) + register_fn(some_fn, reason='some-reason', when=lambda **_: False) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause_any_diff) assert not handlers @@ -395,14 +388,14 @@ def test_irrelevant_handlers_with_annotations_not_satisfied(cause_any_diff, regi def test_irrelevant_handlers_with_when_satisfied(cause_any_diff, registry, register_fn): - register_fn(some_fn, reason='another-reason', when=lambda *_: True) + register_fn(some_fn, reason='another-reason', when=lambda **_: True) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause_any_diff) assert not handlers def test_irrelevant_handlers_with_when_not_satisfied(cause_any_diff, registry, register_fn): - register_fn(some_fn, reason='another-reason', when=lambda *_: False) + register_fn(some_fn, reason='another-reason', when=lambda **_: False) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause_any_diff) assert not handlers diff --git a/tests/registries/legacy-2/test_legacy2_registering.py b/tests/registries/legacy-2/test_legacy2_registering.py index f9efae4b..1d371772 100644 --- a/tests/registries/legacy-2/test_legacy2_registering.py +++ b/tests/registries/legacy-2/test_legacy2_registering.py @@ -2,6 +2,7 @@ import pytest +from kopf.reactor.causation import ResourceWatchingCause, ResourceChangingCause from kopf.structs.handlers import Activity @@ -10,9 +11,10 @@ def some_fn(): pass -def test_generic_registry_via_iter(mocker, generic_registry_cls): - cause = mocker.Mock(event=None, diff=None) +def test_generic_registry_via_iter( + generic_registry_cls, cause_factory): + cause = cause_factory(generic_registry_cls) registry = generic_registry_cls() iterator = registry.iter_handlers(cause) @@ -25,9 +27,10 @@ def test_generic_registry_via_iter(mocker, generic_registry_cls): assert not handlers -def test_generic_registry_via_list(mocker, generic_registry_cls): - cause = mocker.Mock(event=None, diff=None) +def test_generic_registry_via_list( + generic_registry_cls, cause_factory): + cause = cause_factory(generic_registry_cls) registry = generic_registry_cls() handlers = registry.get_handlers(cause) @@ -37,9 +40,10 @@ def test_generic_registry_via_list(mocker, generic_registry_cls): assert not handlers -def test_generic_registry_with_minimal_signature(mocker, generic_registry_cls): - cause = mocker.Mock(event=None, diff=None) +def test_generic_registry_with_minimal_signature( + generic_registry_cls, cause_factory): + cause = cause_factory(generic_registry_cls) registry = generic_registry_cls() with pytest.deprecated_call(match=r"use @kopf.on"): registry.register(some_fn) @@ -67,9 +71,9 @@ def test_operator_registry_with_activity_via_iter( def test_operator_registry_with_resource_watching_via_iter( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, cause_factory): + cause = cause_factory(ResourceWatchingCause) registry = operator_registry_cls() iterator = registry.iter_resource_watching_handlers(cause) @@ -84,9 +88,9 @@ def test_operator_registry_with_resource_watching_via_iter( def test_operator_registry_with_resource_changing_via_iter( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, cause_factory): + cause = cause_factory(ResourceChangingCause) registry = operator_registry_cls() iterator = registry.iter_resource_changing_handlers(cause) @@ -115,10 +119,11 @@ def test_operator_registry_with_activity_via_list( def test_operator_registry_with_resource_watching_via_list( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, cause_factory): + cause = cause_factory(ResourceWatchingCause) registry = operator_registry_cls() + with pytest.deprecated_call(match=r"use registry.resource_watching_handlers"): handlers = registry.get_resource_watching_handlers(cause) @@ -129,10 +134,11 @@ def test_operator_registry_with_resource_watching_via_list( def test_operator_registry_with_resource_changing_via_list( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, cause_factory): + cause = cause_factory(ResourceChangingCause) registry = operator_registry_cls() + with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): handlers = registry.get_resource_changing_handlers(cause) @@ -157,10 +163,11 @@ def test_operator_registry_with_activity_with_minimal_signature( def test_operator_registry_with_resource_watching_with_minimal_signature( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, cause_factory, resource): + cause = cause_factory(ResourceWatchingCause) registry = operator_registry_cls() + with pytest.deprecated_call(match=r"use @kopf.on"): registry.register_resource_watching_handler(resource.group, resource.version, resource.plural, some_fn) with pytest.deprecated_call(match=r"use registry.resource_watching_handlers"): @@ -171,10 +178,11 @@ def test_operator_registry_with_resource_watching_with_minimal_signature( def test_operator_registry_with_resource_changing_with_minimal_signature( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, cause_factory, resource): + cause = cause_factory(ResourceChangingCause) registry = operator_registry_cls() + with pytest.deprecated_call(match=r"use @kopf.on"): registry.register_resource_changing_handler(resource.group, resource.version, resource.plural, some_fn) with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"): diff --git a/tests/registries/legacy-2/test_legacy2_resumes_mixed_in.py b/tests/registries/legacy-2/test_legacy2_resumes_mixed_in.py index 629d6ec4..fab83f1f 100644 --- a/tests/registries/legacy-2/test_legacy2_resumes_mixed_in.py +++ b/tests/registries/legacy-2/test_legacy2_resumes_mixed_in.py @@ -5,12 +5,13 @@ from kopf.structs.resources import Resource -@pytest.mark.parametrize('deleted', [True, False, None]) +@pytest.mark.parametrize('deleted', [True, False]) @pytest.mark.parametrize('reason', HANDLER_REASONS) -def test_resumes_ignored_for_non_initial_causes(mocker, reason, deleted): +def test_resumes_ignored_for_non_initial_causes(reason, deleted, cause_factory): registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=False, deleted=deleted) + cause = cause_factory(resource=resource, reason=reason, initial=False, + body={'metadata': {'deletionTimestamp': '...'} if deleted else {}}) @kopf.on.resume('group', 'version', 'plural') def fn(**_): @@ -22,10 +23,10 @@ def fn(**_): @pytest.mark.parametrize('reason', list(set(HANDLER_REASONS) - {Reason.DELETE})) -def test_resumes_selected_for_initial_non_deletions(mocker, reason): +def test_resumes_selected_for_initial_non_deletions(reason, cause_factory): registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=False) + cause = cause_factory(resource=resource, reason=reason, initial=True) @kopf.on.resume('group', 'version', 'plural') def fn(**_): @@ -38,10 +39,11 @@ def fn(**_): @pytest.mark.parametrize('reason', [Reason.DELETE]) -def test_resumes_ignored_for_initial_deletions_by_default(mocker, reason): +def test_resumes_ignored_for_initial_deletions_by_default(reason, cause_factory): registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=True) + cause = cause_factory(resource=resource, reason=reason, initial=True, + body={'metadata': {'deletionTimestamp': '...'}}) @kopf.on.resume('group', 'version', 'plural') def fn(**_): @@ -53,10 +55,11 @@ def fn(**_): @pytest.mark.parametrize('reason', [Reason.DELETE]) -def test_resumes_selected_for_initial_deletions_when_explicitly_marked(mocker, reason): +def test_resumes_selected_for_initial_deletions_when_explicitly_marked(reason, cause_factory): registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=True) + cause = cause_factory(resource=resource, reason=reason, initial=True, + body={'metadata': {'deletionTimestamp': '...'}}) @kopf.on.resume('group', 'version', 'plural', deleted=True) def fn(**_): diff --git a/tests/registries/test_decorators.py b/tests/registries/test_decorators.py index 3cd69431..ba3b9b62 100644 --- a/tests/registries/test_decorators.py +++ b/tests/registries/test_decorators.py @@ -61,10 +61,10 @@ def fn(**_): # Resume handlers are mixed-in into all resource-changing reactions with initial listing. @pytest.mark.parametrize('reason', HANDLER_REASONS) -def test_on_resume_minimal(mocker, reason): +def test_on_resume_minimal(reason, cause_factory): registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=False) + cause = cause_factory(resource=resource, reason=reason, initial=True) @kopf.on.resume('group', 'version', 'plural') def fn(**_): @@ -84,10 +84,10 @@ def fn(**_): assert handlers[0].when is None -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(**_): @@ -107,10 +107,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(**_): @@ -130,10 +130,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(**_): @@ -153,11 +153,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(**_): @@ -184,7 +184,7 @@ def fn(**_): pass -def test_on_startup_with_all_kwargs(mocker): +def test_on_startup_with_all_kwargs(): registry = OperatorRegistry() @kopf.on.startup( @@ -246,10 +246,10 @@ def fn(**_): # Resume handlers are mixed-in into all resource-changing reactions with initial listing. @pytest.mark.parametrize('reason', HANDLER_REASONS) -def test_on_resume_with_all_kwargs(mocker, reason): +def test_on_resume_with_all_kwargs(mocker, reason, cause_factory): registry = OperatorRegistry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=False) + cause = cause_factory(resource=resource, reason=reason, initial=True) mocker.patch('kopf.reactor.registries.match', return_value=True) when = lambda **_: False @@ -280,10 +280,10 @@ def fn(**_): assert handlers[0].when == when -def test_on_create_with_all_kwargs(mocker): +def test_on_create_with_all_kwargs(mocker, cause_factory): registry = OperatorRegistry() 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 @@ -312,10 +312,10 @@ def fn(**_): assert handlers[0].when == when -def test_on_update_with_all_kwargs(mocker): +def test_on_update_with_all_kwargs(mocker, cause_factory): registry = OperatorRegistry() 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 @@ -348,10 +348,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, cause_factory, optional): registry = OperatorRegistry() 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 @@ -381,11 +381,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 = OperatorRegistry() 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 @@ -430,8 +430,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 = ResourceChangingRegistry() subregistry_var.set(registry) @@ -446,8 +446,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 = ResourceChangingRegistry() subregistry_var.set(registry) diff --git a/tests/registries/test_decorators_deprecated_cooldown.py b/tests/registries/test_decorators_deprecated_cooldown.py index 80dfa175..fc1c6d01 100644 --- a/tests/registries/test_decorators_deprecated_cooldown.py +++ b/tests/registries/test_decorators_deprecated_cooldown.py @@ -58,10 +58,10 @@ def fn(**_): # Resume handlers are mixed-in into all resource-changing reactions with initial listing. @pytest.mark.parametrize('reason', HANDLER_REASONS) -def test_on_resume_with_cooldown(mocker, reason): +def test_on_resume_with_cooldown(mocker, reason, cause_factory): registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=False) + cause = cause_factory(resource=resource, reason=reason, initial=True) mocker.patch('kopf.reactor.registries.match', return_value=True) with pytest.deprecated_call(match=r"use backoff="): @@ -78,10 +78,10 @@ def fn(**_): assert handlers[0].cooldown == 78 -def test_on_create_with_cooldown(mocker): +def test_on_create_with_cooldown(mocker, 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) mocker.patch('kopf.reactor.registries.match', return_value=True) with pytest.deprecated_call(match=r"use backoff="): @@ -98,10 +98,10 @@ def fn(**_): assert handlers[0].cooldown == 78 -def test_on_update_with_cooldown(mocker): +def test_on_update_with_cooldown(mocker, 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) mocker.patch('kopf.reactor.registries.match', return_value=True) with pytest.deprecated_call(match=r"use backoff="): @@ -122,10 +122,10 @@ def fn(**_): pytest.param(True, id='optional'), pytest.param(False, id='mandatory'), ]) -def test_on_delete_with_cooldown(mocker, optional): +def test_on_delete_with_cooldown(mocker, optional, 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) mocker.patch('kopf.reactor.registries.match', return_value=True) with pytest.deprecated_call(match=r"use backoff="): @@ -142,11 +142,11 @@ def fn(**_): assert handlers[0].cooldown == 78 -def test_on_field_with_cooldown(mocker): +def test_on_field_with_cooldown(mocker, 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) mocker.patch('kopf.reactor.registries.match', return_value=True) with pytest.deprecated_call(match=r"use backoff="): diff --git a/tests/registries/test_handler_getting.py b/tests/registries/test_handler_getting.py index 7ec17a31..46ee93d6 100644 --- a/tests/registries/test_handler_getting.py +++ b/tests/registries/test_handler_getting.py @@ -2,6 +2,7 @@ import pytest +from kopf.reactor.causation import ResourceWatchingCause, ResourceChangingCause from kopf.structs.handlers import Activity @@ -10,9 +11,10 @@ def some_fn(): pass -def test_generic_registry_via_iter(mocker, generic_registry_cls): - cause = mocker.Mock(event=None, diff=None) +def test_generic_registry_via_iter( + generic_registry_cls, cause_factory): + cause = cause_factory(generic_registry_cls) registry = generic_registry_cls() iterator = registry.iter_handlers(cause) @@ -25,9 +27,10 @@ def test_generic_registry_via_iter(mocker, generic_registry_cls): assert not handlers -def test_generic_registry_via_list(mocker, generic_registry_cls): - cause = mocker.Mock(event=None, diff=None) +def test_generic_registry_via_list( + generic_registry_cls, cause_factory): + cause = cause_factory(generic_registry_cls) registry = generic_registry_cls() handlers = registry.get_handlers(cause) @@ -54,9 +57,9 @@ def test_operator_registry_with_activity_via_iter( def test_operator_registry_with_resource_watching_via_iter( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, resource, cause_factory): + cause = cause_factory(ResourceWatchingCause) registry = operator_registry_cls() iterator = registry.resource_watching_handlers[resource].iter_handlers(cause) @@ -70,9 +73,9 @@ def test_operator_registry_with_resource_watching_via_iter( def test_operator_registry_with_resource_changing_via_iter( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, resource, cause_factory): + cause = cause_factory(ResourceChangingCause) registry = operator_registry_cls() iterator = registry.resource_changing_handlers[resource].iter_handlers(cause) @@ -99,9 +102,9 @@ def test_operator_registry_with_activity_via_list( def test_operator_registry_with_resource_watching_via_list( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, resource, cause_factory): + cause = cause_factory(ResourceWatchingCause) registry = operator_registry_cls() handlers = registry.resource_watching_handlers[resource].get_handlers(cause) @@ -112,9 +115,9 @@ def test_operator_registry_with_resource_watching_via_list( def test_operator_registry_with_resource_changing_via_list( - mocker, operator_registry_cls, resource): - cause = mocker.Mock(resource=resource, event=None, diff=None) + operator_registry_cls, resource, cause_factory): + cause = cause_factory(ResourceChangingCause) registry = operator_registry_cls() handlers = registry.resource_changing_handlers[resource].get_handlers(cause) diff --git a/tests/registries/test_handler_matching.py b/tests/registries/test_handler_matching.py index 08d63439..d88702b6 100644 --- a/tests/registries/test_handler_matching.py +++ b/tests/registries/test_handler_matching.py @@ -7,7 +7,7 @@ from kopf.reactor.causation import ResourceChangingCause from kopf.structs.bodies import Body from kopf.structs.dicts import parse_field -from kopf.structs.diffs import DiffOperation, DiffItem +from kopf.structs.diffs import DiffOperation, DiffItem, Diff, EMPTY from kopf.structs.filters import MetaFilterToken from kopf.structs.handlers import ResourceChangingHandler, Reason, ALL_REASONS @@ -68,31 +68,28 @@ def factory(**kwargs): @pytest.fixture(params=[ - pytest.param(None, id='without-diff'), pytest.param([], id='with-empty-diff'), ]) -def cause_no_diff(request, resource): +def cause_no_diff(request, cause_factory): body = {'metadata': {'labels': {'somelabel': 'somevalue'}, 'annotations': {'someannotation': 'somevalue'}}} - return Mock(resource=resource, reason='some-reason', diff=request.param, body=body) + return cause_factory(diff=request.param, body=body) @pytest.fixture(params=[ pytest.param([('op', ('some-field',), 'old', 'new')], id='with-field-diff'), ]) -def cause_with_diff(resource): +def cause_with_diff(request, cause_factory): body = {'metadata': {'labels': {'somelabel': 'somevalue'}, 'annotations': {'someannotation': 'somevalue'}}} - diff = [('op', ('some-field',), 'old', 'new')] - return Mock(resource=resource, reason='some-reason', diff=diff, body=body) + return cause_factory(diff=request.param, body=body) @pytest.fixture(params=[ - pytest.param(None, id='without-diff'), pytest.param([], id='with-empty-diff'), pytest.param([('op', ('some-field',), 'old', 'new')], id='with-field-diff'), ]) -def cause_any_diff(resource, request): +def cause_any_diff(request, cause_factory): body = {'metadata': {'labels': {'somelabel': 'somevalue'}, 'annotations': {'someannotation': 'somevalue'}}} - return Mock(resource=resource, reason='some-reason', diff=request.param, body=body) + return cause_factory(diff=request.param, body=body) # @@ -128,8 +125,8 @@ def test_catchall_handlers_with_field_ignored( pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, id='with-extra-label'), ]) def test_catchall_handlers_with_exact_labels_satisfied( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels={'somelabel': 'somevalue'}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -141,8 +138,8 @@ def test_catchall_handlers_with_exact_labels_satisfied( pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), ]) def test_catchall_handlers_with_exact_labels_not_satisfied( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels={'somelabel': 'somevalue'}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers @@ -153,8 +150,8 @@ def test_catchall_handlers_with_exact_labels_not_satisfied( pytest.param({'somelabel': 'othervalue'}, id='with-other-value'), ]) def test_catchall_handlers_with_desired_labels_present( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels={'somelabel': MetaFilterToken.PRESENT}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -165,8 +162,8 @@ def test_catchall_handlers_with_desired_labels_present( pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), ]) def test_catchall_handlers_with_desired_labels_absent( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels={'somelabel': MetaFilterToken.PRESENT}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers @@ -177,8 +174,8 @@ def test_catchall_handlers_with_desired_labels_absent( pytest.param({'somelabel': 'othervalue'}, id='with-other-value'), ]) def test_catchall_handlers_with_undesired_labels_present( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels={'somelabel': MetaFilterToken.ABSENT}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers @@ -189,8 +186,8 @@ def test_catchall_handlers_with_undesired_labels_present( pytest.param({'otherlabel': 'othervalue'}, id='with-other-label'), ]) def test_catchall_handlers_with_undesired_labels_absent( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels={'somelabel': MetaFilterToken.ABSENT}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -202,8 +199,8 @@ def test_catchall_handlers_with_undesired_labels_absent( pytest.param({'somelabel': 'othervalue'}, id='with-other-value'), ]) def test_catchall_handlers_with_labels_callback_says_true( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels={'somelabel': _always}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -215,8 +212,8 @@ def test_catchall_handlers_with_labels_callback_says_true( pytest.param({'somelabel': 'othervalue'}, id='with-other-value'), ]) def test_catchall_handlers_with_labels_callback_says_false( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels={'somelabel': _never}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers @@ -230,8 +227,8 @@ def test_catchall_handlers_with_labels_callback_says_false( pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, id='with-extra-label'), ]) def test_catchall_handlers_without_labels( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels=None) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -242,8 +239,8 @@ def test_catchall_handlers_without_labels( pytest.param({'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-extra-annotation'), ]) def test_catchall_handlers_with_exact_annotations_satisfied( - registry, handler_factory, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) handler_factory(reason=None, annotations={'someannotation': 'somevalue'}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -255,8 +252,8 @@ def test_catchall_handlers_with_exact_annotations_satisfied( pytest.param({'otherannotation': 'othervalue'}, id='with-other-annotation'), ]) def test_catchall_handlers_with_exact_annotations_not_satisfied( - registry, handler_factory, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) handler_factory(reason=None, annotations={'someannotation': 'somevalue'}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers @@ -267,8 +264,8 @@ def test_catchall_handlers_with_exact_annotations_not_satisfied( pytest.param({'someannotation': 'othervalue'}, id='with-other-value'), ]) def test_catchall_handlers_with_desired_annotations_present( - registry, handler_factory, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) handler_factory(reason=None, annotations={'someannotation': MetaFilterToken.PRESENT}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -279,8 +276,8 @@ def test_catchall_handlers_with_desired_annotations_present( pytest.param({'otherannotation': 'othervalue'}, id='with-other-annotation'), ]) def test_catchall_handlers_with_desired_annotations_absent( - registry, handler_factory, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) handler_factory(reason=None, annotations={'someannotation': MetaFilterToken.PRESENT}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers @@ -291,8 +288,8 @@ def test_catchall_handlers_with_desired_annotations_absent( pytest.param({'someannotation': 'othervalue'}, id='with-other-value'), ]) def test_catchall_handlers_with_undesired_annotations_present( - registry, handler_factory, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) handler_factory(reason=None, annotations={'someannotation': MetaFilterToken.ABSENT}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers @@ -303,8 +300,8 @@ def test_catchall_handlers_with_undesired_annotations_present( pytest.param({'otherannotation': 'othervalue'}, id='with-other-annotation'), ]) def test_catchall_handlers_with_undesired_annotations_absent( - registry, handler_factory, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) handler_factory(reason=None, annotations={'someannotation': MetaFilterToken.ABSENT}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -316,8 +313,8 @@ def test_catchall_handlers_with_undesired_annotations_absent( pytest.param({'someannotation': 'othervalue'}, id='with-other-value'), ]) def test_catchall_handlers_with_annotations_callback_says_true( - registry, handler_factory, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) handler_factory(reason=None, annotations={'someannotation': _always}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -329,8 +326,8 @@ def test_catchall_handlers_with_annotations_callback_says_true( pytest.param({'someannotation': 'othervalue'}, id='with-other-value'), ]) def test_catchall_handlers_with_annotations_callback_says_false( - registry, handler_factory, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) handler_factory(reason=None, annotations={'someannotation': _never}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers @@ -344,8 +341,8 @@ def test_catchall_handlers_with_annotations_callback_says_false( pytest.param({'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-extra-annotation'), ]) def test_catchall_handlers_without_annotations( - registry, handler_factory, resource, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, annotations): + cause = cause_factory(body={'metadata': {'annotations': annotations}}) handler_factory(reason=None) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -358,8 +355,8 @@ def test_catchall_handlers_without_annotations( pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, {'someannotation': 'somevalue', 'otherannotation': 'othervalue'}, id='with-extra-label-extra-annotation'), ]) def test_catchall_handlers_with_labels_and_annotations_satisfied( - registry, handler_factory, resource, labels, annotations): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels, 'annotations': annotations}}) + cause_factory, registry, handler_factory, resource, labels, annotations): + cause = cause_factory(body={'metadata': {'labels': labels, 'annotations': annotations}}) handler_factory(reason=None, labels={'somelabel': 'somevalue'}, annotations={'someannotation': 'somevalue'}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers @@ -373,8 +370,8 @@ def test_catchall_handlers_with_labels_and_annotations_satisfied( pytest.param({'somelabel': 'somevalue', 'otherlabel': 'othervalue'}, id='with-extra-label'), ]) def test_catchall_handlers_with_labels_and_annotations_not_satisfied( - registry, handler_factory, resource, labels): - cause = Mock(resource=resource, reason='some-reason', diff=None, body={'metadata': {'labels': labels}}) + cause_factory, registry, handler_factory, resource, labels): + cause = cause_factory(body={'metadata': {'labels': labels}}) handler_factory(reason=None, labels={'somelabel': 'somevalue'}, annotations={'someannotation': 'somevalue'}) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers @@ -387,17 +384,8 @@ def test_catchall_handlers_with_labels_and_annotations_not_satisfied( pytest.param(lambda **_: True, id='with-other-when'), ]) def test_catchall_handlers_with_when_callback_matching( - registry, handler_factory, resource, reason, when): - cause = ResourceChangingCause( - resource=resource, - reason='some-reason', - diff=None, - body=Body({'spec': {'name': 'test'}}), - logger=None, - patch=None, - memo=None, - initial=None - ) + cause_factory, registry, handler_factory, resource, reason, when): + cause = cause_factory(body={'spec': {'name': 'test'}}) handler_factory(reason=None, when=when) handlers = registry.resource_changing_handlers[resource].get_handlers(cause) assert handlers @@ -408,17 +396,8 @@ def test_catchall_handlers_with_when_callback_matching( pytest.param(lambda **_: False, id='with-other-when'), ]) def test_catchall_handlers_with_when_callback_mismatching( - registry, handler_factory, resource, when): - cause = ResourceChangingCause( - resource=resource, - reason='some-reason', - diff=None, - body=Body({'spec': {'name': 'test'}}), - logger=None, - patch=None, - memo=None, - initial=None - ) + cause_factory, registry, handler_factory, resource, when): + cause = cause_factory(body={'spec': {'name': 'test'}}) handler_factory(reason=None, when=when) handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert not handlers diff --git a/tests/registries/test_resumes_mixed_in.py b/tests/registries/test_resumes_mixed_in.py index d4c52844..38c47aa1 100644 --- a/tests/registries/test_resumes_mixed_in.py +++ b/tests/registries/test_resumes_mixed_in.py @@ -7,10 +7,13 @@ @pytest.mark.parametrize('deleted', [True, False, None]) @pytest.mark.parametrize('reason', HANDLER_REASONS) -def test_resumes_ignored_for_non_initial_causes(mocker, reason, deleted): +def test_resumes_ignored_for_non_initial_causes( + reason, deleted, cause_factory): + registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=False, deleted=deleted) + cause = cause_factory(resource=resource, reason=reason, initial=False, + body={'metadata': {'deletionTimestamp': '...'} if deleted else {}}) @kopf.on.resume('group', 'version', 'plural') def fn(**_): @@ -21,10 +24,12 @@ def fn(**_): @pytest.mark.parametrize('reason', list(set(HANDLER_REASONS) - {Reason.DELETE})) -def test_resumes_selected_for_initial_non_deletions(mocker, reason): +def test_resumes_selected_for_initial_non_deletions( + reason, cause_factory): + registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=False) + cause = cause_factory(resource=resource, reason=reason, initial=True) @kopf.on.resume('group', 'version', 'plural') def fn(**_): @@ -36,10 +41,13 @@ def fn(**_): @pytest.mark.parametrize('reason', [Reason.DELETE]) -def test_resumes_ignored_for_initial_deletions_by_default(mocker, reason): +def test_resumes_ignored_for_initial_deletions_by_default( + reason, cause_factory): + registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=True) + cause = cause_factory(resource=resource, reason=reason, initial=True, + body={'metadata': {'deletionTimestamp': '...'}}) @kopf.on.resume('group', 'version', 'plural') def fn(**_): @@ -50,10 +58,13 @@ def fn(**_): @pytest.mark.parametrize('reason', [Reason.DELETE]) -def test_resumes_selected_for_initial_deletions_when_explicitly_marked(mocker, reason): +def test_resumes_selected_for_initial_deletions_when_explicitly_marked( + reason, cause_factory): + registry = kopf.get_default_registry() resource = Resource('group', 'version', 'plural') - cause = mocker.MagicMock(resource=resource, reason=reason, initial=True, deleted=True) + cause = cause_factory(resource=resource, reason=reason, initial=True, + body={'metadata': {'deletionTimestamp': '...'}}) @kopf.on.resume('group', 'version', 'plural', deleted=True) def fn(**_): diff --git a/tests/registries/test_subhandlers_ids.py b/tests/registries/test_subhandlers_ids.py index 18990649..92a4c827 100644 --- a/tests/registries/test_subhandlers_ids.py +++ b/tests/registries/test_subhandlers_ids.py @@ -9,28 +9,30 @@ def child_fn(**_): def test_with_no_parent( - mocker, resource_registry_cls): + resource_registry_cls, cause_factory): + cause = cause_factory(resource_registry_cls) registry = resource_registry_cls() with context([(handler_var, None)]): kopf.on.this(registry=registry)(child_fn) - handlers = registry.get_handlers(mocker.MagicMock()) + handlers = registry.get_handlers(cause) assert len(handlers) == 1 assert handlers[0].fn is child_fn assert handlers[0].id == 'child_fn' def test_with_parent( - mocker, parent_handler, resource_registry_cls): + parent_handler, resource_registry_cls, cause_factory): + cause = cause_factory(resource_registry_cls) registry = resource_registry_cls() with context([(handler_var, parent_handler)]): kopf.on.this(registry=registry)(child_fn) - handlers = registry.get_handlers(mocker.MagicMock()) + handlers = registry.get_handlers(cause) assert len(handlers) == 1 assert handlers[0].fn is child_fn assert handlers[0].id == 'parent_fn/child_fn'