comparison bio/tests/test_receivers.py @ 935:7a795ccd6479

Add tests for bio signal handlers.
author Brian Neal <bgneal@gmail.com>
date Mon, 20 Apr 2015 21:05:27 -0500
parents
children
comparison
equal deleted inserted replaced
934:9d6c2ed2f348 935:7a795ccd6479
1 """Tests for the bio app's signal handlers."""
2
3 from collections import namedtuple
4
5 from django.contrib.auth.models import User
6 from django.db.models.signals import post_save
7 from django.test import TestCase
8
9 import bio.badges
10 from bio.models import Badge
11 from bio.models import BadgeOwnership
12 from bio.models import UserProfile
13 import custom_search.receivers
14 from donations.models import Donation
15 from downloads.models import Download
16 import downloads.receivers
17 from news.models import Story
18 from potd.models import Photo
19 import potd.receivers
20 from weblinks.models import Link
21 import weblinks.receivers
22
23
24 FakeDonation = namedtuple('FakeDonation', ['is_anonymous', 'user'])
25 FakeUserObject = namedtuple('FakeUserObject', ['user'])
26 FakeStory = namedtuple('FakeStory', ['submitter'])
27
28
29 class ReceiverTestCase(TestCase):
30
31 fixtures = ['badges.json']
32
33 def setUp(self):
34 self.user = User.objects.create_user('user', 'user@example.com', 'pw')
35
36 # Don't let our custom search signal handler class catch any of the
37 # signals we are throwing here.
38 custom_search.receivers.signal_processor.teardown()
39
40 # Don't let these signal handlers fire either
41 post_save.disconnect(sender=Link, dispatch_uid='weblinks.receivers')
42 post_save.disconnect(sender=Download, dispatch_uid='downloads.receivers')
43 post_save.disconnect(sender=Photo, dispatch_uid='potd.receivers')
44
45 def tearDown(self):
46 custom_search.receivers.signal_processor.setup()
47 post_save.connect(weblinks.receivers.on_link_save, sender=Link,
48 dispatch_uid='weblinks.receivers')
49 post_save.connect(downloads.receivers.on_download_save, sender=Download,
50 dispatch_uid='downloads.receivers')
51 post_save.connect(potd.receivers.on_photo_save, sender=Photo,
52 dispatch_uid='potd.receivers')
53
54 def test_profile_creation(self):
55 profile = UserProfile.objects.get(user=self.user)
56 self.assertEqual(self.user.profile, profile)
57
58 def test_donation_created(self):
59 donation = FakeDonation(False, self.user)
60 post_save.send(sender=Donation, created=True, instance=donation)
61
62 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
63 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
64 self.assertEqual(ownership.count, 1)
65
66 def test_donation_updated(self):
67 donation = FakeDonation(False, self.user)
68 post_save.send(sender=Donation, created=False, instance=donation)
69
70 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
71 self.assertRaises(BadgeOwnership.DoesNotExist,
72 BadgeOwnership.objects.get,
73 badge=badge, profile=self.user.profile)
74
75 def test_donation_anonymous(self):
76 donation = FakeDonation(True, self.user)
77 post_save.send(sender=Donation, created=False, instance=donation)
78
79 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
80 self.assertRaises(BadgeOwnership.DoesNotExist,
81 BadgeOwnership.objects.get,
82 badge=badge, profile=self.user.profile)
83
84 def test_donation_no_user(self):
85 donation = FakeDonation(False, None)
86 post_save.send(sender=Donation, created=False, instance=donation)
87
88 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
89 self.assertRaises(BadgeOwnership.DoesNotExist,
90 BadgeOwnership.objects.get,
91 badge=badge, profile=self.user.profile)
92
93 def test_donation_anon_and_no_user(self):
94 donation = FakeDonation(True, None)
95 post_save.send(sender=Donation, created=False, instance=donation)
96
97 badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN)
98 self.assertRaises(BadgeOwnership.DoesNotExist,
99 BadgeOwnership.objects.get,
100 badge=badge, profile=self.user.profile)
101
102 def test_link_created(self):
103 link = FakeUserObject(self.user)
104 post_save.send(sender=Link, created=True, instance=link)
105
106 badge = Badge.objects.get(numeric_id=bio.badges.LINK_PIN)
107 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
108 self.assertEqual(ownership.count, 1)
109
110 def test_link_updated(self):
111 link = FakeUserObject(self.user)
112 post_save.send(sender=Link, created=False, instance=link)
113
114 badge = Badge.objects.get(numeric_id=bio.badges.LINK_PIN)
115 self.assertRaises(BadgeOwnership.DoesNotExist,
116 BadgeOwnership.objects.get,
117 badge=badge, profile=self.user.profile)
118
119 def test_download_created(self):
120 dl = FakeUserObject(self.user)
121 post_save.send(sender=Download, created=True, instance=dl)
122
123 badge = Badge.objects.get(numeric_id=bio.badges.DOWNLOAD_PIN)
124 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
125 self.assertEqual(ownership.count, 1)
126
127 def test_download_updated(self):
128 dl = FakeUserObject(self.user)
129 post_save.send(sender=Download, created=False, instance=dl)
130
131 badge = Badge.objects.get(numeric_id=bio.badges.DOWNLOAD_PIN)
132 self.assertRaises(BadgeOwnership.DoesNotExist,
133 BadgeOwnership.objects.get,
134 badge=badge, profile=self.user.profile)
135
136 def test_story_created(self):
137 story = FakeStory(self.user)
138 post_save.send(sender=Story, created=True, instance=story)
139
140 badge = Badge.objects.get(numeric_id=bio.badges.NEWS_PIN)
141 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
142 self.assertEqual(ownership.count, 1)
143
144 def test_story_updated(self):
145 story = FakeStory(self.user)
146 post_save.send(sender=Story, created=False, instance=story)
147
148 badge = Badge.objects.get(numeric_id=bio.badges.NEWS_PIN)
149 self.assertRaises(BadgeOwnership.DoesNotExist,
150 BadgeOwnership.objects.get,
151 badge=badge, profile=self.user.profile)
152
153 def test_photo_created(self):
154 photo = FakeUserObject(self.user)
155 post_save.send(sender=Photo, created=True, instance=photo)
156
157 badge = Badge.objects.get(numeric_id=bio.badges.POTD_PIN)
158 ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile)
159 self.assertEqual(ownership.count, 1)
160
161 def test_photo_updated(self):
162 photo = FakeUserObject(self.user)
163 post_save.send(sender=Photo, created=False, instance=photo)
164
165 badge = Badge.objects.get(numeric_id=bio.badges.POTD_PIN)
166 self.assertRaises(BadgeOwnership.DoesNotExist,
167 BadgeOwnership.objects.get,
168 badge=badge, profile=self.user.profile)