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] [project.optional-dependencies]
cryptg = ["cryptg~=0.4"] cryptg = ["cryptg~=0.4"]
dev = [
"pytest~=7.3",
"pytest-asyncio~=0.21",
]
[project.urls] [project.urls]
"Homepage" = "https://telethon.dev/" "Homepage" = "https://telethon.dev/"

View File

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

View File

@ -13,42 +13,39 @@ TELEGRAM_DEFAULT_TEST_DC = TELEGRAM_TEST_DC_2
TEST_TIMEOUT = 10000 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) 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: def timeout() -> float:
return deadline - asyncio.get_running_loop().time() return deadline - asyncio.get_running_loop().time()
sender = await asyncio.wait_for( sender = await asyncio.wait_for(
connect(Full(), TELEGRAM_DEFAULT_TEST_DC), timeout() 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( while True:
functions.invoke_with_layer( await asyncio.wait_for(sender.step(), timeout=timeout())
layer=LAYER, if rx.done():
query=functions.init_connection( nearest = abcs.NearestDc.from_bytes(rx.result())
api_id=1, assert isinstance(nearest, types.NearestDc)
device_model="Test", break
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())

View File

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

2
pytest.ini Normal file
View File

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