view forums/tests/test_views.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -0500
parents 9e803323a0d0
children 02181fa5ac9d
line wrap: on
line source
"""
Tests for the views in the forums application.

"""
from django.test import TestCase
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

from forums.models import Forum, Topic


class ForumPostTestCase(TestCase):
    fixtures = ['forums.json']

    def setUp(self):
        self.username = 'test_user'
        self.pw = 'password'
        self.user = User.objects.create_user(self.username, '', self.pw)
        self.user.save()
        self.assertTrue(self.client.login(username=self.username,
            password=self.pw))

    def tearDown(self):
        self.client.logout()

    def testBasicForumsTest(self):

        forum_slug = 'shallow-end'
        topic_name = 'A test topic'
        topic_body = 'testing 1, 2, 3...'

        response = self.client.post(
                reverse('forums-new_topic', kwargs={'slug': forum_slug}),
                {'name': topic_name, 'body': topic_body},
                follow=True)

        self.assertEqual(len(response.redirect_chain), 1)

        if response.redirect_chain:
            self.assertEqual(response.redirect_chain[0][0],
                'http://testserver' + reverse('forums-new_topic_thanks',
                    kwargs={'tid': '1'}))
            self.assertEqual(response.redirect_chain[0][1], 302)

        self.assertEqual(response.status_code, 200)

        forum = Forum.objects.get(slug=forum_slug)
        try:
            topic = Topic.objects.get(pk=1)
        except Topic.DoesNotExist:
            self.fail("topic doesn't exist")

        self.assertEqual(topic.forum.pk, forum.pk)
        self.assertEqual(topic.user.pk, self.user.pk)
        self.assertEqual(topic.name, topic_name)
        self.assertEqual(topic.post_count, 1)

        post = topic.last_post
        self.assertFalse(post is None)

        if post:
            self.assertEqual(post.body, topic_body)
            self.assertEqual(post.user.pk, self.user.pk)

        # post to the thread
        response = self.client.get(
                reverse('forums-topic_index', kwargs={'id': '1'}))
        self.assertEqual(response.status_code, 200)

        post2_body = 'test quick post'
        response = self.client.post(
                reverse('forums-quick_reply'),
                {'body': post2_body, 'topic_id': 1})
        self.assertEqual(response.status_code, 200)
        try:
            topic = Topic.objects.get(pk=1)
        except Topic.DoesNotExist:
            self.fail("topic doesn't exist")

        post = topic.last_post
        self.assertFalse(post is None)
        if post:
            self.assertEqual(post.body, post2_body)
            self.assertEqual(post.user.pk, self.user.pk)
        self.assertEqual(topic.post_count, 2)

        # quote last post
        response = self.client.get(
                reverse('forums-new_post', kwargs={'topic_id': 1}),
                {'quote_id': 2})
        self.assertEqual(response.status_code, 200)

        post3_body = 'new post 3 content'
        response = self.client.post(
                reverse('forums-new_post', kwargs={'topic_id': 1}),
                {'body': post3_body, 'post_id': 2},
                follow=True)
        self.assertEqual(response.status_code, 200)
        try:
            topic = Topic.objects.get(pk=1)
        except Topic.DoesNotExist:
            self.fail("topic doesn't exist")

        post = topic.last_post
        self.assertFalse(post is None)
        if post:
            self.assertEqual(post.body, post3_body)
            self.assertEqual(post.user.pk, self.user.pk)
        self.assertEqual(topic.post_count, 3)

        # edit last post
        response = self.client.get(
                reverse('forums-edit_post', kwargs={'id': 3}))
        self.assertEqual(response.status_code, 200)

        post3_body = 'edited post 3 content'
        response = self.client.post(
                reverse('forums-edit_post', kwargs={'id': 3}),
                {'body': post3_body},
                follow=True)
        self.assertEqual(response.status_code, 200)
        try:
            topic = Topic.objects.get(pk=1)
        except Topic.DoesNotExist:
            self.fail("topic doesn't exist")

        post = topic.last_post
        self.assertFalse(post is None)
        if post:
            self.assertEqual(post.body, post3_body)
            self.assertEqual(post.user.pk, self.user.pk)
        self.assertEqual(topic.post_count, 3)

        # our cached copy of the profile is out of date; need to refetch from
        # the database
        user = User.objects.get(username=self.username)
        profile = user.profile
        self.assertEqual(profile.forum_post_count, 3)