From 91a74068b388c16c87e1700dbf27484922e747cf Mon Sep 17 00:00:00 2001 From: David Graves Date: Wed, 30 Nov 2022 22:14:55 -0600 Subject: [PATCH] additional basename tests --- tests/test_routers.py | 102 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 7 deletions(-) diff --git a/tests/test_routers.py b/tests/test_routers.py index 2ef2bd541..aa7d4ca30 100644 --- a/tests/test_routers.py +++ b/tests/test_routers.py @@ -21,6 +21,11 @@ class RouterTestModel(models.Model): text = models.CharField(max_length=200) +class BasenameTestModel(models.Model): + uuid = models.CharField(max_length=20) + text = models.CharField(max_length=200) + + class NoteSerializer(serializers.HyperlinkedModelSerializer): url = serializers.HyperlinkedIdentityField(view_name='routertestmodel-detail', lookup_field='uuid') @@ -42,6 +47,11 @@ class KWargedNoteViewSet(viewsets.ModelViewSet): lookup_url_kwarg = 'text' +class BasenameViewSet(viewsets.ModelViewSet): + queryset = BasenameTestModel.objects.all() + serializer_class = None + + class MockViewSet(viewsets.ModelViewSet): queryset = None serializer_class = None @@ -156,7 +166,7 @@ class TestSimpleRouter(URLPatternsTestCase, TestCase): def test_register_after_accessing_urls(self): self.router.register(r'notes', NoteViewSet) assert len(self.router.urls) == 2 # list and detail - self.router.register(r'notes_bis', NoteViewSet) + self.router.register(r'notes_bis', NoteViewSet, basename='notes_bis') assert len(self.router.urls) == 4 @@ -483,15 +493,93 @@ class TestViewInitkwargs(URLPatternsTestCase, TestCase): assert initkwargs['basename'] == 'routertestmodel' -class TestDuplicateBasename(URLPatternsTestCase, TestCase): - def test_exception_for_duplicate_basename(self): - class NoteViewSet(viewsets.ModelViewSet): - queryset = RouterTestModel.objects.all() - +class TestDuplicateBasename(TestCase): + def test_conflicting_autogenerated_basenames(self): + """ + Ensure 2 routers with the same model, and no basename specified + throws an ImproperlyConfigured exception + """ self.router = SimpleRouter(trailing_slash=False) self.router.register(r'notes', NoteViewSet) + with pytest.raises(ImproperlyConfigured): + self.router.register(r'notes_kwduplicate', KWargedNoteViewSet) + with pytest.raises(ImproperlyConfigured): self.router.register(r'notes_duplicate', NoteViewSet) - self.router.register(r'notes_duplicate_2', NoteViewSet, basename='note_duplicate') \ No newline at end of file + def test_conflicting_mixed_basenames(self): + """ + Ensure 2 routers with the same model, and no basename specified on 1 + throws an ImproperlyConfigured exception + """ + self.router = SimpleRouter(trailing_slash=False) + self.router.register(r'notes', NoteViewSet) + + with pytest.raises(ImproperlyConfigured): + self.router.register(r'notes_kwduplicate', KWargedNoteViewSet, basename='routertestmodel') + + with pytest.raises(ImproperlyConfigured): + self.router.register(r'notes_duplicate', NoteViewSet, basename='routertestmodel') + + def test_nonconflicting_mixed_basenames(self): + """ + Ensure 2 routers with the same model, and a distinct basename + specified on the second router does not fail + """ + self.router = SimpleRouter(trailing_slash=False) + self.router.register(r'notes', NoteViewSet) + self.router.register(r'notes_kwduplicate', KWargedNoteViewSet, basename='routertestmodel_kwduplicate') + self.router.register(r'notes_duplicate', NoteViewSet, basename='routertestmodel_duplicate') + + def test_conflicting_specified_basename(self): + """ + Ensure 2 routers with the same model, and the same basename specified + on both throws an ImproperlyConfigured exception + """ + self.router = SimpleRouter(trailing_slash=False) + self.router.register(r'notes', NoteViewSet, basename='notes') + + with pytest.raises(ImproperlyConfigured): + self.router.register(r'notes_kwduplicate', KWargedNoteViewSet, basename='notes') + + with pytest.raises(ImproperlyConfigured): + self.router.register(r'notes_duplicate', KWargedNoteViewSet, basename='notes') + + def test_nonconflicting_specified_basename(self): + """ + Ensure 2 routers with the same model, and a distinct basename specified + on each does not throw an exception + """ + self.router = SimpleRouter(trailing_slash=False) + self.router.register(r'notes', NoteViewSet, basename='notes') + self.router.register(r'notes_kwduplicate', KWargedNoteViewSet, basename='notes_kwduplicate') + self.router.register(r'notes_duplicate', NoteViewSet, basename='notes_duplicate') + + def test_nonconflicting_specified_basename_different_models(self): + """ + Ensure 2 routers with different models, and a distinct basename specified + on each does not throw an exception + """ + self.router = SimpleRouter(trailing_slash=False) + self.router.register(r'notes', NoteViewSet, basename='notes') + self.router.register(r'notes_basename', BasenameViewSet, basename='notes_basename') + + def test_conflicting_specified_basename_different_models(self): + """ + Ensure 2 routers with different models, and a conflicting basename specified + throws an exception + """ + self.router = SimpleRouter(trailing_slash=False) + self.router.register(r'notes', NoteViewSet) + with pytest.raises(ImproperlyConfigured): + self.router.register(r'notes_basename', BasenameViewSet, basename='routertestmodel') + + def test_nonconflicting_autogenerated_basename_different_models(self): + """ + Ensure 2 routers with different models, and a distinct basename specified + on each does not throw an exception + """ + self.router = SimpleRouter(trailing_slash=False) + self.router.register(r'notes', NoteViewSet) + self.router.register(r'notes_basename', BasenameViewSet)