Skip to content

Commit 1831f6a

Browse files
committed
Add unit tests for the async function, change deprecate message and address other review comments
1 parent e808deb commit 1831f6a

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

firebase_admin/_messaging_encoder.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ class Message:
3939
apns: An instance of ``messaging.ApnsConfig`` (optional).
4040
fcm_options: An instance of ``messaging.FCMOptions`` (optional).
4141
fid: The Firebase installation ID of an FCM registered app instance to which the
42-
message should be sent (optional)
42+
message should be sent (optional).
4343
token: Deprecated. Use ``fid`` instead.
4444
topic: Name of the FCM topic to which the message should be sent (optional). Topic name
4545
may contain the ``/topics/`` prefix.
4646
condition: The FCM condition to which the message should be sent (optional).
4747
"""
4848

4949
def __init__(self, data=None, notification=None, android=None, webpush=None, apns=None,
50-
fcm_options=None, fid=None, token=None, topic=None, condition=None):
50+
fcm_options=None, token=None, topic=None, condition=None, fid=None):
5151
if token is not None:
5252
warnings.warn(
53-
"Deprecated. Use 'fid' instead.",
53+
"Message.token is deprecated. Use fid instead.",
5454
DeprecationWarning,
5555
stacklevel=2
5656
)
@@ -73,7 +73,7 @@ class MulticastMessage:
7373
"""A message that can be sent to multiple tokens or fids via Firebase Cloud Messaging.
7474
7575
Args:
76-
fids: A list of Firebase Installation IDs of targeted app instances (optional)
76+
fids: A list of Firebase Installation IDs of targeted app instances (optional).
7777
tokens: Deprecated. Use ``fids`` instead (optional).
7878
data: A dictionary of data fields (optional). All keys and values in the dictionary must be
7979
strings.
@@ -88,7 +88,7 @@ def __init__(
8888
webpush=None, apns=None, fcm_options=None):
8989
if tokens is not None:
9090
warnings.warn(
91-
"Deprecated. Use 'fids' instead.",
91+
"MulticastMessage.tokens is deprecated. Use fids instead.",
9292
DeprecationWarning,
9393
stacklevel=2
9494
)

tests/test_messaging.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,13 @@ def test_fids_type(self):
157157
assert len(message.fids) == 500
158158

159159
def test_tokens_deprecation_warning(self):
160-
with pytest.deprecated_call():
160+
msg = 'MulticastMessage.tokens is deprecated. Use fids instead.'
161+
with pytest.warns(DeprecationWarning, match=msg):
161162
messaging.MulticastMessage(tokens=['token'])
162163

163164
def test_tokens_deprecation_warning_positional(self):
164-
with pytest.deprecated_call():
165+
msg = 'MulticastMessage.tokens is deprecated. Use fids instead.'
166+
with pytest.warns(DeprecationWarning, match=msg):
165167
messaging.MulticastMessage(['token'])
166168

167169

@@ -220,7 +222,8 @@ def test_empty_message(self):
220222
check_encoding(messaging.Message(condition='value'), {'condition': 'value'})
221223

222224
def test_token_deprecation_warning(self):
223-
with pytest.deprecated_call():
225+
msg = 'Message.token is deprecated. Use fid instead.'
226+
with pytest.warns(DeprecationWarning, match=msg):
224227
messaging.Message(token='value')
225228

226229
@pytest.mark.parametrize('data', NON_DICT_ARGS)
@@ -2287,6 +2290,48 @@ def test_send_each_for_multicast_fids(self):
22872290
assert all(r.success for r in batch_response.responses)
22882291
assert not any(r.exception for r in batch_response.responses)
22892292

2293+
@respx.mock
2294+
@pytest.mark.asyncio
2295+
async def test_send_each_for_multicast_async(self):
2296+
responses = [
2297+
respx.MockResponse(200, http_version='HTTP/2', json={'name': 'message-id1'}),
2298+
respx.MockResponse(200, http_version='HTTP/2', json={'name': 'message-id2'}),
2299+
]
2300+
msg = messaging.MulticastMessage(tokens=['foo1', 'foo2'])
2301+
route = respx.request(
2302+
method='POST',
2303+
url='https://fcm.googleapis.com/v1/projects/explicit-project-id/messages:send'
2304+
)
2305+
route.side_effect = responses
2306+
batch_response = await messaging.send_each_for_multicast_async(msg, dry_run=True)
2307+
assert batch_response.success_count == 2
2308+
assert batch_response.failure_count == 0
2309+
assert len(batch_response.responses) == 2
2310+
assert [r.message_id for r in batch_response.responses] == ['message-id1', 'message-id2']
2311+
assert all(r.success for r in batch_response.responses)
2312+
assert not any(r.exception for r in batch_response.responses)
2313+
2314+
@respx.mock
2315+
@pytest.mark.asyncio
2316+
async def test_send_each_for_multicast_async_fids(self):
2317+
responses = [
2318+
respx.MockResponse(200, http_version='HTTP/2', json={'name': 'message-id1'}),
2319+
respx.MockResponse(200, http_version='HTTP/2', json={'name': 'message-id2'}),
2320+
]
2321+
msg = messaging.MulticastMessage(fids=['foo1', 'foo2'])
2322+
route = respx.request(
2323+
method='POST',
2324+
url='https://fcm.googleapis.com/v1/projects/explicit-project-id/messages:send'
2325+
)
2326+
route.side_effect = responses
2327+
batch_response = await messaging.send_each_for_multicast_async(msg, dry_run=True)
2328+
assert batch_response.success_count == 2
2329+
assert batch_response.failure_count == 0
2330+
assert len(batch_response.responses) == 2
2331+
assert [r.message_id for r in batch_response.responses] == ['message-id1', 'message-id2']
2332+
assert all(r.success for r in batch_response.responses)
2333+
assert not any(r.exception for r in batch_response.responses)
2334+
22902335
@pytest.mark.parametrize('status', HTTP_ERROR_CODES)
22912336
def test_send_each_for_multicast_detailed_error(self, status):
22922337
success_payload = json.dumps({'name': 'message-id'})

0 commit comments

Comments
 (0)