# HG changeset patch # User Brian Neal # Date 1430274162 18000 # Node ID 9f9e50df5b83f8ab30202d3d46de2eca45dc43b4 # Parent 89e2d00d653c1ed4b3365c76397aec46b2b8bce8 Add tests for downloads signal handlers. diff -r 89e2d00d653c -r 9f9e50df5b83 downloads/tests/test_receivers.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/downloads/tests/test_receivers.py Tue Apr 28 21:22:42 2015 -0500 @@ -0,0 +1,71 @@ +"""Tests for the downloads app signal handlers.""" + +from django.contrib.auth.models import User +from django.test import TestCase + +from mock import Mock + +import custom_search.receivers + +from downloads.models import Category +from downloads.models import Download +from downloads.models import PendingDownload + + +class ReceiverTestCase(TestCase): + + fixtures = ['downloads_categories.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() + + def tearDown(self): + custom_search.receivers.signal_processor.setup() + + def test_download_signal_handlers(self): + + category = Category.objects.get(pk=1) + dl = Download(category=category, + title='Title', + description='Cool stuff', + is_public=True, + user=self.user, + ip_address='127.0.0.1') + dl.save() + + category = Category.objects.get(pk=1) + self.assertEqual(1, category.count) + + category2 = Category.objects.get(pk=4) + dl.category = category2 + dl.save() + + category = Category.objects.get(pk=1) + self.assertEqual(0, category.count) + category2 = Category.objects.get(pk=4) + self.assertEqual(1, category2.count) + + dl.delete() + category2 = Category.objects.get(pk=4) + self.assertEqual(0, category2.count) + + def test_pending_download_signal_handlers(self): + + category = Category.objects.get(pk=1) + dl = PendingDownload( + category=category, + title='Title', + description='Cool stuff', + user=self.user, + ip_address='127.0.0.1') + dl.save() + + dl.file = Mock() + dl.delete() + + dl.file.delete.assert_called_with(save=False) +