1
0

test_route.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import pytest
  2. from reflex import constants
  3. from reflex.app import App
  4. from reflex.route import catchall_in_route, get_route_args, verify_route_validity
  5. @pytest.mark.parametrize(
  6. "route_name, expected",
  7. [
  8. ("/users/[id]", {"id": constants.RouteArgType.SINGLE}),
  9. (
  10. "/posts/[postId]/comments/[commentId]",
  11. {
  12. "postId": constants.RouteArgType.SINGLE,
  13. "commentId": constants.RouteArgType.SINGLE,
  14. },
  15. ),
  16. ],
  17. )
  18. def test_route_args(route_name, expected):
  19. assert get_route_args(route_name) == expected
  20. @pytest.mark.parametrize(
  21. "route_name",
  22. [
  23. "/products/[id]/[id]",
  24. "/posts/[postId]/comments/[postId]",
  25. ],
  26. )
  27. def test_invalid_route_args(route_name):
  28. with pytest.raises(ValueError):
  29. get_route_args(route_name)
  30. @pytest.mark.parametrize(
  31. "route_name,expected",
  32. [
  33. ("/events/[year]/[month]/[...slug]", "[...slug]"),
  34. ("pages/shop/[[...slug]]", "[[...slug]]"),
  35. ],
  36. )
  37. def test_catchall_in_route(route_name, expected):
  38. assert catchall_in_route(route_name) == expected
  39. @pytest.mark.parametrize(
  40. "route_name",
  41. [
  42. "/products",
  43. "/products/[category]/[...]/details/[version]",
  44. "[...]",
  45. "/products/details",
  46. ],
  47. )
  48. def test_verify_valid_routes(route_name):
  49. verify_route_validity(route_name)
  50. @pytest.mark.parametrize(
  51. "route_name",
  52. [
  53. "/products/[...]/details/[category]/latest",
  54. "/blog/[...]/post/[year]/latest",
  55. "/products/[...]/details/[...]/[category]/[...]/latest",
  56. "/products/[...]/details/category",
  57. ],
  58. )
  59. def test_verify_invalid_routes(route_name):
  60. with pytest.raises(ValueError):
  61. verify_route_validity(route_name)
  62. @pytest.fixture()
  63. def app():
  64. return App()
  65. @pytest.mark.parametrize(
  66. "route1,route2",
  67. [
  68. ("/posts/[slug]", "/posts/[slug1]"),
  69. ("/posts/[slug]/info", "/posts/[slug1]/info1"),
  70. ("/posts/[slug]/info/[[slug1]]", "/posts/[slug1]/info1/[[slug2]]"),
  71. ("/posts/[slug]/info/[[slug1]]", "/posts/[slug]/info/[[slug2]]"),
  72. ("/posts/[slug]/info/[[...slug1]]", "/posts/[slug1]/info/[[...slug2]]"),
  73. ("/posts/[slug]/info/[[...slug1]]", "/posts/[slug]/info/[[...slug2]]"),
  74. ],
  75. )
  76. def test_check_routes_conflict_invalid(mocker, app, route1, route2):
  77. mocker.patch.object(app, "_pages", {route1: []})
  78. with pytest.raises(ValueError):
  79. app._check_routes_conflict(route2)
  80. @pytest.mark.parametrize(
  81. "route1,route2",
  82. [
  83. ("/posts/[slug]", "/post/[slug1]"),
  84. ("/posts/[slug]", "/post/[slug]"),
  85. ("/posts/[slug]/info", "/posts/[slug]/info1"),
  86. ("/posts/[slug]/info/[[slug1]]", "/posts/[slug]/info1/[[slug1]]"),
  87. ("/posts/[slug]/info/[[slug1]]", "/posts/[slug]/info1/[[slug2]]"),
  88. (
  89. "/posts/[slug]/info/[slug2]/[[slug1]]",
  90. "/posts/[slug]/info1/[slug2]/[[slug1]]",
  91. ),
  92. (
  93. "/posts/[slug]/info/[slug1]/random1/[slug2]/x",
  94. "/posts/[slug]/info/[slug1]/random/[slug4]/x1",
  95. ),
  96. ("/posts/[slug]/info/[[...slug1]]", "/posts/[slug]/info1/[[...slug1]]"),
  97. ("/posts/[slug]/info/[[...slug1]]", "/posts/[slug]/info1/[[...slug2]]"),
  98. ("/posts/[slug]/info/[...slug1]", "/posts/[slug]/info1/[...slug1]"),
  99. ("/posts/[slug]/info/[...slug1]", "/posts/[slug]/info1/[...slug2]"),
  100. ],
  101. )
  102. def test_check_routes_conflict_valid(mocker, app, route1, route2):
  103. mocker.patch.object(app, "_pages", {route1: []})
  104. # test that running this does not throw an error.
  105. app._check_routes_conflict(route2)