def test_item_has_all_expected_properties(operation): item = DiffItem(operation, ('field',), 'a', 'b') assert item.operation is operation assert item.op is operation assert item.field == ('field',) assert item.old == 'a' assert item.new == 'b'
def test_field_longer_than_diff_for_right_field(cause_with_diff, registry, resource, old, new): @kopf.on.field(resource.group, resource.version, resource.plural, registry=registry, field='level1.level2.level3') def some_fn(**_): ... cause = cause_with_diff cause.reason = Reason.UPDATE cause.diff = [DiffItem(DiffOperation.CHANGE, ('level1', 'level2'), old, new)] handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers
def test_field_shorter_than_diff(cause_with_diff, registry, resource): @kopf.on.field(resource.group, resource.version, resource.plural, registry=registry, field='level1') def some_fn(**_): ... cause = cause_with_diff cause.reason = Reason.UPDATE cause.diff = [DiffItem(DiffOperation.CHANGE, ('level1', 'level2'), 'old', 'new')] handlers = registry.resource_changing_handlers[cause.resource].get_handlers(cause) assert handlers
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.")
def test_diff_comparison_to_the_same(): d1 = Diff([ DiffItem(DiffOperation.ADD , ('key1',), None, 'new1'), DiffItem(DiffOperation.CHANGE, ('key2',), 'old2', 'new2'), DiffItem(DiffOperation.REMOVE, ('key3',), 'old3', None), ]) d2 = Diff([ DiffItem(DiffOperation.ADD , ('key1',), None, 'new1'), DiffItem(DiffOperation.CHANGE, ('key2',), 'old2', 'new2'), DiffItem(DiffOperation.REMOVE, ('key3',), 'old3', None), ]) assert d1 == d2
import pytest from kopf.structs.diffs import reduce, Diff, DiffItem, DiffOperation DIFF = Diff([ DiffItem(DiffOperation.ADD, ('key1', ), None, 'new1'), DiffItem(DiffOperation.CHANGE, ('key2', ), 'old2', 'new2'), DiffItem(DiffOperation.ADD, ('key2', 'suba'), 'olda', 'newa'), DiffItem(DiffOperation.REMOVE, ('key2', 'subb'), 'oldb', 'newb'), DiffItem(DiffOperation.REMOVE, ('key3', ), 'old3', None), DiffItem(DiffOperation.CHANGE, ('key4', ), { 'suba': 'olda', 'subc': 'oldc' }, { 'subb': 'newb', 'subc': 'newc' }), ]) @pytest.mark.parametrize('diff', [ [['op', ['key', 'sub'], 'old', 'new']], [['op', ('key', 'sub'), 'old', 'new']], [('op', ['key', 'sub'], 'old', 'new')], [('op', ('key', 'sub'), 'old', 'new')], (['op', ['key', 'sub'], 'old', 'new'], ), (['op', ('key', 'sub'), 'old', 'new'], ), (('op', ['key', 'sub'], 'old', 'new'), ), (('op', ('key', 'sub'), 'old', 'new'), ), ], ids=[
def test_item_comparison_to_another_item(operation): item1 = DiffItem(operation.value, (), 'a', 'b') item2 = DiffItem(operation.value, (), 'a', 'b') assert item1 == item2
def test_item_comparison_to_list(operation): item = DiffItem(operation.value, (), 'a', 'b') assert item == [operation.value, (), 'a', 'b']
def test_item_comparison_to_tuple(operation): item = DiffItem(operation.value, (), 'a', 'b') assert item == (operation.value, (), 'a', 'b')
def make_cause( cls=ResourceChangingCause, *, resource=resource, type=None, raw=None, body=None, diff=(), old=None, new=None, reason='some-reason', initial=False, activity=None, settings=None, ): if cls is ActivityCause or cls is ActivityRegistry: return ActivityCause( memo=Memo(), logger=logging.getLogger('kopf.test.fake.logger'), indices=OperatorIndexers().indices, activity=activity, settings=settings, ) if cls is ResourceCause or cls is ResourceRegistry: return ResourceCause( logger=logging.getLogger('kopf.test.fake.logger'), indices=OperatorIndexers().indices, 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'), indices=OperatorIndexers().indices, 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'), indices=OperatorIndexers().indices, resource=resource, patch=Patch(), memo=Memo(), body=Body(body if body is not None else {}), diff=Diff(DiffItem(*d) for d in diff), old=old, new=new, initial=initial, reason=reason, ) if cls is ResourceSpawningCause or cls is ResourceSpawningRegistry: return ResourceSpawningCause( logger=logging.getLogger('kopf.test.fake.logger'), indices=OperatorIndexers().indices, resource=resource, patch=Patch(), memo=Memo(), body=Body(body if body is not None else {}), reset=False, ) if cls is ResourceWebhookCause or cls is ResourceWebhooksRegistry: return ResourceWebhookCause( logger=logging.getLogger('kopf.test.fake.logger'), indices=OperatorIndexers().indices, resource=resource, patch=Patch(), memo=Memo(), body=Body(body if body is not None else {}), dryrun=False, sslpeer={}, headers={}, userinfo={}, warnings=[], reason=None, webhook=None, operation=None, ) raise TypeError( f"Cause/registry type {cls} is not supported by this fixture.")