From f3db530d0f0a0ed87fc84775588ca6ce229da29b Mon Sep 17 00:00:00 2001 From: Chris Van Date: Sun, 24 Mar 2013 15:35:27 -0700 Subject: [PATCH] speed up abuse, applications, and bandwagon tests --- apps/abuse/tests/test_models.py | 12 ++---- apps/applications/tests.py | 1 - apps/bandwagon/tests/test_helpers.py | 55 ++++++++++------------------ apps/bandwagon/tests/test_models.py | 40 ++++++++------------ 4 files changed, 39 insertions(+), 69 deletions(-) diff --git a/apps/abuse/tests/test_models.py b/apps/abuse/tests/test_models.py index f2f5fd17181..aa49f5ba5ed 100644 --- a/apps/abuse/tests/test_models.py +++ b/apps/abuse/tests/test_models.py @@ -3,27 +3,23 @@ from nose.tools import eq_ -import amo import amo.tests from abuse.models import AbuseReport class TestAbuse(amo.tests.TestCase): - fixtures = ['base/users', 'base/addon_3615'] + fixtures = ['base/addon_3615', 'base/user_999'] def test_user(self): - abuse = AbuseReport.objects.create(user_id=999) - abuse.send() + AbuseReport(user_id=999).send() assert mail.outbox[0].subject.startswith('[User]') eq_(mail.outbox[0].to, [settings.ABUSE_EMAIL]) def test_addon(self): - abuse = AbuseReport.objects.create(addon_id=3615) - abuse.send() + AbuseReport(addon_id=3615).send() assert mail.outbox[0].subject.startswith('[Extension]') def test_addon_fr(self): - abuse = AbuseReport.objects.create(addon_id=3615) with self.activate(locale='fr'): - abuse.send() + AbuseReport(addon_id=3615).send() assert mail.outbox[0].subject.startswith('[Extension]') diff --git a/apps/applications/tests.py b/apps/applications/tests.py index af48feba66d..03378a6d67f 100644 --- a/apps/applications/tests.py +++ b/apps/applications/tests.py @@ -1,7 +1,6 @@ import json from django.core.management import call_command -from django.core.files.storage import default_storage as storage from nose.tools import eq_ import amo diff --git a/apps/bandwagon/tests/test_helpers.py b/apps/bandwagon/tests/test_helpers.py index af2318e2291..b6da58cdaca 100644 --- a/apps/bandwagon/tests/test_helpers.py +++ b/apps/bandwagon/tests/test_helpers.py @@ -1,5 +1,4 @@ from django import test -from django.conf import settings from nose.tools import eq_ from mock import Mock @@ -7,30 +6,19 @@ import jingo from amo.urlresolvers import reverse -from bandwagon.helpers import (user_collection_list, barometer, - collection_favorite) +from bandwagon.helpers import (barometer, collection_favorite, + user_collection_list) from bandwagon.models import Collection from users.models import UserProfile class TestHelpers(test.TestCase): - fixtures = ('base/apps', - 'base/users', - 'base/addon_3615', - 'base/collections', - 'users/test_backends', - ) - - def setUp(self): - self.client.get('/') + def test_collection_favorite(self): self.user = UserProfile.objects.create(username='uniq', email='uniq') - def test_collection_favorite(self): - c = {} - c['request'] = Mock() - c['request'].amo_user = self.user - collection = Collection.objects.get(pk=80) + c = {'request': Mock(amo_user=self.user)} + collection = Collection.objects.create() # Not subscribed yet. doc = pq(collection_favorite(c, collection)) @@ -43,33 +31,30 @@ def test_collection_favorite(self): def test_barometer(self): jingo.load_helpers() - collection = Collection.objects.get(pk=80) - collection.upvotes = 1 + collection = Collection(upvotes=1, slug='mccrackin', + author=UserProfile(username='clouserw')) # Mock logged out. - c = dict(request=Mock(), user=Mock(), LANG='en-US', APP='firefox') - c['request'].path = 'yermom' - c['request'].GET.urlencode = lambda: '' - c['request'].user.is_authenticated = lambda: False - c['settings'] = settings + c = { + 'request': Mock(path='yermom', GET=Mock(urlencode=lambda: '')), + 'user': Mock(), + 'settings': Mock() + } + c['request'].user.is_authenticated.return_value = False doc = pq(barometer(c, collection)) - eq_(doc('form')[0].action, '/en-US/firefox/users/login?to=yermom') + eq_(doc('form')[0].action, '/users/login?to=yermom') # Mock logged in. - vote = Mock() - vote.vote = 1 - c['request'].amo_user.votes.filter.return_value = [vote] - c['request'].user.is_authenticated = lambda: True + c['request'].amo_user.votes.filter.return_value = [Mock(vote=1)] + c['request'].user.is_authenticated.return_value = True barometer(c, collection) doc = pq(barometer(c, collection)) eq_(doc('form')[0].action, reverse('collections.vote', args=['clouserw', 'mccrackin', 'up'])) def test_user_collection_list(self): - c1 = Collection.objects.create(author=self.user, - uuid='eb4e3cd8-5cf1-4832-86fb-a90fc6d3765c') - c2 = Collection.objects.create(author=self.user, - uuid='61780943-e159-4206-8acd-0ae9f63f294c', - nickname='my_collection') + c1 = Collection(uuid='eb4e3cd8-5cf1-4832-86fb-a90fc6d3765c') + c2 = Collection(uuid='61780943-e159-4206-8acd-0ae9f63f294c', + nickname='my_collection') heading = 'My Heading' response = unicode(user_collection_list([c1, c2], heading)) @@ -78,7 +63,7 @@ def test_user_collection_list(self): # both items # TODO reverse URLs - assert c1.get_url_path() in response, ('Collection UUID link missing.') + assert c1.get_url_path() in response, 'Collection UUID link missing.' assert c2.get_url_path() in response, ( 'Collection nickname link missing.') diff --git a/apps/bandwagon/tests/test_models.py b/apps/bandwagon/tests/test_models.py index 09f1f25dd63..f6a78d7af62 100644 --- a/apps/bandwagon/tests/test_models.py +++ b/apps/bandwagon/tests/test_models.py @@ -1,3 +1,4 @@ +import datetime import itertools import random @@ -28,9 +29,7 @@ def activitylog_count(type): class TestCollections(amo.tests.TestCase): - fixtures = ('base/apps', 'base/users', 'base/addon_3615', - 'base/addon_10423_youtubesearch', 'base/addon_1833_yoono', - 'base/collections', 'bandwagon/test_models') + fixtures = ('base/addon_3615', 'bandwagon/test_models') def setUp(self): self.user = UserProfile.objects.create(username='uhhh', email='uh@hh') @@ -38,12 +37,11 @@ def setUp(self): amo.set_user(self.user) def test_icon_url(self): - - # Has no icon - c = Collection.objects.get(pk=512) + # Has no icon. + c = Collection(pk=512, modified=datetime.datetime.now()) assert c.icon_url.endswith('img/icons/collection.png') - c.icontype = "image/png" + c.icontype = 'image/png' url = c.icon_url.split('?')[0] assert url.endswith('0/512.png') @@ -56,7 +54,7 @@ def test_icon_url(self): assert c.icon_url.endswith('img/icons/heart.png') def test_is_subscribed(self): - c = Collection.objects.get(pk=512) + c = Collection(pk=512) c.following.create(user=self.user) assert c.is_subscribed(self.user) @@ -68,14 +66,12 @@ def test_translation_default(self): def test_listed(self): """Make sure the manager's listed() filter works.""" listed_count = Collection.objects.listed().count() - # make a private collection - private = Collection( + # Make a private collection. + Collection.objects.create( name="Hello", uuid="4e2a1acc-39ae-47ec-956f-46e080ac7f69", listed=False, author=self.user) - private.save() - listed = Collection.objects.listed() - eq_(len(listed), listed_count) + eq_(Collection.objects.listed().count(), listed_count) def test_auto_uuid(self): c = Collection.objects.create(author=self.user) @@ -83,7 +79,7 @@ def test_auto_uuid(self): assert isinstance(c.uuid, basestring) def test_addon_index(self): - c = Collection.objects.get(pk=80) + c = Collection.objects.get(pk=512) c.author = self.user eq_(c.addon_index, None) ids = c.addons.values_list('id', flat=True) @@ -111,7 +107,7 @@ def test_set_addons(self): eq_(get_addons(c), addons) # Check delete. - delete_cnt = len(addons) - 2 + delete_cnt = len(addons) - 1 addons = addons[:2] c.set_addons(addons) eq_(activitylog_count(amo.LOG.REMOVE_FROM_COLLECTION), delete_cnt) @@ -119,7 +115,7 @@ def test_set_addons(self): eq_(c.addons.count(), len(addons)) def test_publishable_by(self): - c = Collection.objects.create(author=self.other) + c = Collection(pk=512, author=self.other) CollectionUser(collection=c, user=self.user).save() eq_(c.publishable_by(self.user), True) @@ -181,10 +177,9 @@ def test_can_view_stats(self): eq_(c.can_view_stats(fake_request), True) # Developer. - user = UserProfile.objects.get(username='regularuser') - CollectionUser.objects.create(collection=c, user=user) + CollectionUser.objects.create(collection=c, user=self.user) fake_request.groups = () - fake_request.amo_user = user + fake_request.amo_user = self.user eq_(c.can_view_stats(fake_request), True) @@ -192,10 +187,6 @@ class TestRecommendations(amo.tests.TestCase): fixtures = ['base/addon-recs'] ids = [5299, 1843, 2464, 7661, 5369] - def setUp(self): - self.user = UserProfile.objects.create(username='uhhh', email='uh@hh') - amo.set_user(self.user) - @classmethod def expected_recs(self): scores, ranked = [], {} @@ -210,8 +201,7 @@ def expected_recs(self): return [x[0] for x in addons if x[0] not in self.ids] def test_build_recs(self): - recs = RecommendedCollection.build_recs(self.ids) - eq_(recs, self.expected_recs()) + eq_(RecommendedCollection.build_recs(self.ids), self.expected_recs()) @mock.patch('bandwagon.models.AddonRecommendation.scores') def test_no_dups(self, scores):