Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Merged
merged 3 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions codecove.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
comment: #this is a top-level key
layout: " diff, flags, files"
behavior: default
require_changes: false # if true: only post the comment if coverage changes
require_base: false # [true :: must have a base report to post]
require_head: true # [true :: must have a head report to post]
hide_project_coverage: false # [true :: only show coverage on the git diff aka patch coverage]]
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='djangorestframework-hybridrouter',
version='0.1.4',
version='0.1.5',
packages=[
'hybridrouter',
],
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