From a4876c1ac59f63db35cfb9a6ab4c92ac59745c9d Mon Sep 17 00:00:00 2001 From: NotAFile Date: Sat, 7 Dec 2019 03:43:04 +0100 Subject: [PATCH] Add example unit test and config for testing Add testing configuration: - pytest as test framework - tox for creating testing environments and running tests (run with `tox`) - pytest-asycio for asyncio testing - coverage/pytest-cov for measuring test coverage - flake8 for pep8 checking I've also added one quick example test demonstrating basic unit testing and use of the basic fixtures and marks provided by pytest-asyncio. Just this already covers a suprising 32% of the codebase, mostly through imports, but I wouldn't expect it to be helpful yet. This should provide a good base to build on in the future though. --- .coveragerc | 8 +++++ dev-requirements.txt | 3 ++ optional-requirements.txt | 2 +- tests/__init__.py | 0 tests/telethon/__init__.py | 0 tests/telethon/test_helpers.py | 59 ++++++++++++++++++++++++++++++++++ tox.ini | 22 +++++++++++++ 7 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 .coveragerc create mode 100644 dev-requirements.txt create mode 100644 tests/__init__.py create mode 100644 tests/telethon/__init__.py create mode 100644 tests/telethon/test_helpers.py create mode 100644 tox.ini diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 00000000..1b2271b9 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,8 @@ +[run] +branch = true +parallel = true +source = + telethon + +[report] +precision = 2 diff --git a/dev-requirements.txt b/dev-requirements.txt new file mode 100644 index 00000000..e01a8206 --- /dev/null +++ b/dev-requirements.txt @@ -0,0 +1,3 @@ +pytest +pytest-cov +pytest-asyncio diff --git a/optional-requirements.txt b/optional-requirements.txt index aeaf3994..00cd3324 100644 --- a/optional-requirements.txt +++ b/optional-requirements.txt @@ -1,4 +1,4 @@ cryptg pysocks -hachoir3 +hachoir pillow diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/telethon/__init__.py b/tests/telethon/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/telethon/test_helpers.py b/tests/telethon/test_helpers.py new file mode 100644 index 00000000..689db8af --- /dev/null +++ b/tests/telethon/test_helpers.py @@ -0,0 +1,59 @@ +""" +tests for telethon.helpers +""" + +from base64 import b64decode + +import pytest + +from telethon import helpers + + +def test_strip_text(): + assert helpers.strip_text(" text ", []) == "text" + # I can't interpret the rest of the code well enough yet + + +class TestSyncifyAsyncContext: + class NoopContextManager: + def __init__(self, loop): + self.count = 0 + self.loop = loop + + async def __aenter__(self): + self.count += 1 + return self + + async def __aexit__(self, exc_type, *args): + assert exc_type is None + self.count -= 1 + + __enter__ = helpers._sync_enter + __exit__ = helpers._sync_exit + + def test_sync_acontext(self, event_loop): + contm = self.NoopContextManager(event_loop) + assert contm.count == 0 + + with contm: + assert contm.count == 1 + + assert contm.count == 0 + + @pytest.mark.asyncio + async def test_async_acontext(self, event_loop): + contm = self.NoopContextManager(event_loop) + assert contm.count == 0 + + async with contm: + assert contm.count == 1 + + assert contm.count == 0 + + +def test_generate_key_data_from_nonce(): + gkdfn = helpers.generate_key_data_from_nonce + + key_expect = b64decode(b'NFwRFB8Knw/kAmvPWjtrQauWysHClVfQh0UOAaABqZA=') + nonce_expect = b64decode(b'1AgjhU9eDvJRjFik73bjR2zZEATzL/jLu9yodYfWEgA=') + assert gkdfn(123456789, 1234567) == (key_expect, nonce_expect) diff --git a/tox.ini b/tox.ini new file mode 100644 index 00000000..b2ad75eb --- /dev/null +++ b/tox.ini @@ -0,0 +1,22 @@ +[tox] +envlist = py35,py36,py37,py38 + +[testenv] +deps = + -rrequirements.txt + -roptional-requirements.txt + -rdev-requirements.txt +commands = + # NOTE: you can run any command line tool here - not just tests + pytest {posargs} + +# run with tox -e flake +[testenv:flake] +deps = + -rrequirements.txt + -roptional-requirements.txt + -rdev-requirements.txt + flake8 +commands = +# TODO: move options to dedicated flake8 config + flake8 --exclude telethon/tl/,telethon/errors/rpcerrorlist.py --ignore E501,F401 telethon/ tests/