如何测试“POST”通过带有动态参数的 url 的方法(Django Rest Framework)?

发布于 2025-01-14 01:49:21 字数 2382 浏览 2 评论 0原文

我需要在我的应用程序中测试一些 API 端点,

即我想使用 Django Rest Framework 使用“toggle_bookmark/”创建“书签” url

如何替换的值在我的 test_boomark.py 文件中?

这是我的 urls.py 文件:

from django.urls import path, include
from rest_framework import routers
from api import bookmark, daily_exercise, like, upload
from api.exercise import ExerciseViewSet, exercise_user_info, toggle_done
from api.plan import PlanViewSet
from api.practice_session import PracticeSessionViewSet
from api.practice_session_exercise import PracticeSessionExerciseViewSet
app_name = "api"

router = routers.DefaultRouter()
router.register(r"exercise", ExerciseViewSet)
router.register(r"plan", PlanViewSet)
router.register(r"practice-session", PracticeSessionViewSet)
router.register(r"practice-session-exercise", PracticeSessionExerciseViewSet)


urlpatterns = [
    path("", include(router.urls)),
    path("upload-file", upload.upload_file, name="upload_file"),
    path("exercise_user_info/<user_plan_uid>/<practice_session_exercise_uid>", exercise_user_info, name="get_exercise_user_info"),
    path("toggle_bookmark/<exercise_id>", bookmark.toggle_bookmark, name="toggle_bookmark"),
    path("toggle_daily_exercise/<exercise_id>", daily_exercise.toggle_daily_exercise, name="toggle_daily_exercise"),
    path("toggle_like/<exercise_id>", like.toggle_like, name="toggle_like"),
    path("toggle_done/<user_plan_uid>/<practice_session_exercise_uid>", toggle_done, name="toggle_done"),
]

这是我的 test_bookmark.py 文件:

import json
from django.urls import reverse
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase, APIClient
from rest_framework import status
from core.models import User, Bookmark


class BookmarkTests(APITestCase):
    def test_bookmark_api(self):
        """ Bookmark API Test """
        user = User.objects.create_user(email='[email protected]', name='lauren')
        client = APIClient()
        client.force_authenticate(user=user)
        client.post('toggle_bookmark/<exercise_id>',{"exercise_id":"123123","exercise":"exerciese1"},format="json")
        assert response.status_code == 201

I need to test some API endpoints in my app

I.e. I want to create "Bookmark" using Django Rest Framework using "toggle_bookmark/<exercise_id>" url

How can I substitute a value for <exercise_id> in my test_boomark.py file?

This is my urls.py file:

from django.urls import path, include
from rest_framework import routers
from api import bookmark, daily_exercise, like, upload
from api.exercise import ExerciseViewSet, exercise_user_info, toggle_done
from api.plan import PlanViewSet
from api.practice_session import PracticeSessionViewSet
from api.practice_session_exercise import PracticeSessionExerciseViewSet
app_name = "api"

router = routers.DefaultRouter()
router.register(r"exercise", ExerciseViewSet)
router.register(r"plan", PlanViewSet)
router.register(r"practice-session", PracticeSessionViewSet)
router.register(r"practice-session-exercise", PracticeSessionExerciseViewSet)


urlpatterns = [
    path("", include(router.urls)),
    path("upload-file", upload.upload_file, name="upload_file"),
    path("exercise_user_info/<user_plan_uid>/<practice_session_exercise_uid>", exercise_user_info, name="get_exercise_user_info"),
    path("toggle_bookmark/<exercise_id>", bookmark.toggle_bookmark, name="toggle_bookmark"),
    path("toggle_daily_exercise/<exercise_id>", daily_exercise.toggle_daily_exercise, name="toggle_daily_exercise"),
    path("toggle_like/<exercise_id>", like.toggle_like, name="toggle_like"),
    path("toggle_done/<user_plan_uid>/<practice_session_exercise_uid>", toggle_done, name="toggle_done"),
]

Here is my test_bookmark.py file:

import json
from django.urls import reverse
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase, APIClient
from rest_framework import status
from core.models import User, Bookmark


class BookmarkTests(APITestCase):
    def test_bookmark_api(self):
        """ Bookmark API Test """
        user = User.objects.create_user(email='[email protected]', name='lauren')
        client = APIClient()
        client.force_authenticate(user=user)
        client.post('toggle_bookmark/<exercise_id>',{"exercise_id":"123123","exercise":"exerciese1"},format="json")
        assert response.status_code == 201

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随波逐流 2025-01-21 01:49:21

您计算 URL 的反转,因此:

from django.urls import reverse

# …

client.post(
    reverse('api:toggle_like', kwargs={'exercise_id': '123123'}),
    {'exercise': 'exerciese1'},
    format='json'
)

这里 api:toggle_like 是路径的名称apiapp_name< /code> 应该用作前缀),并且 kwargs=... 包含一个带有参数名称值的字典。

You calculate the reverse of the URL, so:

from django.urls import reverse

# …

client.post(
    reverse('api:toggle_like', kwargs={'exercise_id': '123123'}),
    {'exercise': 'exerciese1'},
    format='json'
)

Here api:toggle_like is the name of the path (with api the app_name that should be used as prefix), and kwargs=… contains a dictionary with the values for the parameter names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文