Skip to content

Commit

Permalink
Fix event notification tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpnowacki-reef committed Oct 28, 2024
1 parent f968d7b commit 456b8f9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/+ignore_new_keys_in_tests.infrastructure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix event notification tests when introducing new keys in API outputs.
19 changes: 19 additions & 0 deletions test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,22 @@ def b2_uri_args_v4(bucket_name, path=_MISSING):
if path is _MISSING:
path = ''
return [f'b2://{bucket_name}/{path}']


def deep_cast_dict(actual, expected):
"""
For composite objects `actual` and `expected`, return a copy of `actual` (with all dicts and lists deeply copied)
with all keys of dicts not appearing in `expected` (comparing dicts on any level) removed. Useful for assertions
in tests ignoring extra keys.
"""
if isinstance(expected, dict) and isinstance(actual, dict):
return {k: deep_cast_dict(actual[k], expected[k]) for k in expected if k in actual}

elif isinstance(expected, list) and isinstance(actual, list):
return [deep_cast_dict(a, e) for a, e in zip(actual, expected)]

return actual


def assert_dict_equal_ignore_extra(actual, expected):
assert deep_cast_dict(actual, expected) == expected
17 changes: 10 additions & 7 deletions test/integration/test_b2_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
)
from b2._internal.console_tool import current_time_millis

from ..helpers import skip_on_windows
from ..helpers import assert_dict_equal_ignore_extra, skip_on_windows
from .helpers import (
ONE_DAY_MILLIS,
ONE_HOUR_MILLIS,
Expand Down Expand Up @@ -3465,7 +3465,7 @@ def test_notification_rules(b2_tool, bucket_name):
expected_stderr_pattern=private_preview_pattern
)
expected_rules = [{**notification_rule, "isSuspended": False, "suspensionReason": ""}]
assert created_rule == expected_rules[0]
assert_dict_equal_ignore_extra(created_rule, expected_rules[0])

# modify rule
secret = "0testSecret000000000000000000032"
Expand All @@ -3485,13 +3485,16 @@ def test_notification_rules(b2_tool, bucket_name):
)
expected_rules[0].update({"objectNamePrefix": "prefix", "isEnabled": False})
expected_rules[0]["targetConfiguration"]["hmacSha256SigningSecret"] = secret
assert modified_rule == expected_rules[0]
assert_dict_equal_ignore_extra(modified_rule, expected_rules[0])

# read updated rules
assert b2_tool.should_succeed_json(
["bucket", "notification-rule", "list", f"b2://{bucket_name}", "--json"],
expected_stderr_pattern=private_preview_pattern
) == expected_rules
modified_rule(
b2_tool.should_succeed_json(
["bucket", "notification-rule", "list", f"b2://{bucket_name}", "--json"],
expected_stderr_pattern=private_preview_pattern
),
expected_rules,
)

# delete rule by name
assert b2_tool.should_succeed(
Expand Down

0 comments on commit 456b8f9

Please sign in to comment.