미해결
파이썬 사용자를 위한 웹개발 입문 A to Z Django + Bootstrap
이런 오류가 납니다ㅠㅠ tests.py 코드 입니다 from django.test import TestCase , Client from bs4 import BeautifulSoup from .models import Post , Category , Tag from django.utils import timezone from django.contrib.auth.models import User def create_category (name= 'life' , description= '' ): category , is_created = Category.objects.get_or_create( name =name , description =description , ) category.slug = category.name.replace( '' , '-' ).replace( '/' , '' ) category.save() return category def create_tag (name= 'some tag' ): tag , is_created = Tag.objects.get_or_create( name =name ) tag.slug = tag.name.replace( '' , '-' ).replace( '/' , '' ) tag.save() return tag def create_post (title , content , author , category= None ): blog_post = Post.objects.create( title =title , content =content , created =timezone.now() , author =author , category =category , ) return blog_post class TestModel(TestCase): def setUp ( self ): self .client = Client() self .author_000 = User.objects.create( username = 'smith' , password = 'nopassword' ) def test_category ( self ): category = create_category() post_000 = create_post( title = 'The first post' , content = 'Hello World. We are the world.' , author = self .author_000 , category =category , ) self .assertEqual(category.post_set.count() , 1 ) def test_tag ( self ): tag_000 = create_tag( name = 'bad_guy' ) tag_001 = create_tag( name = 'america' ) post_000 = create_post( title = 'The first post' , content = 'Hello World. We are the world.' , author = self .author_000 , ) post_000.tags.add(tag_000) post_000.tags.add(tag_001) post_000.save() post_001 = create_post( title = 'Stay Fool, Stay Hungry' , content = 'Story about Steve Jobs' , author = self .author_000 ) post_001.tags.add(tag_001) post_001.save() self .assertEqual(post_000.tags.count() , 2 ) self .assertEqual(tag_001.post_set.count() , 2 ) self .assertEqual(tag_001.post_set.first() , post_000) self .assertEqual(tag_001.post_set.last() , post_001) def test_post ( self ): category=create_category( ) post_000 = create_post( title = 'The first post' , content = 'Hello World. We are the world.' , author = self .author_000 , category =category , ) class TestView(TestCase): def setUp ( self ): self .client = Client() self .author_000 = User.objects.create_user( username = 'smith' , password = 'nopassword' ) self .user_obama = User.objects.create_user( username = 'obama' , password = 'nopassword' ) def check_navbar ( self , soup): navbar = soup.find( 'div' , id = 'navbar' ) self .assertIn( 'Blog' , navbar.text) self .assertIn( 'About me' , navbar.text) def check_right_side ( self , soup): category_card = soup.find( 'div' , id = 'category-card' ) self .assertIn( '미분류 (1)' , category_card.text) # 미분류 (1) 있어야 함 self .assertIn( '정치/사회 (1)' , category_card.text) # 정치/사회 (1) 있어야 함 def test_post_list_no_post ( self ): response = self .client.get( '/blog/' ) self .assertEqual(response.status_code , 200 ) soup = BeautifulSoup(response.content , 'html.parser' ) title = soup.title self .assertIn(title.text , 'Blog' ) self .check_navbar(soup) self .assertEqual(Post.objects.count() , 0 ) self .assertIn( '아직 게시물이 없습니다.' , soup.body.text) def test_post_list_with_post ( self ): tag_america = create_tag( name = 'america' ) post_000 = create_post( title = 'The first post' , content = 'Hello World. We are the world.' , author = self .author_000 , ) post_000.tags.add(tag_america) post_000.save() post_001 = create_post( title = 'The first post' , content = 'Second Second Second' , author = self .author_000 , category =create_category( name = '정치/사회' ) ) post_001.tags.add(tag_america) post_001.save() self .assertGreater(Post.objects.count() , 0 ) response = self .client.get( '/blog/' ) self .assertEqual(response.status_code , 200 ) soup = BeautifulSoup(response.content , 'html.parser' ) body = soup.body self .assertNotIn( '아직 게시물이 없습니다.' , body.text) self .assertIn(post_000.title , body.text) post_000_read_more_btn = body.find( 'a' , id = 'read-more-post-{}' .format(post_000.pk)) self .assertEqual(post_000_read_more_btn[ 'href' ] , post_000.get_absolute_url()) self .check_right_side(soup) # main_div에는 main_div = soup.find( 'div' , id = 'main_div' ) self .assertIn( '정치/사회' , main_div.text) # '정치/사회' 있어야 함 self .assertIn( '미분류' , main_div.text) # '미분류' 있어야 함 # Tag post_card_000 = main_div.find( 'div' , id = 'post-card-{}' .format(post_000.pk)) self .assertIn( '#america' , post_card_000.text) #Tag가 해당 post의 card마다 있다. def test_post_detail ( self ): category_politics = create_category( name = '정치/사회' ) post_000 = create_post( title = 'The first post' , content = 'Hello World. We are the world.' , author = self .author_000 , category =category_politics ) tag_america = create_tag( name = 'america' ) post_000.tags.add(tag_america) post_000.save() post_001 = create_post( title = 'The first post' , content = 'Second Second Second' , author = self .author_000 , ) self .assertGreater(Post.objects.count() , 0 ) post_000_url = post_000.get_absolute_url() self .assertEqual(post_000_url , '/blog/{}/' .format(post_000.pk)) response = self .client.get(post_000_url) self .assertEqual(response.status_code , 200 ) soup = BeautifulSoup(response.content , 'html.parser' ) title = soup.title self .assertEqual(title.text , '{} - Blog' .format(post_000.title)) self .check_navbar(soup) body = soup.body main_div = body.find( 'div' , id = 'main_div' ) self .assertIn(post_000.title , main_div.text) self .assertIn(post_000.author.username , main_div.text) self .assertIn(post_000.content , main_div.text) self .check_right_side(soup) # Tag self .assertIn( '#america' , main_div.text) # Tag가 해당 post의 card마다 있다. self .assertIn(category_politics.name , main_div.text) #category가 main_div에 있다. self .assertNotIn( 'EDIT' , main_div.text) #EDIT 버튼이 로그인하지 않은 경우 보이지 않는다. login_success = self .client.login( username = 'smith' , password = 'nopassword' ) #login을 한 경우에는 self .assertTrue(login_success) response = self .client.get(post_000_url) self .assertEqual(response.status_code , 200 ) soup = BeautifulSoup(response.content , 'html.parser' ) main_div = soup.find( 'div' , id = 'main-div' ) self .assertEqual(post_000.author , self .author_000) #post.author와 login한 사용자가 동일하다면 self .assertIn( 'EDIT' , main_div.text) #EDIT 버튼이 있다. # 다른 사람인 경우에는 없다. login_success = self .client.login( username = 'obama' , password = 'nopassword' ) #login을 한 경우에는 self .assertTrue(login_success) response = self .client.get(post_000_url) self .assertEqual(response.status_code , 200 ) soup = BeautifulSoup(response.content , 'html.parser' ) main_div = soup.find( 'div' , id = 'main-div' ) self .assertEqual(post_000.author , self .author_000) # post.author와 login한 사용자가 동일하다면 self .assertNotIn( 'EDIT' , main_div.text) # EDIT 버튼이 있다. def test_post_list_by_category ( self ): category_politics = create_category( name = '정치/사회' ) post_000 = create_post( title = 'The first post' , content = 'Hello World. We are the world.' , author = self .author_000 , ) post_001 = create_post( title = 'The first post' , content = 'Second Second Second' , author = self .author_000 , category =category_politics ) response = self .client.get(category_politics.get_absolute_url()) self .assertEqual(response.status_code , 200 ) soup = BeautifulSoup(response.content , 'html.parser' ) main_div = soup.find( 'div' , id = 'main_div' ) self .assertNotIn( '미분류' , main_div.text) self .assertIn(category_politics.name , main_div.text) def test_post_list_no_category (self): category_politics = create_category( name = '정치/사회' ) post_000 = create_post( title = 'The first post' , content = 'Hello World. We are the world.' , author =self.author_000 , ) post_001 = create_post( title = 'The first post' , content = 'Second Second Second' , author =self.author_000 , category =category_politics ) response = self.client.get( '/blog/category/_none/' ) self.assertEqual(response.status_code , 200 ) soup = BeautifulSoup(response.content , 'html.parser' ) main_div = soup.find( 'div' , id = 'main_div' ) self.assertIn( '미분류' , main_div.text) self.assertNotIn(category_politics.name , main_div.text) def test_tag_page (self): tag_000 = create_tag( name = 'bad_guy' ) tag_001 = create_tag( name = 'america' ) post_000 = create_post( title = 'The first post' , content = 'Hello World. We are the world.' , author =self.author_000 , ) post_000.tags.add(tag_000) post_000.tags.add(tag_001) post_000.save() post_001 = create_post( title = 'Stay Fool, Stay Hungry' , content = 'Story about Steve Jobs' , author =self.author_000 ) post_001.tags.add(tag_001) post_001.save() response = self.client.get(tag_000.get_absolute_url()) self.assertEqual(response.status_code , 200 ) soup = BeautifulSoup(response.content , 'html.parser' ) main_div = soup.find( 'div' , id = 'main-div' ) blog_h1 = main_div.find( 'h1' , id = 'blog-list-title' ) self.assertIn( '#{}' .format(tag_000.name) , blog_h1.text) self.assertIn(post_000.title , main_div.text) self.assertNotIn(post_001.title , main_div.text)
오은비
2020-08-16T14:28:03.519Z
댓글 1
좋아요 0
조회수 404