def test_loop_3(): router = Router() router.route("a", "/a", before="b")(Mock()) router.route("b", "/b", before="c")(Mock()) router.route("c", "/c", before="a")(Mock()) with pytest.raises(DependencyLoop): init({"router": router}, ctx=init_ctx())._finalize()
def test_empty_route(): router = Router() router.route('route', '')( lambda: None) conf = init({'router': router}, ctx=init_ctx()) conf._finalize() assert conf.route('route').url(conf.ctx.Context()) == '/'
def test_pathed_variable_1(): router = Router() router.route('route', '/{article.id}')( lambda article: None) conf = init({'router': router}, ctx=init_ctx()) conf._finalize() assert conf.route('route').url(conf.ctx.Context(), Mock(id=123)) == '/123'
def test_variable_2(): router = Router() router.route('route', '/foo/{var>\d+}')( lambda var: None) conf = init({'router': router}, ctx=init_ctx()) conf._finalize() assert conf.route('route').url(conf.ctx.Context(), 123) == '/foo/123'
def test_pathed_variable_3(): router = Router() router.route('route', '/{article.author.slug}/{article.id}')( lambda article: None) conf = init({'router': router}, ctx=init_ctx()) conf._finalize() with pytest.raises(MissingVariable): conf.route('route').url(conf.ctx.Context(), object())
def test_pathed_variable_2(): router = Router() router.route('route', '/{article.author.slug}/{article.id}')( lambda article: None) conf = init({'router': router}, ctx=init_ctx()) conf._finalize() article = Mock(id=123, author=Mock(slug='author-slug')) assert conf.route('route').url(conf.ctx.Context(), article) == '/author-slug/123'
def test_variable_3(): router = Router() router.route('route', '/foo/{var>\d+}')( lambda var: None) conf = init({'router': router}, ctx=init_ctx()) conf._finalize() with pytest.raises(InvalidVariable): conf.route('route').url(conf.ctx.Context(), 'bar')
def test_custom_order_1(): router = Router() b = router.route("b", "/b")(Mock()) router.route("a", "/a", after=b)(Mock()) conf = init({"router": router}, ctx=init_ctx()) conf._finalize() sorted_routes = list(conf.routes.keys()) assert "a" in sorted_routes assert "b" in sorted_routes assert sorted_routes.index("b") < sorted_routes.index("a")
def test_simple_route_comparison_1(): router = Router() router.route("a", "/a")(Mock()) router.route("b", "/b")(Mock()) conf = init({"router": router}, ctx=init_ctx()) conf._finalize() sorted_routes = list(conf.routes.keys()) assert "a" in sorted_routes assert "b" in sorted_routes assert sorted_routes.index("a") < sorted_routes.index("b")
def test_custom_order_2(): """ same as test_custom_order_1, but passing a.after as string """ router = Router() router.route("b", "/b")(Mock()) router.route("a", "/a", after="b")(Mock()) conf = init({"router": router}, ctx=init_ctx()) conf._finalize() sorted_routes = list(conf.routes.keys()) assert "a" in sorted_routes assert "b" in sorted_routes assert sorted_routes.index("b") < sorted_routes.index("a")
def test_simple_route_comparison_2(): """ same as test_simple_route_comparison_1, but with definition order reversed. """ router = Router() router.route("b", "/b")(Mock()) router.route("a", "/a")(Mock()) conf = init({"router": router}, ctx=init_ctx()) conf._finalize() sorted_routes = list(conf.routes.keys()) assert "a" in sorted_routes assert "b" in sorted_routes assert sorted_routes.index("a") < sorted_routes.index("b")
def test_empty_router(): router = Router() init({"router": router}, ctx=init_ctx())._finalize()