Created
February 8, 2012 20:16
-
-
Save freeekanayaka/1773008 to your computer and use it in GitHub Desktop.
Context routes tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from zope.interface import providedBy | |
from pyramid import testing | |
from pyramid.interfaces import IRequest, ITraverser | |
from pyramid_contextroutes import ( | |
ContextRoutes, route, ContextRoutesTraverser, IContextRoutes) | |
class Dummy(object): | |
def __init__(self, name, route=None): | |
self.name = name | |
self.route = route | |
class DummyRoutes(ContextRoutes): | |
@route("/child1") | |
def route1(self): | |
return Dummy("child1", "route1") | |
@route("/child{index}") | |
def route2(self, index): | |
return Dummy("child" + index, "route2") | |
@route("/childN") | |
def route3(self, name): | |
return Dummy(name, "route3") | |
@route("/other") | |
def route4(self): | |
return "other" | |
class TestContextRoutes(unittest.TestCase): | |
def setUp(self): | |
super(TestContextRoutes, self) | |
self.context = Dummy("parent") | |
self.request = testing.DummyRequest() | |
self.routes = DummyRoutes(self.context, self.request) | |
def test_interface(self): | |
self.assertIn(IContextRoutes, providedBy(self.routes)) | |
def test__init__(self): | |
self.assertIs(self.context, self.routes.context) | |
self.assertIs(self.request, self.routes.request) | |
def test__call__no_match(self): | |
info = self.routes("") | |
self.assertIs(None, info) | |
def test__call__simple_match(self): | |
info = self.routes("/child1") | |
context = info["context"] | |
traverse = info["traverse"] | |
self.assertEqual("child1", context.name) | |
self.assertEqual("route1", context.route) | |
self.assertEqual("", traverse) | |
def test__call__match_with_parameters(self): | |
info = self.routes("/child2") | |
context = info["context"] | |
self.assertEqual("child2", context.name) | |
self.assertEqual("route2", context.route) | |
def test__call__match_order(self): | |
info = self.routes("/childN") | |
context = info["context"] | |
self.assertEqual("childN", context.name) | |
self.assertEqual("route2", context.route) | |
class TestContextRoutesTraverser(unittest.TestCase): | |
def setUp(self): | |
super(TestContextRoutesTraverser, self) | |
self.request = testing.DummyRequest() | |
self.request.registry.registerAdapter(DummyRoutes, (Dummy, IRequest)) | |
self.traverser = ContextRoutesTraverser(Dummy("root")) | |
def test_interface(self): | |
self.assertIn(ITraverser, providedBy(self.traverser)) | |
def test__call__root(self): | |
self.request.environ["PATH_INFO"] = '/' | |
info = self.traverser(self.request) | |
context = info["context"] | |
self.assertEqual("root", context.name) | |
self.assertEqual("", info["view_name"]) | |
self.assertEqual((), info["subpath"]) | |
self.assertEqual((), info["traversed"]) | |
def test__call__no_match(self): | |
self.request.environ["PATH_INFO"] = '/some-view' | |
info = self.traverser(self.request) | |
context = info["context"] | |
self.assertEqual("root", context.name) | |
self.assertEqual("some-view", info["view_name"]) | |
self.assertEqual(("some-view",), info["subpath"]) | |
self.assertEqual((), info["traversed"]) | |
def test__call__no_route(self): | |
self.request.environ["PATH_INFO"] = '/child1/other' | |
info = self.traverser(self.request) | |
context = info["context"] | |
self.assertEqual("other", context) | |
self.assertEqual("", info["view_name"]) | |
self.assertEqual((), info["subpath"]) | |
self.assertEqual(("child1", "other"), info["traversed"]) | |
def test__call__no_route_with_view(self): | |
self.request.environ["PATH_INFO"] = '/child1/other/some-view' | |
info = self.traverser(self.request) | |
context = info["context"] | |
self.assertEqual("other", context) | |
self.assertEqual("some-view", info["view_name"]) | |
self.assertEqual(("some-view",), info["subpath"]) | |
self.assertEqual(("child1", "other"), info["traversed"]) | |
def test__call__simple_route(self): | |
self.request.environ["PATH_INFO"] = '/child1' | |
info = self.traverser(self.request) | |
context = info["context"] | |
self.assertEqual("child1", context.name) | |
self.assertEqual("route1", context.route) | |
self.assertEqual("", info["view_name"]) | |
self.assertEqual((), info["subpath"]) | |
self.assertEqual(("child1",), info["traversed"]) | |
def test__call__nested_route(self): | |
self.request.environ["PATH_INFO"] = '/child1/child2/child3' | |
info = self.traverser(self.request) | |
context = info["context"] | |
self.assertEqual("child3", context.name) | |
self.assertEqual("route2", context.route) | |
self.assertEqual("", info["view_name"]) | |
self.assertEqual((), info["subpath"]) | |
self.assertEqual(("child1", "child2", "child3"), info["traversed"]) | |
def test__call__view_name(self): | |
self.request.environ["PATH_INFO"] = '/child1/some-view' | |
info = self.traverser(self.request) | |
context = info["context"] | |
self.assertEqual("child1", context.name) | |
self.assertEqual("route1", context.route) | |
self.assertEqual("some-view", info["view_name"]) | |
self.assertEqual(("some-view",), info["subpath"]) | |
self.assertEqual(("child1",), info["traversed"]) | |
def test__call__sub_path(self): | |
self.request.environ["PATH_INFO"] = '/child1/some-view/other/segments' | |
info = self.traverser(self.request) | |
context = info["context"] | |
self.assertEqual("child1", context.name) | |
self.assertEqual("route1", context.route) | |
self.assertEqual("some-view", info["view_name"]) | |
self.assertEqual(("some-view", "other", "segments"), info["subpath"]) | |
self.assertEqual(("child1",), info["traversed"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment