view bio/tests/test_receivers.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 7a795ccd6479
children
line wrap: on
line source
"""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)