Skip to content

Commit 4ddf19c

Browse files
committed
[tests] test overrides, signatures and return types
1 parent 063e0c5 commit 4ddf19c

File tree

2 files changed

+97
-12
lines changed

2 files changed

+97
-12
lines changed

pywa_async/types/sent_message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dataclasses
1+
from __future__ import annotations
22

33
from pywa.types.sent_message import * # noqa MUST BE IMPORTED FIRST
44
from pywa.types.sent_message import SentMessage as _SentMessage

tests/test_async.py

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
11
import asyncio
2-
2+
import inspect
33
from pywa import WhatsApp as WhatsAppSync
44
from pywa_async import WhatsApp as WhatsAppAsync
5+
from pywa.api import WhatsAppCloudApi as WhatsAppCloudApiSync
6+
from pywa_async.api import WhatsAppCloudApiAsync
7+
from pywa.types import (
8+
Message as MessageSync,
9+
CallbackButton as CallbackButtonSync,
10+
CallbackSelection as CallbackSelectionSync,
11+
MessageStatus as MessageStatusSync,
12+
ChatOpened as ChatOpenedSync,
13+
FlowCompletion as FlowCompletionSync,
14+
TemplateStatus as TemplateStatusSync,
15+
FlowRequest as FlowRequestSync,
16+
MediaUrlResponse as MediaUrlResponseSync,
17+
)
18+
from pywa_async.types import (
19+
Message as MessageAsync,
20+
CallbackButton as CallbackButtonAsync,
21+
CallbackSelection as CallbackSelectionAsync,
22+
MessageStatus as MessageStatusAsync,
23+
ChatOpened as ChatOpenedAsync,
24+
FlowCompletion as FlowCompletionAsync,
25+
TemplateStatus as TemplateStatusAsync,
26+
FlowRequest as FlowRequestAsync,
27+
MediaUrlResponse as MediaUrlResponseAsync,
28+
)
29+
from pywa.types.flows import FlowDetails as FlowDetailsSync
30+
from pywa_async.types.flows import FlowDetails as FlowDetailsAsync
31+
from pywa.types.media import BaseMedia as BaseMediaSync
32+
from pywa_async.types.media import BaseMediaAsync
33+
from pywa.types.sent_message import SentMessage as SentMessageSync
34+
from pywa_async.types.sent_message import SentMessage as SentMessageAsync
535

636

7-
def test_all_methods_overwritten_in_async():
37+
def test_all_methods_are_overwritten_in_async():
38+
objs: list[tuple] = [
39+
(WhatsAppSync, WhatsAppAsync),
40+
(MessageSync, MessageAsync),
41+
(CallbackButtonSync, CallbackButtonAsync),
42+
(CallbackSelectionSync, CallbackSelectionAsync),
43+
(MessageStatusSync, MessageStatusAsync),
44+
(ChatOpenedSync, ChatOpenedAsync),
45+
(FlowCompletionSync, FlowCompletionAsync),
46+
(TemplateStatusSync, TemplateStatusAsync),
47+
(FlowRequestSync, FlowRequestAsync),
48+
(FlowDetailsSync, FlowDetailsAsync),
49+
(MediaUrlResponseSync, MediaUrlResponseAsync),
50+
(BaseMediaSync, BaseMediaAsync),
51+
(SentMessageSync, SentMessageAsync),
52+
(WhatsAppCloudApiSync, WhatsAppCloudApiAsync),
53+
]
854
skip_methods = {
9-
"api",
10-
"token",
1155
"add_handlers",
1256
"remove_handlers",
1357
"remove_callbacks",
@@ -22,14 +66,12 @@ def test_all_methods_overwritten_in_async():
2266
"on_raw_update",
2367
"on_flow_request",
2468
"stop_listening",
25-
"_async_allowed",
2669
"_check_for_async_callback",
2770
"_after_handling_update",
2871
"_check_for_async_filters",
2972
"_check_and_prepare_update",
3073
"_check_for_async_func",
3174
"_get_handler",
32-
"_handlers_to_update_constractor",
3375
"_api_cls",
3476
"_flow_req_cls",
3577
"_httpx_client",
@@ -42,9 +84,52 @@ def test_all_methods_overwritten_in_async():
4284
"_remove_listener",
4385
"get_flow_request_handler",
4486
"load_handlers_modules",
87+
"continue_handling",
88+
"stop_handling",
89+
"from_update",
90+
"from_dict",
91+
"from_flow_completion",
92+
"from_sent_update",
93+
"get_media",
94+
"respond",
95+
"TemplateEvent",
96+
"TemplateRejectionReason",
97+
}
98+
skip_signature_check = {
99+
"upload_media",
100+
"webhook_challenge_handler",
101+
"webhook_update_handler",
102+
"decrypt_media",
45103
}
46-
for method in dir(WhatsAppSync):
47-
if method.startswith("__") or method in skip_methods:
48-
continue
49-
if not asyncio.iscoroutinefunction(getattr(WhatsAppAsync, method)):
50-
raise AssertionError(f"Method {method} is not overwritten in WhatsAppAsync")
104+
for sync_obj, async_obj in objs:
105+
for method_name in filter(
106+
lambda a: callable(getattr(sync_obj, a))
107+
and not a.startswith("__")
108+
and a not in skip_methods,
109+
dir(sync_obj),
110+
):
111+
sync_method, async_method = (
112+
getattr(sync_obj, method_name),
113+
getattr(async_obj, method_name),
114+
)
115+
if not asyncio.iscoroutinefunction(async_method):
116+
raise AssertionError(
117+
f"Method {method_name} is not overwritten in {async_obj.__name__}"
118+
)
119+
if method_name not in skip_signature_check:
120+
sync_sig, async_sig = (
121+
inspect.signature(sync_method),
122+
inspect.signature(async_method),
123+
)
124+
if sync_sig.parameters != async_sig.parameters:
125+
raise AssertionError(
126+
f"Method {method_name} has different signatures in {async_obj.__name__}\n"
127+
f"S: {sync_sig.parameters}\n"
128+
f"A: {async_sig.parameters}"
129+
)
130+
if sync_sig.return_annotation != async_sig.return_annotation:
131+
raise AssertionError(
132+
f"Method {method_name} has different return annotations in {async_obj.__name__}\n"
133+
f"S: {sync_sig.return_annotation}\n"
134+
f"A: {async_sig.return_annotation}"
135+
)

0 commit comments

Comments
 (0)