Browse Source

[tests] cleanup / etc

pull/39/head
andrei 8 years ago
parent
commit
b3cd55e6b0
  1. 2
      examples/basic_plugin.py
  2. 9
      setup.py
  3. 0
      tests/__init__.py
  4. 4
      tests/test_reason.py
  5. 43
      tests/test_types.py
  6. 23
      tests/utils.py

2
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!'
"""

9
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',

0
tests/__init__.py

4
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')

43
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'})

23
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

Loading…
Cancel
Save