Skip to content

Commit

Permalink
Финальное задание
Browse files Browse the repository at this point in the history
  • Loading branch information
esaviv committed Jan 13, 2023
1 parent 5525897 commit 72d6578
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions yatube/posts/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,46 +136,45 @@ def test_edit_post(self):
self.assertRedirects(response, reverse(
"posts:post_detail", kwargs={"post_id": post.pk}))

def test_create_post(self):
"""При отправке валидной формы со страницы создания поста
reverse('posts:create_post')
создаётся новая запись в базе данных.
"""
small_gif = (
b'\x47\x49\x46\x38\x39\x61\x02\x00'
b'\x01\x00\x80\x00\x00\x00\x00\x00'
b'\xFF\xFF\xFF\x21\xF9\x04\x00\x00'
b'\x00\x00\x00\x2C\x00\x00\x00\x00'
b'\x02\x00\x01\x00\x00\x02\x02\x0C'
b'\x0A\x00\x3B'
)
uploaded = SimpleUploadedFile(
name='small.gif',
content=small_gif,
content_type='image/gif'
)
form_data = {
"text": "Текст из формы",
"group": self.group.id,
"image": uploaded,
}
response = self.authorized_author.post(
reverse("posts:post_create"),
data=form_data,
follow=True
)
self.assertEqual(Post.objects.count(), 2)
post = Post.objects.first()
post_attributes = {
post.author: self.auth,
post.text: form_data["text"],
post.group: self.group,
post.image: f"posts/{form_data['image'].name}"
}
for attribute, value in post_attributes.items():
with self.subTest(attribute=attribute):
self.assertEqual(attribute, value)

def test_create_post(self):
"""При отправке валидной формы со страницы создания поста
reverse('posts:create_post')
создаётся новая запись в базе данных.
"""
small_gif = (
b'\x47\x49\x46\x38\x39\x61\x02\x00'
b'\x01\x00\x80\x00\x00\x00\x00\x00'
b'\xFF\xFF\xFF\x21\xF9\x04\x00\x00'
b'\x00\x00\x00\x2C\x00\x00\x00\x00'
b'\x02\x00\x01\x00\x00\x02\x02\x0C'
b'\x0A\x00\x3B'
)
uploaded = SimpleUploadedFile(
name='small.gif',
content=small_gif,
content_type='image/gif'
)
form_data = {
"text": "Текст из формы",
"group": self.group.id,
"image": uploaded,
}
response = self.authorized_author.post(
reverse("posts:post_create"),
data=form_data,
follow=True
)
self.assertEqual(Post.objects.count(), 2)
post = Post.objects.first()
post_attributes = {
post.author: self.auth,
post.text: form_data["text"],
post.group: self.group,
post.image: f"posts/{form_data['image'].name}"
}
for attribute, value in post_attributes.items():
with self.subTest(attribute=attribute):
self.assertEqual(attribute, value)

self.assertRedirects(response, reverse(
"posts:profile", kwargs={"username": self.auth}))
self.assertRedirects(response, reverse(
"posts:profile", kwargs={"username": self.auth}))

0 comments on commit 72d6578

Please sign in to comment.