from django.test import TestCase


class BlogViewsTestCase(TestCase):
    def setUp(self):

from django import test
from django.core.urlresolvers import reverse

from blog.models import Post, Category


class BlogViewsTestCase(test.TestCase):
    def setUp(self):
        # Add parent category and post category
        parent = Category(name='Writing', parent=None)
        parent.save()
        category = Category(name='Thoughts', parent=parent)
        category.save()
        # Create a draft
        _post = Post(title='Random thoughts of the author',
                    body='Thoughts turned to words.', category=category)
        _post.save()
        self.draft = _post
        # Publish a post
        post = Post(title='New thoughts from without',
                    body='A post fit to be published!', category=category)
        post.save()
        post.publish()
        self.post = post

        self.client = test.Client()

    def test_index(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)
        posts = response.context['posts']
        self.assertNotIn(self.draft, posts)
        self.assertIn(self.post, posts)

    def test_post_view(self):
        post_url = reverse('blog:post', kwargs=dict(pid=self.post.id,
                                                    slug=self.post.slug))
        response = self.client.get(post_url)
        self.assertEqual(response.status_code, 200)
        post = response.context['post']
        posts = response.context['posts']
        self.assertEqual(post, self.post)
        self.assertEqual(posts.count(), 0)

    def test_draft_view(self):
        draft_url = reverse('blog:post', kwargs=dict(pid=self.draft.id,
                                                     slug=self.draft.slug))
        response = self.client.get(draft_url)
        self.assertEqual(response.status_code, 404)

Add tests for blog index view and post view
Add tests for blog index view and post view

- from django.test import TestCase
+ from django import test
+ from django.core.urlresolvers import reverse
+ 
+ from blog.models import Post, Category
  
  
- class BlogViewsTestCase(TestCase):
+ class BlogViewsTestCase(test.TestCase):
      def setUp(self):
+         # Add parent category and post category
+         parent = Category(name='Writing', parent=None)
+         parent.save()
+         category = Category(name='Thoughts', parent=parent)
+         category.save()
+         # Create a draft
+         _post = Post(title='Random thoughts of the author',
+                     body='Thoughts turned to words.', category=category)
+         _post.save()
+         self.draft = _post
+         # Publish a post
+         post = Post(title='New thoughts from without',
+                     body='A post fit to be published!', category=category)
+         post.save()
+         post.publish()
+         self.post = post
  
+         self.client = test.Client()
+ 
+     def test_index(self):
+         response = self.client.get('/')
+         self.assertEqual(response.status_code, 200)
+         posts = response.context['posts']
+         self.assertNotIn(self.draft, posts)
+         self.assertIn(self.post, posts)
+ 
+     def test_post_view(self):
+         post_url = reverse('blog:post', kwargs=dict(pid=self.post.id,
+                                                     slug=self.post.slug))
+         response = self.client.get(post_url)
+         self.assertEqual(response.status_code, 200)
+         post = response.context['post']
+         posts = response.context['posts']
+         self.assertEqual(post, self.post)
+         self.assertEqual(posts.count(), 0)
+ 
+     def test_draft_view(self):
+         draft_url = reverse('blog:post', kwargs=dict(pid=self.draft.id,
+                                                      slug=self.draft.slug))
+         response = self.client.get(draft_url)
+         self.assertEqual(response.status_code, 404)
+ 
Add tests for blog index view and post view
## Code Before:
from django.test import TestCase


class BlogViewsTestCase(TestCase):
    def setUp(self):

## Instruction:
Add tests for blog index view and post view
## Code After:
from django import test
from django.core.urlresolvers import reverse

from blog.models import Post, Category


class BlogViewsTestCase(test.TestCase):
    def setUp(self):
        # Add parent category and post category
        parent = Category(name='Writing', parent=None)
        parent.save()
        category = Category(name='Thoughts', parent=parent)
        category.save()
        # Create a draft
        _post = Post(title='Random thoughts of the author',
                    body='Thoughts turned to words.', category=category)
        _post.save()
        self.draft = _post
        # Publish a post
        post = Post(title='New thoughts from without',
                    body='A post fit to be published!', category=category)
        post.save()
        post.publish()
        self.post = post

        self.client = test.Client()

    def test_index(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)
        posts = response.context['posts']
        self.assertNotIn(self.draft, posts)
        self.assertIn(self.post, posts)

    def test_post_view(self):
        post_url = reverse('blog:post', kwargs=dict(pid=self.post.id,
                                                    slug=self.post.slug))
        response = self.client.get(post_url)
        self.assertEqual(response.status_code, 200)
        post = response.context['post']
        posts = response.context['posts']
        self.assertEqual(post, self.post)
        self.assertEqual(posts.count(), 0)

    def test_draft_view(self):
        draft_url = reverse('blog:post', kwargs=dict(pid=self.draft.id,
                                                     slug=self.draft.slug))
        response = self.client.get(draft_url)
        self.assertEqual(response.status_code, 404)
