Skip to content

Commit 79c5793

Browse files
Merge pull request #519 from Backblaze/fix_event_notifications_tests
Fix event notification tests
2 parents c6f291f + c23df68 commit 79c5793

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix event notification tests when introducing new keys in API outputs.

test/helpers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,22 @@ def type_validator_factory(type_):
5757
if pydantic:
5858
return pydantic.TypeAdapter(type_).validate_python
5959
return lambda *args, **kwargs: None
60+
61+
62+
def deep_cast_dict(actual, expected):
63+
"""
64+
For composite objects `actual` and `expected`, return a copy of `actual` (with all dicts and lists deeply copied)
65+
with all keys of dicts not appearing in `expected` (comparing dicts on any level) removed. Useful for assertions
66+
in tests ignoring extra keys.
67+
"""
68+
if isinstance(expected, dict) and isinstance(actual, dict):
69+
return {k: deep_cast_dict(actual[k], expected[k]) for k in expected if k in actual}
70+
71+
elif isinstance(expected, list) and isinstance(actual, list):
72+
return [deep_cast_dict(a, e) for a, e in zip(actual, expected)]
73+
74+
return actual
75+
76+
77+
def assert_dict_equal_ignore_extra(actual, expected):
78+
assert deep_cast_dict(actual, expected) == expected

test/integration/test_bucket.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# License https://www.backblaze.com/using_b2_code.html
88
#
99
######################################################################
10+
from test.helpers import assert_dict_equal_ignore_extra
11+
1012
import pytest
1113

1214

@@ -33,12 +35,13 @@ def test_bucket_notification_rules(bucket, b2_api):
3335

3436
set_notification_rules = bucket.set_notification_rules([notification_rule])
3537
assert set_notification_rules == bucket.get_notification_rules()
36-
assert set_notification_rules == [
37-
{
38+
assert_dict_equal_ignore_extra(
39+
set_notification_rules,
40+
[{
3841
**notification_rule, "isSuspended": False,
3942
"suspensionReason": ""
40-
}
41-
]
43+
}],
44+
)
4245
assert bucket.set_notification_rules([]) == []
4346

4447

test/integration/test_raw_api.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import sys
1717
import time
1818
import traceback
19-
from test.helpers import type_validator_factory
19+
from test.helpers import assert_dict_equal_ignore_extra, type_validator_factory
2020
from typing import List
2121

2222
import pytest
@@ -634,7 +634,9 @@ def _subtest_bucket_notification_rules(raw_api, auth_dict, api_url, account_auth
634634
}
635635
}
636636
]
637-
assert notification_rules_response_list == expected_notification_rule_response_list
637+
assert_dict_equal_ignore_extra(
638+
notification_rules_response_list, expected_notification_rule_response_list
639+
)
638640

639641
assert raw_api.set_bucket_notification_rules(api_url, account_auth_token, bucket_id, []) == []
640642
assert raw_api.get_bucket_notification_rules(api_url, account_auth_token, bucket_id) == []

test/unit/bucket/test_bucket.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import unittest.mock as mock
2222
from contextlib import suppress
2323
from io import BytesIO
24-
from test.helpers import NonSeekableIO
24+
from test.helpers import NonSeekableIO, assert_dict_equal_ignore_extra
2525

2626
import apiver_deps
2727
import pytest
@@ -3329,22 +3329,24 @@ def test_bucket_notification_rules(bucket, b2api_simulator):
33293329

33303330
set_notification_rules = bucket.set_notification_rules([notification_rule])
33313331
assert set_notification_rules == bucket.get_notification_rules()
3332-
assert set_notification_rules == [
3333-
{
3332+
assert_dict_equal_ignore_extra(
3333+
set_notification_rules,
3334+
[{
33343335
**notification_rule, "isSuspended": False,
33353336
"suspensionReason": ""
3336-
}
3337-
]
3337+
}],
3338+
)
33383339

33393340
b2api_simulator.bucket_id_to_bucket[bucket.id_].simulate_notification_rule_suspension(
33403341
notification_rule["name"], "simulated suspension"
33413342
)
3342-
assert bucket.get_notification_rules() == [
3343-
{
3343+
assert_dict_equal_ignore_extra(
3344+
bucket.get_notification_rules(),
3345+
[{
33443346
**notification_rule, "isSuspended": True,
33453347
"suspensionReason": "simulated suspension"
3346-
}
3347-
]
3348+
}],
3349+
)
33483350

33493351
assert bucket.set_notification_rules([]) == []
33503352
assert bucket.get_notification_rules() == []

0 commit comments

Comments
 (0)