简体   繁体   English

在Django测试中,为什么我需要使用 <Model> .objects.get()而不是返回的内容 <Model> .objects.create()?

[英]In Django tests, why do I need to use <Model>.objects.get() instead of what was returned by <Model>.objects.create()?

Although this is perhaps not a minimal example, I'm building on How to test an API endpoint with Django-rest-framework using Django-oauth-toolkit for authentication . 尽管这可能不是一个最小的示例,但我正在构建如何使用Django-oauth-toolkit进行身份验证的Django-rest-framework测试API端点 I have a model Session which I'd like to be able to update using the Django REST framework. 我有一个模型Session ,希望能够使用Django REST框架进行更新。 To this end I wrote the following view: 为此,我编写了以下视图:

from rest_framework import generics

from oauth2_provider.contrib.rest_framework import OAuth2Authentication

from ..models import Session
from ..serializers import SessionSerializer


class SessionDetail(generics.UpdateAPIView):
    authentication_classes = [OAuth2Authentication]
    queryset = Session.objects.all()
    serializer_class = SessionSerializer

where the default permission_classes are set in settings.py : settings.pysettings.py默认的permission_classes位置:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
}

I've written the following test case: 我编写了以下测试用例:

import json

from django.contrib.auth.models import User
from datetime import timedelta

from django.urls import reverse
from django.test import TestCase, Client
from django.utils import timezone

from rest_framework import status

from ..models import Family, Session, SessionType, SessionCategory

from oauth2_provider.models import get_application_model, get_access_token_model


Application = get_application_model()
AccessToken = get_access_token_model()


class OAuth2TestCase(TestCase):
    def setUp(self):
        self.username = "test_user"
        self.password = "123456"

        self.user = User.objects.create_user(username=self.username, password=self.password)

        self.application = Application(
            name="Test Application",
            redirect_uris="http://localhost",
            user=self.user,
            client_type=Application.CLIENT_CONFIDENTIAL,
            authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
        )
        self.application.save()


        self.TOKEN = '1234567890'
        self.token = AccessToken.objects.create(
            user=self.user,
            token=self.TOKEN,
            application=self.application,
            scope='read write',
            expires=timezone.now() + timedelta(days=1)
        )

        self._create_session()

    def _create_session(self):
        self.session_category = SessionCategory.objects.create(
            name='Birth Prep')
        self.session_type = SessionType.objects.create(
            title='Initial Postpartum Lactation',
            category=self.session_category)
        self.family = Family.objects.create()
        self.session = Session.objects.create(
            family=self.family,
            session_type=self.session_type)

    def test_make_patch_request(self):
        data = {'feedback_expert': 'Yes'}
        url = reverse('sessions-feedback', kwargs={'pk': self.session.id})

        auth = f"Bearer {self.TOKEN}"
        response = self.client.patch(
            path=url,
            data=json.dumps(data),
            content_type="application/json",
            HTTP_AUTHORIZATION=auth,
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertEqual(Session.objects.get(pk=self.session.id).feedback_expert, 'Yes')
        # self.assertEqual(self.session.feedback_expert, 'Yes')

The test passes, but what puzzles me is that if I comment in the last line and instead do 测试通过了,但令我困惑的是,如果我在最后一行发表评论,而是

self.assertEqual(self.session.feedback_expert, 'Yes')

I get a test failure: 我测试失败:

(venv) Kurts-MacBook-Pro:lucy-web kurtpeek$ python manage.py test lucy_web.tests.test_feedback
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_make_patch_request (lucy_web.tests.test_feedback.OAuth2TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/lucy_web/tests/test_feedback.py", line 295, in test_make_patch_request
    self.assertEqual(self.session.feedback_expert, 'Yes')
AssertionError: '' != 'Yes'
+ Yes

----------------------------------------------------------------------
Ran 1 test in 1.299s

FAILED (failures=1)
Destroying test database for alias 'default'...

Apparently, the self.session doesn't have its feedback_expert field updated yet, whereas the one returned by Session.objects.get(pk=self.session.id) does. 显然, self.session尚未更新其feedback_expert字段,而Session.objects.get(pk=self.session.id)返回的字段却可以更新。 Any idea why this is the case? 知道为什么会这样吗?

It turns out I needed to call refresh_from_db() on the self.session . 事实证明,我需要在self.session上调用refresh_from_db() So the following works: 因此,以下工作:

    self.session.refresh_from_db()
    self.assertEqual(self.session.feedback_expert, 'Yes')

Now the test passes. 现在测试通过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM