Skip to content

Commit

Permalink
Test the utils module.
Browse files Browse the repository at this point in the history
The doc tests have also been explicitly ommited from the coverage
results.
  • Loading branch information
dpnova committed Aug 15, 2014
1 parent a7848cc commit 9838918
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cyclone/tests/test_httpserver.py
Expand Up @@ -365,7 +365,7 @@ def test_request_time_empty_finish(self):
self.assertTrue(self.req.request_time() < 0.01)

def test_request_time(self):
self.assertTrue(self.req.request_time() <0.01)
self.assertTrue(self.req.request_time() < 0.01)

def test_repr(self):
"""
Expand Down
47 changes: 47 additions & 0 deletions cyclone/tests/test_utils.py
Expand Up @@ -20,6 +20,9 @@
from cyclone.escape import squeeze, url_escape, url_unescape
from cyclone.escape import utf8, to_unicode, to_basestring
from cyclone.escape import recursive_unicode, linkify
from cyclone.util import _emit, ObjectDict, import_object
from mock import Mock
import datetime


class EscapeTest(unittest.TestCase):
Expand Down Expand Up @@ -100,3 +103,47 @@ def test_linkify(self):
"alongurlrighthere"
"/a/long/url/right/here",
shorten=True, require_protocol=True, extra_params=lambda x: "x=y")


class UtilsTest(unittest.TestCase):
def test_emit(self):
m = Mock()
t = datetime.time()
_emit(m, {
"message": "some message",
"time": t
})
m.formatTime.assert_called_with(t)

def test_emit_empty(self):
m = Mock()
t = datetime.time()
failure = Mock()
failure.getTraceback.return_value = ""
_emit(m, {
"message": "",
"time": t,
"isError": "",
"failure": failure
})
self.assertEqual(m.formatTime.call_count, 0)

def test_object_dict(self):
od = ObjectDict()
self.assertRaises(AttributeError, getattr, od, "something")
od["foo"] = "bar"
self.assertEqual(od['foo'], "bar")
self.assertEqual(od.foo, "bar")
od.rah = "meow"
self.assertEqual(od['rah'], "meow")

def test_import_object(self):
import os.path
other_os = import_object("os.path")
self.assertIs(os.path, other_os)

def test_import_object_fail(self):
self.assertRaises(ImportError, import_object, "meowkittens.something")

def test_import_object_fail_no_method(self):
self.assertRaises(ImportError, import_object, "os.something")
2 changes: 1 addition & 1 deletion cyclone/util.py
Expand Up @@ -87,6 +87,6 @@ def import_object(name):
basestring_type = basestring


def doctests():
def doctests(): # pragma: no cover
import doctest
return doctest.DocTestSuite()

0 comments on commit 9838918

Please sign in to comment.