From 82514162d456f8f29f507f1881f7559571819fc7 Mon Sep 17 00:00:00 2001 From: Fournet Enzo Date: Sun, 19 May 2024 11:50:47 +0200 Subject: [PATCH 1/3] adding working register methode + test --- hybridrouter/hybridrouter.py | 10 ++++- test/app/hybridroutertest/tests.py | 67 +++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/hybridrouter/hybridrouter.py b/hybridrouter/hybridrouter.py index 5492ec0..3bf136d 100644 --- a/hybridrouter/hybridrouter.py +++ b/hybridrouter/hybridrouter.py @@ -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 @@ -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): diff --git a/test/app/hybridroutertest/tests.py b/test/app/hybridroutertest/tests.py index c158b56..d11c1f8 100644 --- a/test/app/hybridroutertest/tests.py +++ b/test/app/hybridroutertest/tests.py @@ -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: @@ -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): @@ -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) @@ -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): @@ -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() From 0395480362e385d3118e92c0bf9dd57460beb525 Mon Sep 17 00:00:00 2001 From: Fournet Enzo Date: Sun, 19 May 2024 15:33:33 +0200 Subject: [PATCH 2/3] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ffd5ee0..2800b23 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name='djangorestframework-hybridrouter', - version='0.1.4', + version='0.1.5', packages=[ 'hybridrouter', ], From 601a9d93a0dd708cea38f53b0c9a41aa3ee05468 Mon Sep 17 00:00:00 2001 From: Fournet Enzo Date: Sun, 19 May 2024 15:37:05 +0200 Subject: [PATCH 3/3] Create codecove.yml --- codecove.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 codecove.yml diff --git a/codecove.yml b/codecove.yml new file mode 100644 index 0000000..dc3c4b8 --- /dev/null +++ b/codecove.yml @@ -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]] \ No newline at end of file