Use pytest-asyncio for client and sender tests

This commit is contained in:
Lonami Exo 2023-09-01 14:02:24 +02:00
parent 294f7dedd5
commit f69e309b0b
5 changed files with 61 additions and 59 deletions

View File

@ -27,6 +27,10 @@ dynamic = ["version"]
[project.optional-dependencies]
cryptg = ["cryptg~=0.4"]
dev = [
"pytest~=7.3",
"pytest-asyncio~=0.21",
]
[project.urls]
"Homepage" = "https://telethon.dev/"

View File

@ -1,4 +1,3 @@
import asyncio
import os
import random
@ -8,30 +7,27 @@ from telethon._impl.session.message_box.defs import Session
from telethon._impl.tl.mtproto import functions, types
def test_ping_pong() -> None:
async def func() -> None:
api_id = os.getenv("TG_ID")
api_hash = os.getenv("TG_HASH")
assert api_id and api_id.isdigit()
assert api_hash
client = Client(
Config(
session=Session(
dcs=[],
user=None,
state=None,
),
api_id=int(api_id),
api_hash=api_hash,
)
async def test_ping_pong() -> None:
api_id = os.getenv("TG_ID")
api_hash = os.getenv("TG_HASH")
assert api_id and api_id.isdigit()
assert api_hash
client = Client(
Config(
session=Session(
dcs=[],
user=None,
state=None,
),
api_id=int(api_id),
api_hash=api_hash,
)
assert not client.connected
await client.connect()
assert client.connected
)
assert not client.connected
await client.connect()
assert client.connected
ping_id = random.randrange(-(2**63), 2**63)
pong = await client(functions.ping(ping_id=ping_id))
assert isinstance(pong, types.Pong)
assert pong.ping_id == ping_id
asyncio.run(func())
ping_id = random.randrange(-(2**63), 2**63)
pong = await client(functions.ping(ping_id=ping_id))
assert isinstance(pong, types.Pong)
assert pong.ping_id == ping_id

View File

@ -13,42 +13,39 @@ TELEGRAM_DEFAULT_TEST_DC = TELEGRAM_TEST_DC_2
TEST_TIMEOUT = 10000
def test_invoke_encrypted_method(caplog: LogCaptureFixture) -> None:
async def test_invoke_encrypted_method(caplog: LogCaptureFixture) -> None:
caplog.set_level(logging.DEBUG)
async def func() -> None:
deadline = asyncio.get_running_loop().time() + TEST_TIMEOUT
deadline = asyncio.get_running_loop().time() + TEST_TIMEOUT
def timeout() -> float:
return deadline - asyncio.get_running_loop().time()
def timeout() -> float:
return deadline - asyncio.get_running_loop().time()
sender = await asyncio.wait_for(
connect(Full(), TELEGRAM_DEFAULT_TEST_DC), timeout()
sender = await asyncio.wait_for(
connect(Full(), TELEGRAM_DEFAULT_TEST_DC), timeout()
)
rx = sender.enqueue(
functions.invoke_with_layer(
layer=LAYER,
query=functions.init_connection(
api_id=1,
device_model="Test",
system_version="0.1",
app_version="0.1",
system_lang_code="en",
lang_pack="",
lang_code="",
proxy=None,
params=None,
query=functions.help.get_nearest_dc(),
),
)
)
rx = sender.enqueue(
functions.invoke_with_layer(
layer=LAYER,
query=functions.init_connection(
api_id=1,
device_model="Test",
system_version="0.1",
app_version="0.1",
system_lang_code="en",
lang_pack="",
lang_code="",
proxy=None,
params=None,
query=functions.help.get_nearest_dc(),
),
)
)
while True:
await asyncio.wait_for(sender.step(), timeout=timeout())
if rx.done():
nearest = abcs.NearestDc.from_bytes(rx.result())
assert isinstance(nearest, types.NearestDc)
break
asyncio.run(func())
while True:
await asyncio.wait_for(sender.step(), timeout=timeout())
if rx.done():
nearest = abcs.NearestDc.from_bytes(rx.result())
assert isinstance(nearest, types.NearestDc)
break

View File

@ -21,6 +21,9 @@ classifiers = [
]
dynamic = ["version"]
[project.optional-dependencies]
dev = ["pytest~=7.3"]
[project.urls]
"Homepage" = "https://telethon.dev/"
"Source" = "https://telethon.dev/code/"

2
pytest.ini Normal file
View File

@ -0,0 +1,2 @@
[pytest]
asyncio_mode = auto