from django.utils import unittest
from django.test.client import RequestFactory

from djadmin2 import views

class ViewTest(unittest.TestCase):
    def setUp(self):
        self.factory = RequestFactory()


class IndexViewTest(ViewTest):
    def test_response_ok(self):
        request = self.factory.get('/admin/blog/post/')
        response = views.IndexView.as_view()(request)
        self.assertEqual(response.status_code, 200)


class ModelListViewTest(ViewTest):
    pass


class ModelDetailViewTest(ViewTest):
    pass


class ModelEditFormViewTest(ViewTest):
    pass


class ModelAddFormViewTest(ViewTest):
    pass


class ModelDeleteViewTest(ViewTest):
    pass


from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test import TestCase, Client

from .models import Post


class BaseIntegrationTest(TestCase):
    """
    Base TestCase for integration tests.
    """
    def setUp(self):
        self.client = Client()
        self.user = get_user_model()(username='user', is_staff=True,
                                     is_superuser=True)
        self.user.set_password("password")
        self.user.save()
        self.client.login(username='user', password='password')


class AdminIndexTest(BaseIntegrationTest):
    def test_view_ok(self):
        response = self.client.get(reverse("admin2:dashboard"))
        self.assertContains(response, reverse("admin2:blog_post_index"))


class PostListTest(BaseIntegrationTest):
    def test_view_ok(self):
        post = Post.objects.create(title="a_post_title", body="body")
        response = self.client.get(reverse("admin2:blog_post_index"))
        self.assertContains(response, post.title)


class PostDetailViewTest(BaseIntegrationTest):
    def test_view_ok(self):
        post = Post.objects.create(title="a_post_title", body="body")
        response = self.client.get(reverse("admin2:blog_post_detail",
                                           args=(post.pk, )))
        self.assertContains(response, post.title)


class PostCreateViewTest(BaseIntegrationTest):
    def test_view_ok(self):
        response = self.client.get(reverse("admin2:blog_post_create"))
        self.assertEqual(response.status_code, 200)

    def test_create_post(self):
        response = self.client.post(reverse("admin2:blog_post_create"),
                                    {"title": "a_post_title",
                                     "body": "a_post_body"},
                                    follow=True)
        self.assertTrue(Post.objects.filter(title="a_post_title").exists())
        post = Post.objects.get(title="a_post_title")
        self.assertRedirects(response, reverse("admin2:blog_post_detail",
                                               args=(post.pk, )))


class PostDeleteViewTest(BaseIntegrationTest):
    def test_view_ok(self):
        post = Post.objects.create(title="a_post_title", body="body")
        response = self.client.get(reverse("admin2:blog_post_delete",
                                           args=(post.pk, )))
        self.assertContains(response, post.title)

    def test_delete_post(self):
        post = Post.objects.create(title="a_post_title", body="body")
        response = self.client.post(reverse("admin2:blog_post_delete",
                                            args=(post.pk, )))
        self.assertRedirects(response, reverse("admin2:blog_post_index"))
        self.assertFalse(Post.objects.filter(pk=post.pk).exists())

add more tests for the example app