Skip to content

Commit e3e411f

Browse files
author
jjjkkkjjj
committed
add usual case with include (#2110)
1 parent e35c2d5 commit e3e411f

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

channels/routing.py

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def _parse_resolver(child_url_pattern, parent_resolver, parent_regex, routes):
9494
child_url_pattern.pattern.regex,
9595
]
9696
)
97-
print([parent_resolver.pattern.regex, child_url_pattern.pattern.regex])
9897

9998
# Remove the redundant caret ^ which is appended by `path` function
10099
regex = re.sub(r"(?<!^)\^", "", regex)

tests/test_routing.py

+128
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,85 @@ async def test_path_remaining():
301301
)
302302

303303

304+
# @pytest.mark.asyncio
305+
# async def test_url_router_nesting_by_include(root_urlconf):
306+
# """
307+
# Tests that nested URLRouters is constructed by include function.
308+
# """
309+
# import sys
310+
# from django.urls import include
311+
312+
# test_app = MockApplication(return_value=1)
313+
314+
# # mocking the universe module following the directory structure;
315+
# # ├── universe
316+
# # │ └── routings.py
317+
# # └── routings.py (parent)
318+
# #
319+
# #
320+
# # in routings.py
321+
# # ======================
322+
# # ...
323+
# # urlpatterns = [
324+
# # re_path(r"book/(?P<book>[\w\-]+)/page/(?P<page>\d+)/$", test_app),
325+
# # re_path(r"test/(\d+)/$", test_app),
326+
# # ]
327+
# # ======================
328+
# module_routings = type(sys)("routings")
329+
# module_routings.urlpatterns = [
330+
# re_path(r"book/(?P<book>[\w\-]+)/page/(?P<page>\d+)/$", test_app),
331+
# re_path(r"test/(\d+)/$", test_app),
332+
# path("home/", test_app, name='home'),
333+
# ]
334+
# module = type(sys)("universe")
335+
# module.routings = module_routings
336+
# sys.modules["src"] = type(sys)("routings")
337+
# sys.modules["src.universe"] = module
338+
# sys.modules["src.universe.routings"] = module.routings
339+
340+
# # parent routings.py
341+
# outer_router = URLRouter(
342+
# [
343+
# path("universe/", include("src.universe.routings"), name="universe"),
344+
# ]
345+
# )
346+
# assert (
347+
# await outer_router(
348+
# {
349+
# "type": "http",
350+
# "path": "/universe/book/channels-guide/page/10/",
351+
# },
352+
# None,
353+
# None,
354+
# )
355+
# == 1
356+
# )
357+
358+
# assert (
359+
# await outer_router(
360+
# {
361+
# "type": "http",
362+
# "path": "/universe/test/10/",
363+
# },
364+
# None,
365+
# None,
366+
# )
367+
# == 1
368+
# )
369+
370+
# assert (
371+
# await outer_router(
372+
# {
373+
# "type": "http",
374+
# "path": reverse('universe:home'),
375+
# },
376+
# None,
377+
# None,
378+
# )
379+
# == 1
380+
# )
381+
382+
304383
@pytest.mark.asyncio
305384
async def test_url_router_nesting_by_include(root_urlconf):
306385
"""
@@ -341,8 +420,33 @@ async def test_url_router_nesting_by_include(root_urlconf):
341420
outer_router = URLRouter(
342421
[
343422
path("universe/", include("universe.routings"), name="universe"),
423+
path("moon/", test_app, name="moon"),
424+
re_path(r"mars/(\d+)/$", test_app, name="mars"),
344425
]
345426
)
427+
assert (
428+
await outer_router(
429+
{
430+
"type": "http",
431+
"path": "/moon/",
432+
},
433+
None,
434+
None,
435+
)
436+
== 1
437+
)
438+
assert (
439+
await outer_router(
440+
{
441+
"type": "http",
442+
"path": "/mars/5/",
443+
},
444+
None,
445+
None,
446+
)
447+
== 1
448+
)
449+
346450
assert (
347451
await outer_router(
348452
{
@@ -421,8 +525,32 @@ async def test_url_router_deep_nesting_by_include(root_urlconf):
421525
outer_router = URLRouter(
422526
[
423527
path("universe/", include("universe.routings"), name="universe"),
528+
path("moon/", test_app, name="moon"),
529+
re_path(r"mars/(\d+)/$", test_app, name="mars"),
424530
]
425531
)
532+
assert (
533+
await outer_router(
534+
{
535+
"type": "http",
536+
"path": "/moon/",
537+
},
538+
None,
539+
None,
540+
)
541+
== 1
542+
)
543+
assert (
544+
await outer_router(
545+
{
546+
"type": "http",
547+
"path": "/mars/5/",
548+
},
549+
None,
550+
None,
551+
)
552+
== 1
553+
)
426554
assert (
427555
await outer_router(
428556
{

0 commit comments

Comments
 (0)