コード例 #1
0
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()
コード例 #2
0
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()) == '/'
コード例 #3
0
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'
コード例 #4
0
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'
コード例 #5
0
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())
コード例 #6
0
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'
コード例 #7
0
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')
コード例 #8
0
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")
コード例 #9
0
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")
コード例 #10
0
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")
コード例 #11
0
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")
コード例 #12
0
def test_empty_router():
    router = Router()
    init({"router": router}, ctx=init_ctx())._finalize()