gpt4 book ai didi

python - 单元测试 get_absolute_url django

转载 作者:行者123 更新时间:2023-11-28 21:19:32 24 4
gpt4 key购买 nike

我对 Django 和编写测试还很陌生。我目前正在从事一个项目,该项目有两个模型,Project 和 Technologies。项目模型与技术模型具有多对多关系。我有一个覆盖 get_queryset 方法的 View 。在我的报道报告中,这种方法是我唯一的失误。我正在寻找有关如何为此方法编写单元测试的指导。感谢您抽出时间回答我的问题。

模型.py

from django.db import models 
from django.template.defaultfilters import slugify
from django.urls import reverse

class Technologies(models.Model):

name = models.CharField(max_length=64, unique=True)
slug = models.SlugField()

def __str__(self):
return self.slug

def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Technologies, self).save(*args, **kwargs)

class Project(models.Model):

title = models.CharField(max_length=64)
description = models.CharField(max_length=128)
repo = models.URLField()
slug = models.SlugField()

image = models.ImageField(
upload_to='project_images',
default='project_images/default_project.png')
technologies = models.ManyToManyField(Technologies)

def __str__(self):
return self.title

def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Project, self).save(*args, **kwargs)

def get_absolute_url(self):
return reverse("projects:detail", kwargs={"slug": self.slug})

View .py

from django.views.generic import DetailView, ListView 
from .models import Project

class ProjectsListView(ListView):

model = Project

class ProjectDetailView(DetailView):

model = Project

class TechnologiesListView(ListView):

model = Project
allow_empty = False # If list is empty 404

def get_queryset(self):

return Project.objects.filter(technologies__slug=self.kwargs['slug'])

网址.py

from django.urls import path 
from . import views

app_name = "projects"
urlpatterns = [

path('', view=views.ProjectsListView.as_view(), name='list'),
path('<slug>', view=views.ProjectDetailView.as_view(), name='detail'),
path('tagged/<slug>', view=views.TechnologiesListView.as_view(), name='tech_list'),

]

所以我的问题是这个方法:

def get_queryset(self): 

return Project.objects.filter(technologies__slug=self.kwargs['slug'])

测试 View .py

import pytest
from django.test import RequestFactory
from django.urls import reverse
from mixer.backend.django import mixer

from projects.views import ProjectDetailView, ProjectsListView, TechnologiesListView


@pytest.mark.django_db
class TestProjectViews:

def test_projects_by_technologies_view(self):
tech = mixer.blend('projects.Technologies')
path = reverse('projects:tech_list', kwargs={'slug': tech.slug})
request = RequestFactory().get(path)
response = TechnologiesListView.as_view()(request)
assert response.status_code == 200, 'Should be status code 200'

当我运行这个测试时,我得到:

    def get_queryset(self):
> return Project.objects.filter(technologies__slug=self.kwargs['slug'])
E KeyError: 'slug'

这是因为 kwargs 不在请求工厂生成的请求中吗?或者这是一个数据库问题?我对此非常迷茫和困惑,非常感谢任何明智的话。

最佳答案

写出您的问题可以更好地分解问题,这真是令人惊讶。这是我的解决方案。

def test_projects_by_technologies_view(self):
self.tech = mixer.blend('projects.Technologies')
self.proj = mixer.blend(Project, technologies=self.tech)
self.path = reverse('projects:tech_list', kwargs={'slug': self.tech.slug})
self.request = RequestFactory().get(self.path)
self.response = TechnologiesListView.as_view()(self.request, slug=self.tech.slug)
assert self.response.status_code == 200, 'Should be status code 200'

关于python - 单元测试 get_absolute_url django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53470445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com