def test_get_url_with_empty_prefix(self): class OwnerViewSet(viewsets.ListModelMixin, viewsets.GenericViewSet): model = Owner router = SimpleRouter() router.register("", OwnerViewSet, "") urlpattern = router.get_urls()[0] self.assertEqual(self.get_regex(urlpattern), "^$")
from django.urls import include, path from django_sorcery.routers import SimpleRouter from . import views router = SimpleRouter() router.register("", views.PollsViewSet) app_name = "polls" urlpatterns = [path("", include(router.urls))]
def test_router(self): router = SimpleRouter() router.register("prefix", OwnerViewSet) self.assertEqual(router.registry, [("prefix", OwnerViewSet, "owner")]) self.assertEqual( router.get_routes(OwnerViewSet), [ Route( url="^{prefix}{trailing_slash}$", mapping={ "get": "list", "post": "create" }, name="{basename}-list", detail=False, initkwargs={"suffix": "List"}, ), Route( url="^{prefix}/new{trailing_slash}$", mapping={"get": "new"}, name="{basename}-new", detail=False, initkwargs={"suffix": "New"}, ), Route( url="^{prefix}/{lookup}{trailing_slash}$", mapping={ "get": "retrieve", "post": "update", "put": "update", "patch": "update", "delete": "destroy", }, name="{basename}-detail", detail=True, initkwargs={"suffix": "Instance"}, ), Route( url="^{prefix}/{lookup}/edit{trailing_slash}$", mapping={"get": "edit"}, name="{basename}-edit", detail=True, initkwargs={"suffix": "Instance"}, ), Route( url="^{prefix}/{lookup}/delete{trailing_slash}$", mapping={ "get": "confirm_destroy", "post": "destroy" }, name="{basename}-destroy", detail=True, initkwargs={"suffix": "Instance"}, ), Route( url="^{prefix}/{lookup}/custom{trailing_slash}$", mapping={"get": "custom"}, name="{basename}-custom", detail=True, initkwargs={}, ), ], ) urls = {url.name: url for url in router.urls} self.assertEqual(self.get_regex(urls["owner-custom"]), "^prefix/(?P<id>[^/.]+)/custom/$") self.assertEqual(self.get_regex(urls["owner-destroy"]), "^prefix/(?P<id>[^/.]+)/delete/$") self.assertEqual(self.get_regex(urls["owner-detail"]), "^prefix/(?P<id>[^/.]+)/$") self.assertEqual(self.get_regex(urls["owner-edit"]), "^prefix/(?P<id>[^/.]+)/edit/$") self.assertEqual(self.get_regex(urls["owner-list"]), "^prefix/$") self.assertEqual(self.get_regex(urls["owner-new"]), "^prefix/new/$")