I'm pretty new to django, and writing tests. I'm currently working on a project that has two models, Project and Technologies. The project models has a many to many relationship with the technologies model. I have a view that overrides the get_queryset method. On my coverage report this method is my only miss. I am looking for guidance on how to write a unit test for this method. Thank you for taking the time to answer my question.
models.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})
views.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'])
urls.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'),
]
Aucun commentaire:
Enregistrer un commentaire