bgneal@936: """Tests for the forum app's signal handlers.""" bgneal@936: bgneal@936: from django.contrib.auth.models import User bgneal@936: from django.test import TestCase bgneal@936: bgneal@936: from mock import patch bgneal@936: bgneal@936: import custom_search.receivers bgneal@936: bgneal@936: from forums.models import Forum bgneal@936: from forums.models import Post bgneal@936: from forums.models import Topic bgneal@936: bgneal@936: bgneal@936: class ReceiverTestCase(TestCase): bgneal@936: bgneal@936: fixtures = ['forums.json'] bgneal@936: bgneal@936: def setUp(self): bgneal@936: self.user = User.objects.create_user('user', 'user@example.com', 'pw') bgneal@936: bgneal@936: # Don't let our custom search signal handler class catch any of the bgneal@936: # signals we are throwing here. bgneal@936: custom_search.receivers.signal_processor.teardown() bgneal@936: bgneal@936: def tearDown(self): bgneal@936: custom_search.receivers.signal_processor.setup() bgneal@936: bgneal@936: @patch('forums.latest.notify_topic_delete') bgneal@936: def test_signal_handlers(self, mock_latest): bgneal@936: forum = Forum.objects.get(pk=2) bgneal@936: topic = Topic(forum=forum, bgneal@936: name='Test Topic', bgneal@936: user=self.user) bgneal@936: topic.save() bgneal@936: forum = Forum.objects.get(pk=2) bgneal@936: self.assertEqual(1, forum.topic_count) bgneal@936: bgneal@936: post = Post(topic=topic, user=self.user, body='test') bgneal@936: post.save() bgneal@936: bgneal@936: topic = Topic.objects.get(pk=topic.pk) bgneal@936: self.assertEqual(topic.post_count, 1) bgneal@936: forum = Forum.objects.get(pk=2) bgneal@936: self.assertEqual(forum.post_count, 1) bgneal@936: bgneal@936: # To prevent cascading deletes bgneal@936: topic.last_post_pre_delete() bgneal@936: topic.save() bgneal@936: forum.last_post_pre_delete() bgneal@936: forum.save() bgneal@936: bgneal@936: post.delete() bgneal@936: topic = Topic.objects.get(pk=topic.pk) bgneal@936: self.assertEqual(topic.post_count, 0) bgneal@936: forum = Forum.objects.get(pk=2) bgneal@936: self.assertEqual(forum.post_count, 0) bgneal@936: bgneal@936: topic.delete() bgneal@936: forum = Forum.objects.get(pk=2) bgneal@936: self.assertEqual(0, forum.topic_count) bgneal@936: bgneal@936: mock_latest.assert_called_once_with(topic)