Skip to content

Commit

Permalink
adding working register methode + test
Browse files Browse the repository at this point in the history
  • Loading branch information
enzofrnt committed May 19, 2024
1 parent a64191f commit 8251416
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
10 changes: 8 additions & 2 deletions hybridrouter/hybridrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework.reverse import reverse
from rest_framework.routers import DefaultRouter
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from urllib.parse import urlsplit, urlunsplit

Expand Down Expand Up @@ -30,8 +31,13 @@ def register_view(self, url, view, name):
def register_viewset(self, prefix, viewset, basename=None):
super().register(prefix, viewset, basename)

def register(self, prefix, viewset, basename=None):
raise NotImplementedError("The 'register' method is deprecated. Use 'register_viewset' instead.")
def register(self, prefix, view_class, basename=None):
if issubclass(view_class, ViewSet):
self.register_viewset(prefix, view_class, basename)
elif issubclass(view_class, APIView):
self.register_view(prefix, view_class, basename)
else:
raise ValueError("The class must be a subclass of APIView or ViewSet")

@property
def api_view_urls(self):
Expand Down
67 changes: 51 additions & 16 deletions test/app/hybridroutertest/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def setUp(self):
self.router.register_view(r'^coucou/server', ServerModsView, name='coucou-server')
self.router.register_viewset(r'coucou', ServerConfigViewSet, basename='coucou')

self.router.register_view('prefix1', ServerConfigView, 'basename1')
self.router.register_viewset('prefix2', ServerConfigViewSet, 'basename2')
self.router.register('prefix3', ServerConfigViewSet, 'basename3')
self.router.register('prefix4', ServerConfigView, 'basename4')


self.client = APIClient()

class CommonHybridRouterTests:
Expand Down Expand Up @@ -68,9 +74,36 @@ def test_coucou_viewset_list(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, {'a': 'b'})

def test_router_regsiter_view_deprecated(self):
with self.assertRaises(NotImplementedError):
self.router.register('prefix', 'viewset', 'basename')
def test_router_regsiter_view(self):
url = reverse('basename1')
repsonse = self.client.get(url)
self.assertEqual(repsonse.status_code, 200)
self.assertEqual(repsonse.data, {'config': 'server'})

def test_router_regsiter_viewset(self):
url = reverse('basename2-list')
repsonse = self.client.get(url)
self.assertEqual(repsonse.status_code, 200)
self.assertEqual(repsonse.data, {'a': 'b'})

def test_router_regsiter_a_viewset(self):
url = reverse('basename3-list')
repsonse = self.client.get(url)
self.assertEqual(repsonse.status_code, 200)
self.assertEqual(repsonse.data, {'a': 'b'})


def test_router_resgister_an_api_view(self):
url = reverse('basename4')
repsonse = self.client.get(url)
self.assertEqual(repsonse.status_code, 200)
self.assertEqual(repsonse.data, {'config': 'server'})

def test_router_resgister_not_api_view_not_viewset(self):
class TestNotApiViewNotViewSet() :
pass
with self.assertRaises(ValueError):
self.router.register('prefix', TestNotApiViewNotViewSet, 'basename')

@override_settings(ROOT_URLCONF='hybridroutertest.tests')
class HybridRouterTestCaseWithIntermediaryViews(CommonHybridRouterTests, HybridRouterTests):
Expand Down Expand Up @@ -108,11 +141,16 @@ def test_api_root_view(self):
url = reverse('api-root')
response = self.client.get(url)
attended_data = {
'basename1': response.wsgi_request.build_absolute_uri(reverse('basename1')),
'basename4': response.wsgi_request.build_absolute_uri(reverse('basename4')),
'server-config': response.wsgi_request.build_absolute_uri(reverse('server-config')),
'mods': response.wsgi_request.build_absolute_uri(reverse('mods')),
'coucou': response.wsgi_request.build_absolute_uri(reverse('coucou')),
'coucou-client': response.wsgi_request.build_absolute_uri(reverse('coucou-client')),
'coucou-server': response.wsgi_request.build_absolute_uri(reverse('coucou-server')),

'prefix2': response.wsgi_request.build_absolute_uri(reverse('basename2-list')),
'prefix3': response.wsgi_request.build_absolute_uri(reverse('basename3-list')),
}
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, attended_data)
Expand Down Expand Up @@ -141,22 +179,20 @@ def test_api_root_view(self):
url = reverse('api-root')
response = self.client.get(url)
attended_data = {
'server-config': response.wsgi_request.build_absolute_uri(reverse('server-config')),
'basename1': response.wsgi_request.build_absolute_uri(reverse('basename1')),
'basename4': response.wsgi_request.build_absolute_uri(reverse('basename4')),
'mods-client': response.wsgi_request.build_absolute_uri(reverse('mods-client')),
'mods-server': response.wsgi_request.build_absolute_uri(reverse('mods-server')),
'coucou-client': response.wsgi_request.build_absolute_uri(reverse('coucou-client')),
'coucou-server': response.wsgi_request.build_absolute_uri(reverse('coucou-server')),

'server-config': response.wsgi_request.build_absolute_uri(reverse('server-config')),
'coucou': response.wsgi_request.build_absolute_uri(reverse('coucou-list')),
'coucou-client': response.wsgi_request.build_absolute_uri(reverse('coucou-client')),
'coucou-server': response.wsgi_request.build_absolute_uri(reverse('coucou-server')),
'prefix2': response.wsgi_request.build_absolute_uri(reverse('basename2-list')),
'prefix3': response.wsgi_request.build_absolute_uri(reverse('basename3-list')),
}
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, attended_data)







self.assertEqual(response.data, attended_data)

class TestHybridRouterWithoutSpectacular(unittest.TestCase):

Expand All @@ -175,12 +211,11 @@ def test_import_error(self):
importlib.reload(hybridrouter)
self.assertTrue(hasattr(hybridrouter, 'DRF_SPECTACULAR'))
self.assertFalse(getattr(hybridrouter, 'DRF_SPECTACULAR'))


class HybridRouterTestCaseWithIntermediaryViewsWithoutSpectacular(TestHybridRouterWithoutSpectacular, HybridRouterTestCaseWithIntermediaryViews):
def setUp(cls):
super().setUp()

class HybridRouterTestCaseWithoutIntermediaryViewsWithoutSpectacular(TestHybridRouterWithoutSpectacular, HybridRouterTestCaseWithoutIntermediaryViews):
def setUp(cls):
super().setUp()
Expand Down

0 comments on commit 8251416

Please sign in to comment.