I am writing a test case for a Posts app in my Django project. I have defined views for adding a new post, listing the posts, view details of the post, edit a post and delete a post. Adding a new post redirects the page to the detailed view of that page and deleting a post redirects the page to the list view. While writing tests for the same, I am getting https response code 302 for the new post as expected, but for the delete post, I am only getting an https response 200 while it should be 302.
Adding necessary code below. Please feel free to ask any more code related to this if needed.
Is there any concept that I am missing? Any lead is welcome.
Thanks in advance.
Test.py
from django.test import TestCase
from .models import Post
from django.urls import reverse, reverse_lazy
class PostTest(TestCase):
def setUp(self):
'''This function inserts
dummy data into database
to check during testing'''
self.post = Post.objects.create(title='Test', content='abcd', author='testauthor@testing.test')
def test_post_content(self):
post_object = Post.objects.get(pk=1)
expected_object_title = f'{post_object.title}'
expected_object_content = f'{post_object.content}'
expected_object_author = f'{post_object.author}'
self.assertEqual(expected_object_title,'Test')
self.assertEqual(expected_object_content, 'abcd')
return self.assertEqual(expected_object_author, 'testauthor@testing.test')
def test_post_list_view(self):
response = self.client.get(reverse('lists'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Test')
self.assertTemplateUsed(response, 'index.html')
def test_post_details(self):
response = self.client.get('/detail/1/')
no_response = self.client.get('/detail/100/')
self.assertEqual(response.status_code, 200)
self.assertEqual(no_response.status_code, 404)
self.assertContains(response, 'abcd')
self.assertTemplateUsed(response, 'detailed.html')
def test_newpost_view(self):
response = self.client.post(reverse('addpost'),{
'title' : 'New title',
'content' : 'New Content',
'author' : 'test@gmail.com',
})
self.assertEqual(response.status_code, 302)
#self.assertContains(response,'New Content')
def test_post_update_view(self):
response = self.client.post(reverse('update', args='1'),{
'title': 'Updated title',
'content' : 'Updated content',
})
self.assertContains(response, 'Updated title')
self.assertEqual(response.status_code, 200)
def test_postdelete_view(self):
response = self.client.get(reverse('delete',args='1'))
self.assertEqual(response.status_code,302)
views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView,UpdateView, DeleteView
from django.urls import reverse,reverse_lazy
from .models import Post
class ListingView(ListView):
model = Post
template_name = 'index.html'
context_object_name = 'list_of_posts'
class DetailedListView(DetailView):
model = Post
template_name = 'detailed.html'
class AddPostView(CreateView):
model = Post
template_name = 'addpost.html'
fields = ['title','author', 'content']
class PostUpdateView(UpdateView):
model = Post
template_name = 'update.html'
fields = ['title', 'author', 'content']
class DeletePostView(DeleteView):
model = Post
template_name = 'deleteview.html'
success_url = reverse_lazy('lists')
Models.py
from django.db import models
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
author = models.EmailField()
def __str__(self):
return self.title[:30]
def get_absolute_url(self):
return reverse('detailed', args=[str(self.id)])
response = self.client.get(reverse('delete',args='1'))
Using GET
request, you are asking for the template 'deleteview.html'
which needs to be filled to delete the object. Hence, the status code 200
is correct.
You should use POST
(or DELETE
) request. Which will delete the object and redirect to success_url = reverse_lazy('lists')
. Hence, you will get the status code 302
.
To clearly understand it, look at the code of DeleteView.