通过PK从pytest-django到达时,如何解决找不到的问题?

发布于 2025-01-20 11:52:20 字数 1835 浏览 4 评论 0原文

我的 django-pytest 有问题
我正在使用,djnago-rest-framework
测试细节有问题。如下面的代码所示,我输入了相同的详细信息:detail1、detail2 和detail3 代码。但是,只有detail1成功,detail2、detail3表示找不到'/api/v1/stats/1/'。执行删除时也会出现这种情况。我很好奇这个错误的原因和解决方案。

在此处输入图片说明

// tests/test_apis.py
import json
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from stats.models import Stats


class StatsApiTests(APITestCase):
    def setUp(self):
        Stats.objects.get_or_create(blockshots=1, memo='test1')
        Stats.objects.get_or_create(blockshots=2, memo='test2')
        self.create_read_url = reverse('api:stats:stats-list')
        self.read_update_delete_url = reverse('api:stats:stats-detail', kwargs={'pk': '1'})

    def test_detail1(self):
        response = self.client.get(self.read_update_delete_url)
        data = json.loads(response.content)
        content = {
            'blockshots': 1,
            'memo': 'test1',
        }
        self.assertEqual(data, content)

    def test_detail2(self):
        response = self.client.get(self.read_update_delete_url)
        data = json.loads(response.content)
        content = {
            'blockshots': 1,
            'memo': 'test1',
        }
        self.assertEqual(data, content)

    def test_detail3(self):
        response = self.client.get(self.read_update_delete_url)
        data = json.loads(response.content)
        content = {
            'blockshots': 1,
            'memo': 'test1',
        }
        self.assertEqual(data, content)

    def test_list(self):
        response = self.client.get(self.create_read_url)
        self.assertContains(response, 'test1')
        self.assertContains(response, 'test2')

I have a problem with django-pytest
I'm using, djnago-rest-framework
There is a problem testing the details. As shown in the code below, I entered the same details, detail1, detail2, and detail3 codes. However, only detail1 succeeds and detail2, detail3 indicates that '/api/v1/stats/1/' could not be found. It also occurs when implementing delete. I am curious about the cause and solution of this error.

enter image description here

// tests/test_apis.py
import json
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from stats.models import Stats


class StatsApiTests(APITestCase):
    def setUp(self):
        Stats.objects.get_or_create(blockshots=1, memo='test1')
        Stats.objects.get_or_create(blockshots=2, memo='test2')
        self.create_read_url = reverse('api:stats:stats-list')
        self.read_update_delete_url = reverse('api:stats:stats-detail', kwargs={'pk': '1'})

    def test_detail1(self):
        response = self.client.get(self.read_update_delete_url)
        data = json.loads(response.content)
        content = {
            'blockshots': 1,
            'memo': 'test1',
        }
        self.assertEqual(data, content)

    def test_detail2(self):
        response = self.client.get(self.read_update_delete_url)
        data = json.loads(response.content)
        content = {
            'blockshots': 1,
            'memo': 'test1',
        }
        self.assertEqual(data, content)

    def test_detail3(self):
        response = self.client.get(self.read_update_delete_url)
        data = json.loads(response.content)
        content = {
            'blockshots': 1,
            'memo': 'test1',
        }
        self.assertEqual(data, content)

    def test_list(self):
        response = self.client.get(self.create_read_url)
        self.assertContains(response, 'test1')
        self.assertContains(response, 'test2')

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

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

发布评论

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

评论(1

梦幻之岛 2025-01-27 11:52:20

很难知道您对read_update_delete_url的实际实现,因此,我假设它正在通过主键查找资源。在这种情况下,您只需在URL中添加主要

stat_one, _ = Stats.objects.get_or_create(blockshots=1, memo='test1')
stat_two, _ = Stats.objects.get_or_create(blockshots=2, memo='test2')

self.read_update_delete_url = reverse('api:stats:stats-detail', kwargs={'pk': stat_one.pk})

键“ rel =“ nofollow noreferrer”> get_or_create 返回对象和对象的状态(是否创建)。您可以将对象的ID用作反向函数的参数。

Its hard to know what your actual implementation for read_update_delete_url, hence I assume it is looking up the resource by primary key. In that case, you can simply add the primary key in the url like this:

stat_one, _ = Stats.objects.get_or_create(blockshots=1, memo='test1')
stat_two, _ = Stats.objects.get_or_create(blockshots=2, memo='test2')

self.read_update_delete_url = reverse('api:stats:stats-detail', kwargs={'pk': stat_one.pk})

Basically, get_or_create returns the object and the state of the object (created or not). You can use the object's id as the parameter of reverse function.

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