Port test suite over to Nose.

This commit is contained in:
Aldo Cortesi 2012-06-09 15:08:51 +12:00
parent 456b11df9a
commit 00c8bef0ff
9 changed files with 110 additions and 80 deletions

3
.coveragerc Normal file
View File

@ -0,0 +1,3 @@
[report]
omit = *contrib*
include = *libpathod*

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ MANIFEST
# Vim swap files # Vim swap files
*.swp *.swp
/doc /doc
.coverage

View File

@ -393,10 +393,8 @@ class Response:
def add_timeout(self, s, callback): def add_timeout(self, s, callback):
if TESTING: if TESTING:
callback() callback()
# begin nocover else: # pragma: no cover
else:
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + s, callback) tornado.ioloop.IOLoop.instance().add_timeout(time.time() + s, callback)
# end nocover
def write_values(self, fp, vals, actions, sofar=0, skip=0, blocksize=BLOCKSIZE): def write_values(self, fp, vals, actions, sofar=0, skip=0, blocksize=BLOCKSIZE):
while vals: while vals:

View File

@ -1,6 +1,7 @@
import json, threading, Queue import json, threading, Queue
import requests import requests
import pathod, utils import pathod, utils
import tutils
IFACE = "127.0.0.1" IFACE = "127.0.0.1"

View File

@ -1,8 +1,7 @@
import libpry
from libpathod import pathod from libpathod import pathod
from tornado import httpserver from tornado import httpserver
class uApplication(libpry.AutoTree): class TestApplication:
def test_anchors(self): def test_anchors(self):
a = pathod.PathodApp(staticdir=None) a = pathod.PathodApp(staticdir=None)
a.add_anchor("/foo", "200") a.add_anchor("/foo", "200")
@ -31,13 +30,6 @@ class uApplication(libpry.AutoTree):
assert not a.log_by_id(0) assert not a.log_by_id(0)
class u_make_server(libpry.AutoTree): def test_make_server():
def test_simple(self):
app = pathod.PathodApp() app = pathod.PathodApp()
assert pathod.make_server(app, 0, "127.0.0.1", None) assert pathod.make_server(app, 0, "127.0.0.1", None)
tests = [
uApplication(),
u_make_server()
]

View File

@ -1,11 +1,11 @@
import os import os
import libpry
from libpathod import rparse, utils from libpathod import rparse, utils
import tutils
rparse.TESTING = True rparse.TESTING = True
class uMisc(libpry.AutoTree): class TestMisc:
def test_generators(self): def test_generators(self):
v = rparse.Value.parseString("'val'")[0] v = rparse.Value.parseString("'val'")[0]
g = v.get_generator({}) g = v.get_generator({})
@ -26,7 +26,7 @@ class uMisc(libpry.AutoTree):
assert g[1] == "n" assert g[1] == "n"
def test_filegenerator(self): def test_filegenerator(self):
t = self.tmpdir() with tutils.tmpdir() as t:
path = os.path.join(t, "foo") path = os.path.join(t, "foo")
f = open(path, "w") f = open(path, "w")
f.write("x"*10000) f.write("x"*10000)
@ -50,7 +50,7 @@ class uMisc(libpry.AutoTree):
v = rparse.Value.parseString("<path")[0] v = rparse.Value.parseString("<path")[0]
assert v.path == "path" assert v.path == "path"
t = self.tmpdir() with tutils.tmpdir() as t:
p = os.path.join(t, "path") p = os.path.join(t, "path")
f = open(p, "w") f = open(p, "w")
f.write("x"*10000) f.write("x"*10000)
@ -59,8 +59,8 @@ class uMisc(libpry.AutoTree):
assert v.get_generator(dict(staticdir=t)) assert v.get_generator(dict(staticdir=t))
v = rparse.Value.parseString("<path2")[0] v = rparse.Value.parseString("<path2")[0]
libpry.raises(rparse.ServerError, v.get_generator, dict(staticdir=t)) tutils.raises(rparse.ServerError, v.get_generator, dict(staticdir=t))
libpry.raises("no static directory", v.get_generator, dict()) tutils.raises("no static directory", v.get_generator, dict())
def test_generated_value(self): def test_generated_value(self):
v = rparse.Value.parseString("@10b")[0] v = rparse.Value.parseString("@10b")[0]
@ -136,7 +136,7 @@ class uMisc(libpry.AutoTree):
s.serve(d) s.serve(d)
class uDisconnects(libpry.AutoTree): class TestDisconnects:
def test_parse(self): def test_parse(self):
assert (0, "disconnect") in rparse.parse({}, "400:d0").actions assert (0, "disconnect") in rparse.parse({}, "400:d0").actions
assert ("r", "disconnect") in rparse.parse({}, "400:dr").actions assert ("r", "disconnect") in rparse.parse({}, "400:dr").actions
@ -155,13 +155,13 @@ class uDisconnects(libpry.AutoTree):
assert v.value == "r" assert v.value == "r"
class uShortcuts(libpry.AutoTree): class TestShortcuts:
def test_parse(self): def test_parse(self):
assert rparse.parse({}, "400:c'foo'").headers[0][0][:] == "Content-Type" assert rparse.parse({}, "400:c'foo'").headers[0][0][:] == "Content-Type"
assert rparse.parse({}, "400:l'foo'").headers[0][0][:] == "Location" assert rparse.parse({}, "400:l'foo'").headers[0][0][:] == "Location"
class uPauses(libpry.AutoTree): class TestPauses:
def test_parse(self): def test_parse(self):
e = rparse.PauseAt.expr() e = rparse.PauseAt.expr()
v = e.parseString("p10,10")[0] v = e.parseString("p10,10")[0]
@ -182,9 +182,9 @@ class uPauses(libpry.AutoTree):
assert r.actions[0] == (10, "pause", 10) assert r.actions[0] == (10, "pause", 10)
class uparse(libpry.AutoTree): class TestParse:
def test_parse_err(self): def test_parse_err(self):
libpry.raises(rparse.ParseException, rparse.parse, {}, "400:msg,b:") tutils.raises(rparse.ParseException, rparse.parse, {}, "400:msg,b:")
try: try:
rparse.parse({}, "400'msg':b:") rparse.parse({}, "400'msg':b:")
except rparse.ParseException, v: except rparse.ParseException, v:
@ -212,7 +212,7 @@ class uparse(libpry.AutoTree):
assert r.length() assert r.length()
class uResponse(libpry.AutoTree): class TestResponse:
def dummy_response(self): def dummy_response(self):
return rparse.parse({}, "400'msg'") return rparse.parse({}, "400'msg'")
@ -291,13 +291,3 @@ class uResponse(libpry.AutoTree):
testlen(rparse.parse({}, "400'msg'")) testlen(rparse.parse({}, "400'msg'"))
testlen(rparse.parse({}, "400'msg':h'foo'='bar'")) testlen(rparse.parse({}, "400'msg':h'foo'='bar'"))
testlen(rparse.parse({}, "400'msg':h'foo'='bar':b@100b")) testlen(rparse.parse({}, "400'msg':h'foo'='bar':b@100b"))
tests = [
uResponse(),
uPauses(),
uDisconnects(),
uMisc(),
uparse(),
uShortcuts()
]

View File

@ -1,18 +1,18 @@
import time, logging import time, logging
import libpry
import requests import requests
from libpathod import test, version, utils from libpathod import test, version, utils
import tutils
logging.disable(logging.CRITICAL) logging.disable(logging.CRITICAL)
class uDaemonManual(libpry.AutoTree): class TestDaemonManual:
def test_startstop(self): def test_startstop(self):
d = test.Daemon() d = test.Daemon()
rsp = requests.get("http://localhost:%s/p/202"%d.port) rsp = requests.get("http://localhost:%s/p/202"%d.port)
assert rsp.ok assert rsp.ok
assert rsp.status_code == 202 assert rsp.status_code == 202
d.shutdown() d.shutdown()
libpry.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port) tutils.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port)
def test_startstop_ssl(self): def test_startstop_ssl(self):
d = test.Daemon(ssl=True) d = test.Daemon(ssl=True)
@ -20,7 +20,7 @@ class uDaemonManual(libpry.AutoTree):
assert rsp.ok assert rsp.ok
assert rsp.status_code == 202 assert rsp.status_code == 202
d.shutdown() d.shutdown()
libpry.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port) tutils.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port)
def test_startstop_ssl_explicit(self): def test_startstop_ssl_explicit(self):
ssloptions = dict( ssloptions = dict(
@ -32,22 +32,18 @@ class uDaemonManual(libpry.AutoTree):
assert rsp.ok assert rsp.ok
assert rsp.status_code == 202 assert rsp.status_code == 202
d.shutdown() d.shutdown()
libpry.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port) tutils.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port)
class uDaemon(libpry.AutoTree): class TestDaemon:
@classmethod
def setUpAll(self): def setUpAll(self):
self.d = test.Daemon() self.d = test.Daemon()
@classmethod
def tearDownAll(self): def tearDownAll(self):
self.d.shutdown() self.d.shutdown()
def test_info(self): def test_info(self):
assert tuple(self.d.info()["version"]) == version.IVERSION assert tuple(self.d.info()["version"]) == version.IVERSION
tests = [
uDaemonManual(),
uDaemon()
]

View File

@ -1,21 +1,13 @@
import libpry
from libpathod import utils from libpathod import utils
import tutils
class uparse_anchor_spec(libpry.AutoTree): def test_parse_anchor_spec():
def test_simple(self):
assert utils.parse_anchor_spec("foo=200", {}) == ("foo", "200") assert utils.parse_anchor_spec("foo=200", {}) == ("foo", "200")
libpry.raises(utils.AnchorError, utils.parse_anchor_spec, "foobar", {}) tutils.raises(utils.AnchorError, utils.parse_anchor_spec, "foobar", {})
libpry.raises(utils.AnchorError, utils.parse_anchor_spec, "*=200", {}) tutils.raises(utils.AnchorError, utils.parse_anchor_spec, "*=200", {})
libpry.raises(utils.AnchorError, utils.parse_anchor_spec, "foo=bar", {}) tutils.raises(utils.AnchorError, utils.parse_anchor_spec, "foo=bar", {})
class udata_path(libpry.AutoTree): def test_data_path():
def test_simple(self): tutils.raises(ValueError, utils.data.path, "nonexistent")
libpry.raises(ValueError, utils.data.path, "nonexistent")
tests = [
udata_path(),
uparse_anchor_spec()
]

57
test/tutils.py Normal file
View File

@ -0,0 +1,57 @@
import tempfile, os, shutil
from contextlib import contextmanager
from libpathod import utils
@contextmanager
def tmpdir(*args, **kwargs):
orig_workdir = os.getcwd()
temp_workdir = tempfile.mkdtemp(*args, **kwargs)
os.chdir(temp_workdir)
yield temp_workdir
os.chdir(orig_workdir)
shutil.rmtree(temp_workdir)
def raises(exc, obj, *args, **kwargs):
"""
Assert that a callable raises a specified exception.
:exc An exception class or a string. If a class, assert that an
exception of this type is raised. If a string, assert that the string
occurs in the string representation of the exception, based on a
case-insenstivie match.
:obj A callable object.
:args Arguments to be passsed to the callable.
:kwargs Arguments to be passed to the callable.
"""
try:
apply(obj, args, kwargs)
except Exception, v:
if isinstance(exc, basestring):
if exc.lower() in str(v).lower():
return
else:
raise AssertionError(
"Expected %s, but caught %s"%(
repr(str(exc)), v
)
)
else:
if isinstance(v, exc):
return
else:
raise AssertionError(
"Expected %s, but caught %s %s"%(
exc.__name__, v.__class__.__name__, str(v)
)
)
raise AssertionError("No exception raised.")
test_data = utils.Data(__name__)