comparison forums/tests/test_receivers.py @ 936:8b48ec450497

Add tests for forums signal handlers.
author Brian Neal <bgneal@gmail.com>
date Tue, 21 Apr 2015 20:32:56 -0500
parents
children
comparison
equal deleted inserted replaced
935:7a795ccd6479 936:8b48ec450497
1 """Tests for the forum app's signal handlers."""
2
3 from django.contrib.auth.models import User
4 from django.test import TestCase
5
6 from mock import patch
7
8 import custom_search.receivers
9
10 from forums.models import Forum
11 from forums.models import Post
12 from forums.models import Topic
13
14
15 class ReceiverTestCase(TestCase):
16
17 fixtures = ['forums.json']
18
19 def setUp(self):
20 self.user = User.objects.create_user('user', 'user@example.com', 'pw')
21
22 # Don't let our custom search signal handler class catch any of the
23 # signals we are throwing here.
24 custom_search.receivers.signal_processor.teardown()
25
26 def tearDown(self):
27 custom_search.receivers.signal_processor.setup()
28
29 @patch('forums.latest.notify_topic_delete')
30 def test_signal_handlers(self, mock_latest):
31 forum = Forum.objects.get(pk=2)
32 topic = Topic(forum=forum,
33 name='Test Topic',
34 user=self.user)
35 topic.save()
36 forum = Forum.objects.get(pk=2)
37 self.assertEqual(1, forum.topic_count)
38
39 post = Post(topic=topic, user=self.user, body='test')
40 post.save()
41
42 topic = Topic.objects.get(pk=topic.pk)
43 self.assertEqual(topic.post_count, 1)
44 forum = Forum.objects.get(pk=2)
45 self.assertEqual(forum.post_count, 1)
46
47 # To prevent cascading deletes
48 topic.last_post_pre_delete()
49 topic.save()
50 forum.last_post_pre_delete()
51 forum.save()
52
53 post.delete()
54 topic = Topic.objects.get(pk=topic.pk)
55 self.assertEqual(topic.post_count, 0)
56 forum = Forum.objects.get(pk=2)
57 self.assertEqual(forum.post_count, 0)
58
59 topic.delete()
60 forum = Forum.objects.get(pk=2)
61 self.assertEqual(0, forum.topic_count)
62
63 mock_latest.assert_called_once_with(topic)