from django.test import TestCase
from django.contrib.auth.models import User
from wiki.models import Page
from django.utils.text import slugify

# Create your tests here.
class WikiPageTest(TestCase):

    def test_edit(self):
        user = User.objects.create_user(username='admin', password='djangopony')
        self.client.login(username='admin', password='djangopony')

        page = Page.objects.create(title="My Test Page", content="test", author=user)
        page.save()
        edit = {
            'title': 'testing title',
            'content': 'testing content'
        }

        response = self.client.post('/%s/' %slugify(page.title), edit)
        updated = Page.objects.get(title = edit['title'])

        self.assertEqual(response.status_code, 302)
        self.assertEqual(updated.title, edit['title'])

    def test_page(self):
        user = User.objects.create_user(username='admin', password='djangopony')
        self.client.login(username='admin', password='djangopony')

        page = Page.objects.create(title="My Test Page", content="test", author=user)
        page.save()

        response = self.client.get('/%s/' %slugify(page.title))

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'test')

    def test_create(self):
        user = User.objects.create_user(username='admin', password='djangopony')
        self.client.login(username='admin', password='djangopony')

        new = {
            'title': 'testing title',
            'content': 'testing content'
        }

        response = self.client.post('/wiki/new/', new)
        updated = Page.objects.get(title = new['title'])

        self.assertEqual(response.status_code, 302)
        self.assertEqual(updated.title, new['title'])
'''
Steps to writing a test
    1. Set up your test data
    2. Make a request (GET, POST)
    3a. Check if response matches what we expect
    3b. Check if database matches what we expect
'''
[((10, 15, 10, 80), 'django.contrib.auth.models.User.objects.create_user', 'User.objects.create_user', (), '', False, 'from django.contrib.auth.models import User\n'), ((13, 15, 13, 85), 'wiki.models.Page.objects.create', 'Page.objects.create', (), '', False, 'from wiki.models import Page\n'), ((21, 18, 21, 57), 'wiki.models.Page.objects.get', 'Page.objects.get', (), '', False, 'from wiki.models import Page\n'), ((27, 15, 27, 80), 'django.contrib.auth.models.User.objects.create_user', 'User.objects.create_user', (), '', False, 'from django.contrib.auth.models import User\n'), ((30, 15, 30, 85), 'wiki.models.Page.objects.create', 'Page.objects.create', (), '', False, 'from wiki.models import Page\n'), ((39, 15, 39, 80), 'django.contrib.auth.models.User.objects.create_user', 'User.objects.create_user', (), '', False, 'from django.contrib.auth.models import User\n'), ((48, 18, 48, 56), 'wiki.models.Page.objects.get', 'Page.objects.get', (), '', False, 'from wiki.models import Page\n'), ((20, 44, 20, 63), 'django.utils.text.slugify', 'slugify', ({(20, 52, 20, 62): 'page.title'}, {}), '(page.title)', False, 'from django.utils.text import slugify\n'), ((33, 43, 33, 62), 'django.utils.text.slugify', 'slugify', ({(33, 51, 33, 61): 'page.title'}, {}), '(page.title)', False, 'from django.utils.text import slugify\n')]