def test_find_target_with_invalid_path(): bar = StubTraversable(_name='bar') foo = StubTraversable( _name='foo', members=Struct( bar=bar, ), ) root = StubTraversable( _name='root', members=Struct(foo=foo), ) root = root.bind(request=None) with pytest.raises(InvalidEndpointPathException) as e: find_target(path='/foo/bar/baz', root=root) assert ( str(e.value) == "Given path /foo/bar/baz not found.\n" " Short alternatives:\n" " ''\n" " foo\n" " bar\n" " Long alternatives:\n" " ''\n" " foo\n" " foo/bar" )
def test_debug_tree_on_debug_false(): class NestedPage(Page): foo = 'foo' class MyPage(Page): bar = 'bar' nested = NestedPage() root = MyPage().bind(request=req('get', **{'/debug_tree': '7'})) with pytest.raises(AssertionError): find_target(path='/debug_tree', root=root)
def test_debug_tree(settings): settings.DEBUG = True class NestedPage(Page): foo = 'foo' class MyPage(Page): bar = 'bar' nested = NestedPage() root = MyPage().bind(request=req('get', **{'/debug_tree': '7'})) target = find_target(path='/debug_tree', root=root) result = target.func(value='', **target.iommi_evaluate_parameters()) assert isinstance(result, Table) tree = [ ', '.join([str(x.value) for x in cells]) for cells in result.cells_for_rows() ] expected = """, , MyPage, True endpoints, None, Members[Endpoint], True endpoints__debug_tree, debug_tree, Endpoint, True parts, None, Members[Part], True parts__bar, bar, Fragment, True parts__bar__children, None, Members[str], True parts__bar__children__text, None, str, False parts__nested, nested, NestedPage, True parts__nested__parts, None, Members[Part], True parts__nested__parts__foo, foo, Fragment, True parts__nested__parts__foo__children, None, Members[str], True parts__nested__parts__foo__children__text, None, str, False""" assert '\n'.join(tree) == expected
def test_find_target(): # To build paths: _declared_members: Struct, and optionally name # To find target: _long_path_by_path: Dict on root bar = StubTraversable(_name='bar') foo = StubTraversable( _name='foo', members=Struct(bar=bar, ), ) root = StubTraversable( _name='root', members=Struct(foo=foo), ) root = root.bind(request=None) target = find_target(path='/foo/bar', root=root) assert target._declared is bar assert build_long_path(target) == 'foo/bar'