From faccde41541505e4c0d88bd8b99e333d9c3f3b24 Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 18 Jul 2017 14:25:05 -0700 Subject: [PATCH 1/6] Refactor tests, move to pytest, etc --- .gitignore | 1 + setup.py | 1 + tests/api/__init__.py | 0 tests/{test_reason.py => api/reason.py} | 0 tests/bot/__init__.py | 0 tests/{test_bot.py => bot/bot.py} | 0 tests/gateway/__init__.py | 0 tests/gateway/events.py | 20 ++++++++++++++++ tests/{test_imports.py => imports.py} | 0 tests/types/__init__.py | 0 tests/{test_channel.py => types/channel.py} | 6 ++++- tests/{test_embeds.py => types/embeds.py} | 0 tests/{test_types.py => types/types.py} | 0 tests/{test_user.py => types/user.py} | 0 tests/util/__init__.py | 0 .../{test_util_limiter.py => util/limiter.py} | 0 tests/utils.py | 23 ------------------- tests/voice/__init__.py | 0 .../{tests_voice_queue.py => voice/queue.py} | 0 19 files changed, 27 insertions(+), 24 deletions(-) create mode 100644 tests/api/__init__.py rename tests/{test_reason.py => api/reason.py} (100%) create mode 100644 tests/bot/__init__.py rename tests/{test_bot.py => bot/bot.py} (100%) create mode 100644 tests/gateway/__init__.py create mode 100644 tests/gateway/events.py rename tests/{test_imports.py => imports.py} (100%) create mode 100644 tests/types/__init__.py rename tests/{test_channel.py => types/channel.py} (78%) rename tests/{test_embeds.py => types/embeds.py} (100%) rename tests/{test_types.py => types/types.py} (100%) rename tests/{test_user.py => types/user.py} (100%) create mode 100644 tests/util/__init__.py rename tests/{test_util_limiter.py => util/limiter.py} (100%) create mode 100644 tests/voice/__init__.py rename tests/{tests_voice_queue.py => voice/queue.py} (100%) diff --git a/.gitignore b/.gitignore index 8b3cba2..72302f2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ storage.db storage.json *.dca .eggs/ +.cache/ # Documentation stuff docs/api/ diff --git a/setup.py b/setup.py index 7d6cc57..bee0a7b 100644 --- a/setup.py +++ b/setup.py @@ -32,6 +32,7 @@ setup( install_requires=requirements, extras_require=extras_require, test_suite='tests', + tests_require=['pytest', 'pytest-benchmark'], classifiers=[ 'Development Status :: 4 - Beta', 'License :: OSI Approved :: MIT License', diff --git a/tests/api/__init__.py b/tests/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_reason.py b/tests/api/reason.py similarity index 100% rename from tests/test_reason.py rename to tests/api/reason.py diff --git a/tests/bot/__init__.py b/tests/bot/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_bot.py b/tests/bot/bot.py similarity index 100% rename from tests/test_bot.py rename to tests/bot/bot.py diff --git a/tests/gateway/__init__.py b/tests/gateway/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/gateway/events.py b/tests/gateway/events.py new file mode 100644 index 0000000..2714a5a --- /dev/null +++ b/tests/gateway/events.py @@ -0,0 +1,20 @@ +from disco.gateway.events import GatewayEvent, Resumed + + +def create_resumed_payload(): + return GatewayEvent.from_dispatch(None, { + 't': 'RESUMED', + 'd': { + '_trace': ['test', '1', '2', '3'], + } + }) + + +def test_from_dispatch(): + event = create_resumed_payload() + assert isinstance(event, Resumed) + assert event.trace == ['test', '1', '2', '3'] + + +def test_event_creation(benchmark): + benchmark(create_resumed_payload) diff --git a/tests/test_imports.py b/tests/imports.py similarity index 100% rename from tests/test_imports.py rename to tests/imports.py diff --git a/tests/types/__init__.py b/tests/types/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_channel.py b/tests/types/channel.py similarity index 78% rename from tests/test_channel.py rename to tests/types/channel.py index 56d5227..88695ea 100644 --- a/tests/test_channel.py +++ b/tests/types/channel.py @@ -4,7 +4,7 @@ from disco.types.channel import Channel, ChannelType class TestChannel(TestCase): - def test_nsfw_channel(self): + def test_deprecated_nsfw_channel(self): channel = Channel( name='nsfw-testing', type=ChannelType.GUILD_TEXT) @@ -19,3 +19,7 @@ class TestChannel(TestCase): name='nsfw_testing', type=ChannelType.GUILD_TEXT) self.assertFalse(channel.is_nsfw) + + def test_nsfw_channel(self): + channel = Channel(name='test', nsfw=True, type=ChannelType.GUILD_TEXT) + assert channel.is_nsfw diff --git a/tests/test_embeds.py b/tests/types/embeds.py similarity index 100% rename from tests/test_embeds.py rename to tests/types/embeds.py diff --git a/tests/test_types.py b/tests/types/types.py similarity index 100% rename from tests/test_types.py rename to tests/types/types.py diff --git a/tests/test_user.py b/tests/types/user.py similarity index 100% rename from tests/test_user.py rename to tests/types/user.py diff --git a/tests/util/__init__.py b/tests/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_util_limiter.py b/tests/util/limiter.py similarity index 100% rename from tests/test_util_limiter.py rename to tests/util/limiter.py diff --git a/tests/utils.py b/tests/utils.py index c0c615e..74f9766 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,5 +1,3 @@ -import time -import contextlib from disco.api.client import APIClient @@ -15,24 +13,3 @@ class TestAPIClient(APIClient): def __init__(self): self.client = None self.http = CallContainer() - - -def bench(times, func): - main_start = time.time() - - worst = None - best = None - - for _ in range(times): - start = time.time() - func() - dur = time.time() - start - - if not worst or dur > worst: - worst = dur - - if not best or dur < best: - best = dur - - main_dur = time.time() - main_start - return main_dur, worst, best diff --git a/tests/voice/__init__.py b/tests/voice/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tests_voice_queue.py b/tests/voice/queue.py similarity index 100% rename from tests/tests_voice_queue.py rename to tests/voice/queue.py From af4d22e61404698548d2df84d84acdaa04b6fcc6 Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 18 Jul 2017 14:36:16 -0700 Subject: [PATCH 2/6] Fix travis and pathing --- .travis.yml | 2 +- setup.cfg | 5 +++++ setup.py | 1 + tests/api/reason.py | 8 ++++---- tests/utils.py | 4 ++-- 5 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 setup.cfg diff --git a/.travis.yml b/.travis.yml index 4e6a402..cd15ae9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ python: - '3.6' - 'nightly' -script: 'python setup.py test' +script: 'py.test' diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..8f214ee --- /dev/null +++ b/setup.cfg @@ -0,0 +1,5 @@ +[aliases] +test=pytest + +[tool:pytest] +python_files = tests/*.py diff --git a/setup.py b/setup.py index bee0a7b..e348bae 100644 --- a/setup.py +++ b/setup.py @@ -32,6 +32,7 @@ setup( install_requires=requirements, extras_require=extras_require, test_suite='tests', + setup_requires=['pytest-runner'], tests_require=['pytest', 'pytest-benchmark'], classifiers=[ 'Development Status :: 4 - Beta', diff --git a/tests/api/reason.py b/tests/api/reason.py index 4582467..cd9c5fa 100644 --- a/tests/api/reason.py +++ b/tests/api/reason.py @@ -1,18 +1,18 @@ from unittest import TestCase +from tests.utils import APIClient + class TestReason(TestCase): def test_set_unicode_reason(self): - from tests.utils import TestAPIClient - api = TestAPIClient() + api = APIClient() api.guilds_channels_modify(1, 2, 3, reason=u'yo \U0001F4BF test') _, kwargs = api.http.calls[0] self.assertEqual(kwargs['headers']['X-Audit-Log-Reason'], 'yo%20%F0%9F%92%BF%20test') def test_null_reason(self): - from tests.utils import TestAPIClient - api = TestAPIClient() + api = APIClient() api.guilds_channels_modify(1, 2, 3, reason=None) _, kwargs = api.http.calls[0] diff --git a/tests/utils.py b/tests/utils.py index 74f9766..36fde6f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,4 +1,4 @@ -from disco.api.client import APIClient +from disco.api.client import APIClient as _APIClient class CallContainer(object): @@ -9,7 +9,7 @@ class CallContainer(object): self.calls.append((args, kwargs)) -class TestAPIClient(APIClient): +class APIClient(_APIClient): def __init__(self): self.client = None self.http = CallContainer() From 47a10b4cf1bbcb366c835e7a2817396e2f4c397c Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 18 Jul 2017 14:57:54 -0700 Subject: [PATCH 3/6] Try newer version of pytest-benchmarks --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e348bae..3914101 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ setup( extras_require=extras_require, test_suite='tests', setup_requires=['pytest-runner'], - tests_require=['pytest', 'pytest-benchmark'], + tests_require=['pytest', 'pytest-benchmark==3.1.0a2'], classifiers=[ 'Development Status :: 4 - Beta', 'License :: OSI Approved :: MIT License', From 19f0d766adabb83bd21ab2161c02030f9027840f Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 18 Jul 2017 15:01:03 -0700 Subject: [PATCH 4/6] Just use setup.py test for travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cd15ae9..4e6a402 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ python: - '3.6' - 'nightly' -script: 'py.test' +script: 'python setup.py test' From bd213afb6c566cee506033dfceeb4d0e7bf1a8e9 Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 18 Jul 2017 15:13:48 -0700 Subject: [PATCH 5/6] Upgrade pip within travis run --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 4e6a402..fc2dc6a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,7 @@ python: - '3.6' - 'nightly' +install: + - pip install --user -U pip + script: 'python setup.py test' From 3544bc05dfad7fe372355a7e61d6b1001c05888f Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 18 Jul 2017 15:31:47 -0700 Subject: [PATCH 6/6] Actually fix travis builds --- .travis.yml | 3 +-- setup.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index fc2dc6a..ec028e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,9 +8,8 @@ python: - '3.4' - '3.5' - '3.6' - - 'nightly' install: - - pip install --user -U pip + - pip install -U pip setuptools pytest script: 'python setup.py test' diff --git a/setup.py b/setup.py index 3914101..aa2d6fb 100644 --- a/setup.py +++ b/setup.py @@ -32,8 +32,8 @@ setup( install_requires=requirements, extras_require=extras_require, test_suite='tests', - setup_requires=['pytest-runner'], - tests_require=['pytest', 'pytest-benchmark==3.1.0a2'], + setup_requires=['pytest-runner==2.11.1'], + tests_require=['pytest==3.1.3', 'pytest-benchmark==3.1.0a2'], classifiers=[ 'Development Status :: 4 - Beta', 'License :: OSI Approved :: MIT License',