From f69e309b0be5ccdf1e67174531d9784c7e428397 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 1 Sep 2023 14:02:24 +0200 Subject: [PATCH] Use pytest-asyncio for client and sender tests --- client/pyproject.toml | 4 +++ client/tests/client_test.py | 48 ++++++++++++-------------- client/tests/mtsender_test.py | 63 +++++++++++++++++------------------ generator/pyproject.toml | 3 ++ pytest.ini | 2 ++ 5 files changed, 61 insertions(+), 59 deletions(-) create mode 100644 pytest.ini diff --git a/client/pyproject.toml b/client/pyproject.toml index bd940936..4e26967a 100644 --- a/client/pyproject.toml +++ b/client/pyproject.toml @@ -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/" diff --git a/client/tests/client_test.py b/client/tests/client_test.py index ea5b9c4a..82ddc4ba 100644 --- a/client/tests/client_test.py +++ b/client/tests/client_test.py @@ -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 diff --git a/client/tests/mtsender_test.py b/client/tests/mtsender_test.py index a666486b..0ece35a8 100644 --- a/client/tests/mtsender_test.py +++ b/client/tests/mtsender_test.py @@ -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 diff --git a/generator/pyproject.toml b/generator/pyproject.toml index 86f88f44..9a8f4ec7 100644 --- a/generator/pyproject.toml +++ b/generator/pyproject.toml @@ -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/" diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..2f4c80e3 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +asyncio_mode = auto