From b3cd55e6b0a1873d60f8004d7de75ae6e2fc0996 Mon Sep 17 00:00:00 2001 From: andrei Date: Fri, 23 Jun 2017 23:16:53 -0700 Subject: [PATCH] [tests] cleanup / etc --- examples/basic_plugin.py | 2 ++ setup.py | 9 +-------- tests/__init__.py | 0 tests/test_reason.py | 4 ++-- tests/test_types.py | 43 +++++++++++++++++++++------------------- tests/utils.py | 23 +++++++++++++++++++++ 6 files changed, 51 insertions(+), 30 deletions(-) create mode 100644 tests/__init__.py diff --git a/examples/basic_plugin.py b/examples/basic_plugin.py index 6dc57c3..e7ad8ec 100644 --- a/examples/basic_plugin.py +++ b/examples/basic_plugin.py @@ -66,6 +66,7 @@ class BasicPlugin(Plugin): return event.msg.reply(event.parser.format_help()) event.msg.reply(args.asdf) + """ @Plugin.route('/test') def on_test_route(self): # Disco has built-in support for Flask (if installed and enabled) which @@ -73,3 +74,4 @@ class BasicPlugin(Plugin): from flask import request print dict(request.headers) return 'Hi!' + """ diff --git a/setup.py b/setup.py index dd4b3b6..af6c7c4 100644 --- a/setup.py +++ b/setup.py @@ -3,13 +3,6 @@ from setuptools import setup, find_packages from disco import VERSION -def run_tests(): - import unittest - test_loader = unittest.TestLoader() - test_suite = test_loader.discover('tests', pattern='test_*.py') - return test_suite - - with open('requirements.txt') as f: requirements = f.readlines() @@ -38,7 +31,7 @@ setup( include_package_data=True, install_requires=requirements, extras_require=extras_require, - test_suite='setup.run_tests', + test_suite='tests', classifiers=[ 'Development Status :: 4 - Beta', 'License :: OSI Approved :: MIT License', diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_reason.py b/tests/test_reason.py index 8d9b39c..bced948 100644 --- a/tests/test_reason.py +++ b/tests/test_reason.py @@ -1,11 +1,11 @@ from unittest import TestCase -from utils import TestAPIClient class TestReason(TestCase): def test_set_unicode_reason(self): + from tests.utils import TestAPIClient api = TestAPIClient() api.guilds_channels_modify(1, 2, 3, reason=u'yo \U0001F4BF test') _, kwargs = api.http.calls[0] - self.assertEquals(kwargs['headers']['X-Audit-Log-Reason'], 'yo%20%F0%9F%92%BF%20test') + self.assertEqual(kwargs['headers']['X-Audit-Log-Reason'], 'yo%20%F0%9F%92%BF%20test') diff --git a/tests/test_types.py b/tests/test_types.py index a69774d..63d2af0 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1,5 +1,8 @@ -from unittest import TestCase +from __future__ import print_function + +import six +from unittest import TestCase from holster.enum import Enum from disco.types.base import Model, Field, enum, snowflake, ConversionError @@ -24,16 +27,16 @@ class _C(Model): class TestModel(TestCase): def test_model_simple_loading(self): inst = _A(dict(a=1, b=1.1, c='test')) - self.assertEquals(inst.a, 1) - self.assertEquals(inst.b, 1.1) - self.assertEquals(inst.c, 'test') + self.assertEqual(inst.a, 1) + self.assertEqual(inst.b, 1.1) + self.assertEqual(inst.c, 'test') def test_model_load_into(self): inst = _A() _A.load_into(inst, dict(a=1, b=1.1, c='test')) - self.assertEquals(inst.a, 1) - self.assertEquals(inst.b, 1.1) - self.assertEquals(inst.c, 'test') + self.assertEqual(inst.a, 1) + self.assertEqual(inst.b, 1.1) + self.assertEqual(inst.c, 'test') def test_model_loading_consume(self): obj = { @@ -55,10 +58,10 @@ class TestModel(TestCase): inst = _C() inst.load(obj, consume=True) - self.assertEquals(inst.a.a, 1) - self.assertEquals(inst.b.c, '1') + self.assertEqual(inst.a.a, 1) + self.assertEqual(inst.b.c, '1') - self.assertEquals(obj, {'a': {'d': 'wow'}, 'b': {'z': 'wtf'}, 'g': 'lmao'}) + self.assertEqual(obj, {'a': {'d': 'wow'}, 'b': {'z': 'wtf'}, 'g': 'lmao'}) def test_model_field_enum(self): en = Enum( @@ -70,18 +73,18 @@ class TestModel(TestCase): class _M(Model): field = Field(enum(en)) - self.assertEquals(_M(field=en.A).field, en.A) - self.assertEquals(_M(field=2).field, en.B) - self.assertEquals(_M(field='3').field, None) - self.assertEquals(_M(field='a').field, en.A) - self.assertEquals(_M(field='A').field, en.A) + self.assertEqual(_M(field=en.A).field, en.A) + self.assertEqual(_M(field=2).field, en.B) + self.assertEqual(_M(field='3').field, None) + self.assertEqual(_M(field='a').field, en.A) + self.assertEqual(_M(field='A').field, en.A) def test_model_field_snowflake(self): class _M(Model): field = Field(snowflake) - self.assertEquals(_M(field=327936274851954688).field, 327936274851954688) - self.assertEquals(_M(field='327936274851954688').field, 327936274851954688) + self.assertEqual(_M(field=327936274851954688).field, 327936274851954688) + self.assertEqual(_M(field='327936274851954688').field, 327936274851954688) with self.assertRaises(ConversionError): _M(field='asdf') @@ -91,11 +94,11 @@ class TestModel(TestCase): def __init__(self, v): self.v = v - def __unicode__(self): + def __str__(self): return self.v class _M(Model): - field = Field(Object, cast=unicode) + field = Field(Object, cast=six.text_type) inst = _M(field=u'wowza') - self.assertEquals(inst.to_dict(), {'field': u'wowza'}) + self.assertEqual(inst.to_dict(), {'field': u'wowza'}) diff --git a/tests/utils.py b/tests/utils.py index 74f9766..c0c615e 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,3 +1,5 @@ +import time +import contextlib from disco.api.client import APIClient @@ -13,3 +15,24 @@ 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