Write a test to check if a post has been removed from the old group
I need to check that the post has disappeared from the old group page. I need to get my old group for her slack.
old_group_response = self.authorized_client.get( reverse('group_list', args=(self.group.slug,)) ) And compare that
old_group_response.context['page_obj'].paginator.count equals zero. This means that there are no posts in our old group. And check another new group, that there is 1 post there. How can I write it in the right way? I know what rubbish is written here (I'm just learning), this is what I was able to sketch.
from django import forms from django.test import Client, TestCase from django.urls import reverse
from ..models import Group, Post, User
NEW_POST = reverse('posts:post_create')
class PostFormTests(TestCase): @classmethod def setUpClass(cls): super().setUpClass() cls.author_auth = User.objects.create_user(username='test auth') cls.not_author = User.objects.create_user(username='Not Author') cls.group = Group.objects.create( title='Test group_title', slug='test_slug', description='Test description')
def setUp(self):
self.authorized_client = Client()
self.authorized_client.force_login(PostFormTests.author_auth)
self.authorized_client_not_author = Client()
self.authorized_client_not_author.force_login(
PostFormTests.not_author)
def test_post_old_group_response(self):
""" Check if a post has been removed from the old group."""
post = Post.objects.create(
group=PostFormTests.group,
author=PostFormTests.author_auth,
text='test post')
group_2 = Group.objects.create(
title='Test group_title2',
slug='test_slug2',
description='Test description2')
posts_count = Post.objects.count()
form_data = {
'text': 'text_post',
'group': group_2.id}
old_group_response = self.authorized_client.get(
reverse('posts:group_list', args=(self.group.slug)),
data=form_data,
follow=True)
self.assertEqual(
old_group_response, reverse(
'posts:post_detail', kwargs={'post_id': post.pk}))
self.assertEqual(Post.objects.count(), posts_count)
self.assertEqual(old_group_response.context[
'page_obj'].paginator.count == 0)