访问中间件值以测试Django DetailView
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了访问中间件值以测试Django DetailView相关的知识,希望对你有一定的参考价值。
我正在编写一个DetailView测试,通过访问中间件中的值来查询get_object()。这适用于公司应用程序和公司模型。每个用户都在公司。
要在整个项目中访问公司,我通过自定义中间件在请求上设置当前用户的Company.uuid。
中间件
from django.utils.deprecation import MiddlewareMixin
class DynamicCompanyUUIDMiddleware(MiddlewareMixin):
""" Adds the current organization's UUID from the current user."""
def process_request(self, request):
try:
company_uuid = request.user.company_uuid
except:
company_uuid = None
request.company_uuid = company_uuid
通过我用于其他公司视图的Mixin在CompanyDetailView的get_object()方法中使用它。
MI信
class CompanyMixin(LoginRequiredMixin, SetHeadlineMixin):
model = Company
def get_object(self):
return get_object_or_404(
self.model,
uuid=self.request.user.company_uuid)
测试
我正在尝试编写的测试是:
from django.test import RequestFactory
from django.urls import reverse, resolve
from test_plus.test import TestCase
from ..models import Company
from ..views import CompanyDetailView
class BaseCompanyTestCase(TestCase):
def setUp(self):
self.user = self.make_user()
self.object = Company.objects.create(owner=self.user, name="testcompany")
self.user.company_uuid = self.object.uuid
self.factory = RequestFactory()
class TestCompanyDetailView(BaseCompanyTestCase):
def setUp(self):
super(TestCompanyDetailView, self).setUp()
self.client.login(username="testuser", password="password")
self.view = CompanyDetailView()
self.view.object = self.object
request = self.factory.get(reverse('companies:detail'))
request.user = self.user
request.company_uuid = self.user.company_uuid
response = CompanyDetailView.as_view()(request)
self.assertEqual(response.status_code, 200)
def test_get_headline(self):
self.assertEqual(
self.view.get_headline(),
'%s Members' % self.object.name
结果
这导致404找不到testuser的公司。
走过它:
- 我创建了用户
- 为这个新的testuser创建公司
- 设置user.company_uuid
- 这应该允许mixin访问company_uuid
- 因此,在请求中返回用户的公司
然而,我并没有像404那样回归公司。
题
我在哪里错了?在此先感谢您的帮助。
回答
我正在混合Django的Client&RequestFactory。我已经纠正了上面的代码是正确的。
答案
我正在混合Django的Client&RequestFactory。走开后,我在下面想出来 -
from django.test import RequestFactory
from django.urls import reverse, resolve
from test_plus.test import TestCase
from ..models import Company
from ..views import CompanyDetailView
class BaseCompanyTestCase(TestCase):
def setUp(self):
self.user = self.make_user()
self.object = Company.objects.create(owner=self.user, name="testcompany")
self.user.company_uuid = self.object.uuid
self.factory = RequestFactory()
class TestCompanyDetailView(BaseCompanyTestCase):
def setUp(self):
super(TestCompanyDetailView, self).setUp()
self.client.login(username="testuser", password="password")
self.view = CompanyDetailView()
self.view.object = self.object
request = self.factory.get(reverse('companies:detail'))
request.user = self.user
request.company_uuid = self.user.company_uuid
response = CompanyDetailView.as_view()(request)
self.assertEqual(response.status_code, 200)
def test_get_headline(self):
self.assertEqual(
self.view.get_headline(),
'%s Members' % self.object.name
以上是关于访问中间件值以测试Django DetailView的主要内容,如果未能解决你的问题,请参考以下文章
在 Django 视图中检索 Select2 多值以在后端使用