From 337e4db650397203dd42d48d2ee7a2f56f8d635f Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 16 Feb 2023 00:04:25 -0600 Subject: [PATCH] update urls.pyes to use path() and add some tests --- countdown/urls.py | 5 +++-- dispatch/urls.py | 24 ++++++++++-------------- dr_botzo/urls.py | 25 +++++++++++++------------ facts/urls.py | 5 ++--- karma/urls.py | 12 ++++++------ pi/urls.py | 5 +++-- races/urls.py | 6 +++--- tests/test_countdown_api.py | 26 ++++++++++++++++++++++++++ tests/test_dispatch_api.py | 29 +++++++++++++++++++++++++++++ tests/test_pi_views.py | 2 +- tests/test_races_views.py | 29 +++++++++++++++++++++++++++++ 11 files changed, 125 insertions(+), 43 deletions(-) create mode 100644 tests/test_countdown_api.py create mode 100644 tests/test_dispatch_api.py create mode 100644 tests/test_races_views.py diff --git a/countdown/urls.py b/countdown/urls.py index 0083170..8282257 100644 --- a/countdown/urls.py +++ b/countdown/urls.py @@ -1,5 +1,6 @@ """URL patterns for the countdown views.""" -from django.conf.urls import include, url +from django.conf.urls import include +from django.urls import path from rest_framework.routers import DefaultRouter from countdown.views import CountdownItemViewSet @@ -8,5 +9,5 @@ router = DefaultRouter() router.register(r'items', CountdownItemViewSet) urlpatterns = [ - url(r'^api/', include(router.urls)), + path('api/', include(router.urls)), ] diff --git a/dispatch/urls.py b/dispatch/urls.py index 2e2310d..792972c 100644 --- a/dispatch/urls.py +++ b/dispatch/urls.py @@ -1,20 +1,16 @@ """URL patterns for the dispatcher API.""" +from django.urls import path -from django.conf.urls import url - -from dispatch.views import (DispatchMessage, DispatchMessageByKey, DispatcherList, DispatcherDetail, - DispatcherDetailByKey, DispatcherActionList, DispatcherActionDetail) - +from dispatch.views import (DispatcherActionDetail, DispatcherActionList, DispatcherDetail, DispatcherDetailByKey, + DispatcherList, DispatchMessage, DispatchMessageByKey) urlpatterns = [ - url(r'^api/dispatchers/$', DispatcherList.as_view(), name='dispatch_api_dispatchers'), - url(r'^api/dispatchers/(?P[0-9]+)/$', DispatcherDetail.as_view(), name='dispatch_api_dispatcher_detail'), - url(r'^api/dispatchers/(?P[0-9]+)/message$', DispatchMessage.as_view(), name='dispatch_api_dispatch_message'), - url(r'^api/dispatchers/(?P[A-Za-z-]+)/$', DispatcherDetailByKey.as_view(), - name='dispatch_api_dispatcher_detail'), - url(r'^api/dispatchers/(?P[A-Za-z-]+)/message$', DispatchMessageByKey.as_view(), - name='dispatch_api_dispatch_message'), + path('api/dispatchers/', DispatcherList.as_view(), name='dispatch_api_dispatchers'), + path('api/dispatchers//', DispatcherDetail.as_view(), name='dispatch_api_dispatcher_detail'), + path('api/dispatchers//message', DispatchMessage.as_view(), name='dispatch_api_dispatch_message'), + path('api/dispatchers//', DispatcherDetailByKey.as_view(), name='dispatch_api_dispatcher_detail'), + path('api/dispatchers//message', DispatchMessageByKey.as_view(), name='dispatch_api_dispatch_message'), - url(r'^api/actions/$', DispatcherActionList.as_view(), name='dispatch_api_actions'), - url(r'^api/actions/(?P[0-9]+)/$', DispatcherActionDetail.as_view(), name='dispatch_api_action_detail'), + path('api/actions/', DispatcherActionList.as_view(), name='dispatch_api_actions'), + path('api/actions//', DispatcherActionDetail.as_view(), name='dispatch_api_action_detail'), ] diff --git a/dr_botzo/urls.py b/dr_botzo/urls.py index c01df9b..c7c2163 100644 --- a/dr_botzo/urls.py +++ b/dr_botzo/urls.py @@ -1,7 +1,8 @@ """General/baselite/site-wide URLs.""" from adminplus.sites import AdminSitePlus -from django.conf.urls import include, url +from django.conf.urls import include from django.contrib import admin +from django.urls import path from django.views.generic import TemplateView admin.site = AdminSitePlus() @@ -9,17 +10,17 @@ admin.sites.site = admin.site admin.autodiscover() urlpatterns = [ - url(r'^$', TemplateView.as_view(template_name='index.html'), name='index'), + path('', TemplateView.as_view(template_name='index.html'), name='index'), - url(r'^countdown/', include('countdown.urls')), - url(r'^dice/', include('dice.urls')), - url(r'^dispatch/', include('dispatch.urls')), - url(r'^itemsets/', include('facts.urls')), - url(r'^karma/', include('karma.urls')), - url(r'^markov/', include('markov.urls')), - url(r'^pi/', include('pi.urls')), - url(r'^races/', include('races.urls')), - url(r'^weather/', include('weather.urls')), + path('countdown/', include('countdown.urls')), + path('dice/', include('dice.urls')), + path('dispatch/', include('dispatch.urls')), + path('itemsets/', include('facts.urls')), + path('karma/', include('karma.urls')), + path('markov/', include('markov.urls')), + path('pi/', include('pi.urls')), + path('races/', include('races.urls')), + path('weather/', include('weather.urls')), - url(r'^admin/', admin.site.urls), + path('admin/', admin.site.urls), ] diff --git a/facts/urls.py b/facts/urls.py index 1268182..4182d8c 100644 --- a/facts/urls.py +++ b/facts/urls.py @@ -1,5 +1,4 @@ """URL patterns for the facts web views.""" -from django.conf.urls import url from django.urls import path from facts.views import factcategory_detail, index, rpc_get_facts, rpc_get_random_fact @@ -8,6 +7,6 @@ urlpatterns = [ path('rpc//', rpc_get_facts, name='weather_rpc_get_facts'), path('rpc//random/', rpc_get_random_fact, name='weather_rpc_get_random_fact'), - url(r'^$', index, name='facts_index'), - url(r'^(?P.+)/$', factcategory_detail, name='facts_factcategory_detail'), + path('', index, name='facts_index'), + path('/', factcategory_detail, name='facts_factcategory_detail'), ] diff --git a/karma/urls.py b/karma/urls.py index c9fe94c..408e827 100644 --- a/karma/urls.py +++ b/karma/urls.py @@ -1,16 +1,16 @@ """URL patterns for the karma views.""" - -from django.conf.urls import url, include +from django.conf.urls import include +from django.urls import path from rest_framework.routers import DefaultRouter -from karma.views import key_detail, index, KarmaKeyViewSet +from karma.views import KarmaKeyViewSet, index, key_detail router = DefaultRouter() router.register(r'keys', KarmaKeyViewSet) urlpatterns = [ - url(r'^$', index, name='karma_index'), - url(r'^key/(?P.+)/', key_detail, name='karma_key_detail'), + path('', index, name='karma_index'), + path('key//', key_detail, name='karma_key_detail'), - url(r'^api/', include(router.urls)), + path('api/', include(router.urls)), ] diff --git a/pi/urls.py b/pi/urls.py index 3c7596b..0d27ed1 100644 --- a/pi/urls.py +++ b/pi/urls.py @@ -1,5 +1,6 @@ """URL patterns for the pi views.""" -from django.conf.urls import include, url +from django.conf.urls import include +from django.urls import path from rest_framework.routers import DefaultRouter from pi.views import PiLogViewSet @@ -8,5 +9,5 @@ router = DefaultRouter() router.register(r'simulations', PiLogViewSet) urlpatterns = [ - url(r'^api/', include(router.urls)), + path('api/', include(router.urls)), ] diff --git a/races/urls.py b/races/urls.py index 845cb01..f35964a 100644 --- a/races/urls.py +++ b/races/urls.py @@ -1,8 +1,8 @@ -from django.conf.urls import url +from django.urls import path from races.views import index, race_detail urlpatterns = [ - url(r'^$', index, name='races_index'), - url(r'^race/(?P[A-Za-z0-9]+)/$', race_detail, name='race_detail'), + path('', index, name='races_index'), + path('race//', race_detail, name='race_detail'), ] diff --git a/tests/test_countdown_api.py b/tests/test_countdown_api.py new file mode 100644 index 0000000..9f68fee --- /dev/null +++ b/tests/test_countdown_api.py @@ -0,0 +1,26 @@ +"""Test the countdown package's webservice.""" +from django.contrib.auth.models import User +from django.utils.timezone import now +from rest_framework.status import HTTP_200_OK +from rest_framework.test import APITestCase + +from countdown.models import CountdownItem + + +class CountdownAPITest(APITestCase): + """Test countdown DRF views.""" + + def setUp(self): + """Do pre-test stuff.""" + self.client = self.client_class() + self.user = User.objects.create(username='test') + self.client.force_authenticate(user=self.user) + + def test_items_retrieval(self): + """Test that the items endpoint returns objects.""" + CountdownItem.objects.create(at_time=now()) + + resp = self.client.get('/countdown/api/items/') + + self.assertEqual(resp.status_code, HTTP_200_OK) + self.assertEqual(len(resp.json()), CountdownItem.objects.count()) diff --git a/tests/test_dispatch_api.py b/tests/test_dispatch_api.py new file mode 100644 index 0000000..5ffcf20 --- /dev/null +++ b/tests/test_dispatch_api.py @@ -0,0 +1,29 @@ +"""Test the dispatch package's webservice.""" +from django.contrib.auth.models import User +from rest_framework.status import HTTP_200_OK +from rest_framework.test import APITestCase + +from dispatch.models import Dispatcher, DispatcherAction + + +class DispatchAPITest(APITestCase): + """Test dispatch DRF views.""" + + def setUp(self): + """Do pre-test stuff.""" + self.client = self.client_class() + self.user = User.objects.create(username='test') + self.client.force_authenticate(user=self.user) + + def test_dispatch_object_retrieval(self): + """Test that the list endpoints returns objects.""" + dispatcher = Dispatcher.objects.create() + DispatcherAction.objects.create(dispatcher=dispatcher) + + resp = self.client.get('/dispatch/api/dispatchers/') + self.assertEqual(resp.status_code, HTTP_200_OK) + self.assertEqual(len(resp.json()), Dispatcher.objects.count()) + + resp = self.client.get('/dispatch/api/actions/') + self.assertEqual(resp.status_code, HTTP_200_OK) + self.assertEqual(len(resp.json()), DispatcherAction.objects.count()) diff --git a/tests/test_pi_views.py b/tests/test_pi_views.py index 0403f73..9c740a5 100644 --- a/tests/test_pi_views.py +++ b/tests/test_pi_views.py @@ -22,4 +22,4 @@ class PiAPITest(APITestCase): resp = self.client.post('/pi/api/simulations/simulate/') self.assertEqual(resp.status_code, HTTP_201_CREATED) - self.assertEqual(PiLog.objects.count(), 2) # 2 because 0 entry and the real entry + self.assertEqual(PiLog.objects.count(), 2) diff --git a/tests/test_races_views.py b/tests/test_races_views.py new file mode 100644 index 0000000..ed827eb --- /dev/null +++ b/tests/test_races_views.py @@ -0,0 +1,29 @@ +"""Test the race views.""" +from unittest import mock + +from django.test import TestCase +from django.utils.timezone import now + +from races.models import Race, Racer, RaceUpdate + + +class RaceViewTest(TestCase): + """Test races views.""" + + def setUp(self): + """Do pre-test stuff.""" + self.client = self.client_class() + + def test_race_display(self): + """Test the display of race info when it's been somewhat populated.""" + race = Race.objects.create(key='test') + racer_a = Racer.objects.create(nick='hank', race=race) + racer_b = Racer.objects.create(nick='bob', race=race) + RaceUpdate.objects.create(race=race, racer=racer_a, update="test 1") + RaceUpdate.objects.create(race=race, racer=racer_b, update="test 2") + RaceUpdate.objects.create(race=race, racer=racer_a, update="test 3") + + resp = self.client.get('/races/race/test/') + self.assertIn(b'hank — test 1', resp.content) + self.assertIn(b'bob — test 2', resp.content) + self.assertIn(b'hank — test 3', resp.content)