Mercurial > public > sg101
changeset 935:7a795ccd6479
Add tests for bio signal handlers.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 20 Apr 2015 21:05:27 -0500 |
parents | 9d6c2ed2f348 |
children | 8b48ec450497 |
files | bio/tests/test_receivers.py potd/receivers.py |
diffstat | 2 files changed, 170 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bio/tests/test_receivers.py Mon Apr 20 21:05:27 2015 -0500 @@ -0,0 +1,168 @@ +"""Tests for the bio app's signal handlers.""" + +from collections import namedtuple + +from django.contrib.auth.models import User +from django.db.models.signals import post_save +from django.test import TestCase + +import bio.badges +from bio.models import Badge +from bio.models import BadgeOwnership +from bio.models import UserProfile +import custom_search.receivers +from donations.models import Donation +from downloads.models import Download +import downloads.receivers +from news.models import Story +from potd.models import Photo +import potd.receivers +from weblinks.models import Link +import weblinks.receivers + + +FakeDonation = namedtuple('FakeDonation', ['is_anonymous', 'user']) +FakeUserObject = namedtuple('FakeUserObject', ['user']) +FakeStory = namedtuple('FakeStory', ['submitter']) + + +class ReceiverTestCase(TestCase): + + fixtures = ['badges.json'] + + def setUp(self): + self.user = User.objects.create_user('user', 'user@example.com', 'pw') + + # Don't let our custom search signal handler class catch any of the + # signals we are throwing here. + custom_search.receivers.signal_processor.teardown() + + # Don't let these signal handlers fire either + post_save.disconnect(sender=Link, dispatch_uid='weblinks.receivers') + post_save.disconnect(sender=Download, dispatch_uid='downloads.receivers') + post_save.disconnect(sender=Photo, dispatch_uid='potd.receivers') + + def tearDown(self): + custom_search.receivers.signal_processor.setup() + post_save.connect(weblinks.receivers.on_link_save, sender=Link, + dispatch_uid='weblinks.receivers') + post_save.connect(downloads.receivers.on_download_save, sender=Download, + dispatch_uid='downloads.receivers') + post_save.connect(potd.receivers.on_photo_save, sender=Photo, + dispatch_uid='potd.receivers') + + def test_profile_creation(self): + profile = UserProfile.objects.get(user=self.user) + self.assertEqual(self.user.profile, profile) + + def test_donation_created(self): + donation = FakeDonation(False, self.user) + post_save.send(sender=Donation, created=True, instance=donation) + + badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) + ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) + self.assertEqual(ownership.count, 1) + + def test_donation_updated(self): + donation = FakeDonation(False, self.user) + post_save.send(sender=Donation, created=False, instance=donation) + + badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) + self.assertRaises(BadgeOwnership.DoesNotExist, + BadgeOwnership.objects.get, + badge=badge, profile=self.user.profile) + + def test_donation_anonymous(self): + donation = FakeDonation(True, self.user) + post_save.send(sender=Donation, created=False, instance=donation) + + badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) + self.assertRaises(BadgeOwnership.DoesNotExist, + BadgeOwnership.objects.get, + badge=badge, profile=self.user.profile) + + def test_donation_no_user(self): + donation = FakeDonation(False, None) + post_save.send(sender=Donation, created=False, instance=donation) + + badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) + self.assertRaises(BadgeOwnership.DoesNotExist, + BadgeOwnership.objects.get, + badge=badge, profile=self.user.profile) + + def test_donation_anon_and_no_user(self): + donation = FakeDonation(True, None) + post_save.send(sender=Donation, created=False, instance=donation) + + badge = Badge.objects.get(numeric_id=bio.badges.CONTRIBUTOR_PIN) + self.assertRaises(BadgeOwnership.DoesNotExist, + BadgeOwnership.objects.get, + badge=badge, profile=self.user.profile) + + def test_link_created(self): + link = FakeUserObject(self.user) + post_save.send(sender=Link, created=True, instance=link) + + badge = Badge.objects.get(numeric_id=bio.badges.LINK_PIN) + ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) + self.assertEqual(ownership.count, 1) + + def test_link_updated(self): + link = FakeUserObject(self.user) + post_save.send(sender=Link, created=False, instance=link) + + badge = Badge.objects.get(numeric_id=bio.badges.LINK_PIN) + self.assertRaises(BadgeOwnership.DoesNotExist, + BadgeOwnership.objects.get, + badge=badge, profile=self.user.profile) + + def test_download_created(self): + dl = FakeUserObject(self.user) + post_save.send(sender=Download, created=True, instance=dl) + + badge = Badge.objects.get(numeric_id=bio.badges.DOWNLOAD_PIN) + ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) + self.assertEqual(ownership.count, 1) + + def test_download_updated(self): + dl = FakeUserObject(self.user) + post_save.send(sender=Download, created=False, instance=dl) + + badge = Badge.objects.get(numeric_id=bio.badges.DOWNLOAD_PIN) + self.assertRaises(BadgeOwnership.DoesNotExist, + BadgeOwnership.objects.get, + badge=badge, profile=self.user.profile) + + def test_story_created(self): + story = FakeStory(self.user) + post_save.send(sender=Story, created=True, instance=story) + + badge = Badge.objects.get(numeric_id=bio.badges.NEWS_PIN) + ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) + self.assertEqual(ownership.count, 1) + + def test_story_updated(self): + story = FakeStory(self.user) + post_save.send(sender=Story, created=False, instance=story) + + badge = Badge.objects.get(numeric_id=bio.badges.NEWS_PIN) + self.assertRaises(BadgeOwnership.DoesNotExist, + BadgeOwnership.objects.get, + badge=badge, profile=self.user.profile) + + def test_photo_created(self): + photo = FakeUserObject(self.user) + post_save.send(sender=Photo, created=True, instance=photo) + + badge = Badge.objects.get(numeric_id=bio.badges.POTD_PIN) + ownership = BadgeOwnership.objects.get(badge=badge, profile=self.user.profile) + self.assertEqual(ownership.count, 1) + + def test_photo_updated(self): + photo = FakeUserObject(self.user) + post_save.send(sender=Photo, created=False, instance=photo) + + badge = Badge.objects.get(numeric_id=bio.badges.POTD_PIN) + self.assertRaises(BadgeOwnership.DoesNotExist, + BadgeOwnership.objects.get, + badge=badge, profile=self.user.profile)
--- a/potd/receivers.py Thu Apr 16 20:49:19 2015 -0500 +++ b/potd/receivers.py Mon Apr 20 21:05:27 2015 -0500 @@ -27,5 +27,5 @@ Sequence.objects.remove_photo(photo.pk) -post_save.connect(on_photo_save, sender=Photo, dispatch_uid='potd.signals') -post_delete.connect(on_photo_delete, sender=Photo, dispatch_uid='potd.signals') +post_save.connect(on_photo_save, sender=Photo, dispatch_uid='potd.receivers') +post_delete.connect(on_photo_delete, sender=Photo, dispatch_uid='potd.receivers')