from django.test import TestCase, Client
from bs4 import BeautifulSoup
from .models import Post
class TestView(TestCase):
def setUp(self):
self.client = Client()
def test_post_list(self):
pass
# 1.1 ํฌ์คํธ ๋ชฉ๋ก ํ์ด์ง(post list)๋ฅผ ์ฐ๋ค
response = self.client.get('/blog/')
# 1.2 ์ ์์ ์ผ๋ก ํ์ด์ง๊ฐ ๋ก๋๋๋ค.
self.assertEqual(response.status_code, 200)
# 1.3 ํ์ด์ง์ ํ์ดํ์ Blog๋ผ๋ ๋ฌธ๊ตฌ๊ฐ ์๋ค.
soup = BeautifulSoup(response.content, 'html.parser')
self.assertIn('Blog', soup.title.text)
# # 1.4 NavBar๊ฐ ์๋ค
navbar = soup.nav
# # 1.5 Blog, About me๋ผ๋ ๋ฌธ๊ตฌ๊ฐ Nav์ ์๋ค.
self.assertIn('Blog', navbar.text)
self.assertIn('About me', navbar.text)
# 2.1 ๊ฒ์๋ฌผ์ด ํ๋๋ ์์ ๋
self.assertEqual(Post.objects.count(), 0)
# 2.2 ๋ฉ์ธ ์์ญ์ "์์ง ๊ฒ์๋ฌผ์ด ์์ต๋๋ค" ๋ผ๋ ๋ฌธ๊ตฌ๊ฐ ๋์จ๋ค.
main_area = soup.find('div', id='main-area')
self.assertIn('์์ง ๊ฒ์๋ฌผ์ด ์์ต๋๋ค.', main_area.text)
# 3.1 ๋ง์ฝ ๊ฒ์๋ฌผ์ด 2๊ฐ ์๋ค๋ฉด,
post_001 = Post.objects.create(
title='์ฒซ๋ฒ์งธ ํฌ์คํธ ์
๋๋ค.',
content='Hello, World. We are the World.',
)
post_002 = Post.objects.create(
title='๋๋ฒ์งธ ํฌ์คํธ ์
๋๋ค.',
content='์๋
์ฌ๋ฌ๋ถ, ๋๋ ์ฌ๋ฌ๋ถ์ ์ผ๋ถ์ผ.',
)
self.assertEqual(Post.objects.count(), 2)
# 3.2 ํฌ์คํธ ๋ชฉ๋ก ํ์ด์ง๋ฅผ ์๋ก ๊ณ ์นจํ์ ๋,
response = self.client.get('/blog/')
soup = BeautifulSoup(response.content, 'html.parser')
# 3.3 ๋ฉ์ธ ์์ญ์ ํฌ์คํธ 2๊ฐ์ ํ์ดํ์ด ์กด์ฌํ๋ค
main_area = soup.find('div', id='main-area')
self.assertIn(post_001.title, main_area.text)
self.assertIn(post_002.title, main_area.text)
# 3.4 "์์ง ๊ฒ์๋ฌผ์ด ์์ต๋๋ค" ๋ผ๋ ๋ฌธ๊ตฌ๊ฐ ์์ด์ผ ํ๋ค
self.assertNotIn('์์ง ๊ฒ์๋ฌผ์ด ์์ต๋๋ค.', main_area.text)