Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
speed up abuse, applications, and bandwagon tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cvan committed Mar 25, 2013
1 parent 0c3ae73 commit f3db530
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 69 deletions.
12 changes: 4 additions & 8 deletions apps/abuse/tests/test_models.py
Expand Up @@ -3,27 +3,23 @@


from nose.tools import eq_ from nose.tools import eq_


import amo
import amo.tests import amo.tests
from abuse.models import AbuseReport from abuse.models import AbuseReport




class TestAbuse(amo.tests.TestCase): class TestAbuse(amo.tests.TestCase):
fixtures = ['base/users', 'base/addon_3615'] fixtures = ['base/addon_3615', 'base/user_999']


def test_user(self): def test_user(self):
abuse = AbuseReport.objects.create(user_id=999) AbuseReport(user_id=999).send()
abuse.send()
assert mail.outbox[0].subject.startswith('[User]') assert mail.outbox[0].subject.startswith('[User]')
eq_(mail.outbox[0].to, [settings.ABUSE_EMAIL]) eq_(mail.outbox[0].to, [settings.ABUSE_EMAIL])


def test_addon(self): def test_addon(self):
abuse = AbuseReport.objects.create(addon_id=3615) AbuseReport(addon_id=3615).send()
abuse.send()
assert mail.outbox[0].subject.startswith('[Extension]') assert mail.outbox[0].subject.startswith('[Extension]')


def test_addon_fr(self): def test_addon_fr(self):
abuse = AbuseReport.objects.create(addon_id=3615)
with self.activate(locale='fr'): with self.activate(locale='fr'):
abuse.send() AbuseReport(addon_id=3615).send()
assert mail.outbox[0].subject.startswith('[Extension]') assert mail.outbox[0].subject.startswith('[Extension]')
1 change: 0 additions & 1 deletion apps/applications/tests.py
@@ -1,7 +1,6 @@
import json import json


from django.core.management import call_command from django.core.management import call_command
from django.core.files.storage import default_storage as storage
from nose.tools import eq_ from nose.tools import eq_


import amo import amo
Expand Down
55 changes: 20 additions & 35 deletions apps/bandwagon/tests/test_helpers.py
@@ -1,36 +1,24 @@
from django import test from django import test
from django.conf import settings


from nose.tools import eq_ from nose.tools import eq_
from mock import Mock from mock import Mock
from pyquery import PyQuery as pq from pyquery import PyQuery as pq
import jingo import jingo


from amo.urlresolvers import reverse from amo.urlresolvers import reverse
from bandwagon.helpers import (user_collection_list, barometer, from bandwagon.helpers import (barometer, collection_favorite,
collection_favorite) user_collection_list)
from bandwagon.models import Collection from bandwagon.models import Collection
from users.models import UserProfile from users.models import UserProfile




class TestHelpers(test.TestCase): class TestHelpers(test.TestCase):


fixtures = ('base/apps', def test_collection_favorite(self):
'base/users',
'base/addon_3615',
'base/collections',
'users/test_backends',
)

def setUp(self):
self.client.get('/')
self.user = UserProfile.objects.create(username='uniq', email='uniq') self.user = UserProfile.objects.create(username='uniq', email='uniq')


def test_collection_favorite(self): c = {'request': Mock(amo_user=self.user)}
c = {} collection = Collection.objects.create()
c['request'] = Mock()
c['request'].amo_user = self.user
collection = Collection.objects.get(pk=80)


# Not subscribed yet. # Not subscribed yet.
doc = pq(collection_favorite(c, collection)) doc = pq(collection_favorite(c, collection))
Expand All @@ -43,33 +31,30 @@ def test_collection_favorite(self):


def test_barometer(self): def test_barometer(self):
jingo.load_helpers() jingo.load_helpers()
collection = Collection.objects.get(pk=80) collection = Collection(upvotes=1, slug='mccrackin',
collection.upvotes = 1 author=UserProfile(username='clouserw'))
# Mock logged out. # Mock logged out.
c = dict(request=Mock(), user=Mock(), LANG='en-US', APP='firefox') c = {
c['request'].path = 'yermom' 'request': Mock(path='yermom', GET=Mock(urlencode=lambda: '')),
c['request'].GET.urlencode = lambda: '' 'user': Mock(),
c['request'].user.is_authenticated = lambda: False 'settings': Mock()
c['settings'] = settings }
c['request'].user.is_authenticated.return_value = False
doc = pq(barometer(c, collection)) 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. # Mock logged in.
vote = Mock() c['request'].amo_user.votes.filter.return_value = [Mock(vote=1)]
vote.vote = 1 c['request'].user.is_authenticated.return_value = True
c['request'].amo_user.votes.filter.return_value = [vote]
c['request'].user.is_authenticated = lambda: True
barometer(c, collection) barometer(c, collection)
doc = pq(barometer(c, collection)) doc = pq(barometer(c, collection))
eq_(doc('form')[0].action, eq_(doc('form')[0].action,
reverse('collections.vote', args=['clouserw', 'mccrackin', 'up'])) reverse('collections.vote', args=['clouserw', 'mccrackin', 'up']))


def test_user_collection_list(self): def test_user_collection_list(self):
c1 = Collection.objects.create(author=self.user, c1 = Collection(uuid='eb4e3cd8-5cf1-4832-86fb-a90fc6d3765c')
uuid='eb4e3cd8-5cf1-4832-86fb-a90fc6d3765c') c2 = Collection(uuid='61780943-e159-4206-8acd-0ae9f63f294c',
c2 = Collection.objects.create(author=self.user, nickname='my_collection')
uuid='61780943-e159-4206-8acd-0ae9f63f294c',
nickname='my_collection')
heading = 'My Heading' heading = 'My Heading'
response = unicode(user_collection_list([c1, c2], heading)) response = unicode(user_collection_list([c1, c2], heading))


Expand All @@ -78,7 +63,7 @@ def test_user_collection_list(self):


# both items # both items
# TODO reverse URLs # 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, ( assert c2.get_url_path() in response, (
'Collection nickname link missing.') 'Collection nickname link missing.')


Expand Down
40 changes: 15 additions & 25 deletions apps/bandwagon/tests/test_models.py
@@ -1,3 +1,4 @@
import datetime
import itertools import itertools
import random import random


Expand Down Expand Up @@ -28,22 +29,19 @@ def activitylog_count(type):




class TestCollections(amo.tests.TestCase): class TestCollections(amo.tests.TestCase):
fixtures = ('base/apps', 'base/users', 'base/addon_3615', fixtures = ('base/addon_3615', 'bandwagon/test_models')
'base/addon_10423_youtubesearch', 'base/addon_1833_yoono',
'base/collections', 'bandwagon/test_models')


def setUp(self): def setUp(self):
self.user = UserProfile.objects.create(username='uhhh', email='uh@hh') self.user = UserProfile.objects.create(username='uhhh', email='uh@hh')
self.other = UserProfile.objects.exclude(id=self.user.id)[0] self.other = UserProfile.objects.exclude(id=self.user.id)[0]
amo.set_user(self.user) amo.set_user(self.user)


def test_icon_url(self): def test_icon_url(self):

# Has no icon.
# Has no icon c = Collection(pk=512, modified=datetime.datetime.now())
c = Collection.objects.get(pk=512)
assert c.icon_url.endswith('img/icons/collection.png') assert c.icon_url.endswith('img/icons/collection.png')


c.icontype = "image/png" c.icontype = 'image/png'
url = c.icon_url.split('?')[0] url = c.icon_url.split('?')[0]
assert url.endswith('0/512.png') assert url.endswith('0/512.png')


Expand All @@ -56,7 +54,7 @@ def test_icon_url(self):
assert c.icon_url.endswith('img/icons/heart.png') assert c.icon_url.endswith('img/icons/heart.png')


def test_is_subscribed(self): def test_is_subscribed(self):
c = Collection.objects.get(pk=512) c = Collection(pk=512)
c.following.create(user=self.user) c.following.create(user=self.user)
assert c.is_subscribed(self.user) assert c.is_subscribed(self.user)


Expand All @@ -68,22 +66,20 @@ def test_translation_default(self):
def test_listed(self): def test_listed(self):
"""Make sure the manager's listed() filter works.""" """Make sure the manager's listed() filter works."""
listed_count = Collection.objects.listed().count() listed_count = Collection.objects.listed().count()
# make a private collection # Make a private collection.
private = Collection( Collection.objects.create(
name="Hello", uuid="4e2a1acc-39ae-47ec-956f-46e080ac7f69", name="Hello", uuid="4e2a1acc-39ae-47ec-956f-46e080ac7f69",
listed=False, author=self.user) listed=False, author=self.user)
private.save()


listed = Collection.objects.listed() eq_(Collection.objects.listed().count(), listed_count)
eq_(len(listed), listed_count)


def test_auto_uuid(self): def test_auto_uuid(self):
c = Collection.objects.create(author=self.user) c = Collection.objects.create(author=self.user)
assert c.uuid != '' assert c.uuid != ''
assert isinstance(c.uuid, basestring) assert isinstance(c.uuid, basestring)


def test_addon_index(self): def test_addon_index(self):
c = Collection.objects.get(pk=80) c = Collection.objects.get(pk=512)
c.author = self.user c.author = self.user
eq_(c.addon_index, None) eq_(c.addon_index, None)
ids = c.addons.values_list('id', flat=True) ids = c.addons.values_list('id', flat=True)
Expand Down Expand Up @@ -111,15 +107,15 @@ def test_set_addons(self):
eq_(get_addons(c), addons) eq_(get_addons(c), addons)


# Check delete. # Check delete.
delete_cnt = len(addons) - 2 delete_cnt = len(addons) - 1
addons = addons[:2] addons = addons[:2]
c.set_addons(addons) c.set_addons(addons)
eq_(activitylog_count(amo.LOG.REMOVE_FROM_COLLECTION), delete_cnt) eq_(activitylog_count(amo.LOG.REMOVE_FROM_COLLECTION), delete_cnt)
eq_(get_addons(c), addons) eq_(get_addons(c), addons)
eq_(c.addons.count(), len(addons)) eq_(c.addons.count(), len(addons))


def test_publishable_by(self): 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() CollectionUser(collection=c, user=self.user).save()
eq_(c.publishable_by(self.user), True) eq_(c.publishable_by(self.user), True)


Expand Down Expand Up @@ -181,21 +177,16 @@ def test_can_view_stats(self):
eq_(c.can_view_stats(fake_request), True) eq_(c.can_view_stats(fake_request), True)


# Developer. # Developer.
user = UserProfile.objects.get(username='regularuser') CollectionUser.objects.create(collection=c, user=self.user)
CollectionUser.objects.create(collection=c, user=user)
fake_request.groups = () fake_request.groups = ()
fake_request.amo_user = user fake_request.amo_user = self.user
eq_(c.can_view_stats(fake_request), True) eq_(c.can_view_stats(fake_request), True)




class TestRecommendations(amo.tests.TestCase): class TestRecommendations(amo.tests.TestCase):
fixtures = ['base/addon-recs'] fixtures = ['base/addon-recs']
ids = [5299, 1843, 2464, 7661, 5369] 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 @classmethod
def expected_recs(self): def expected_recs(self):
scores, ranked = [], {} scores, ranked = [], {}
Expand All @@ -210,8 +201,7 @@ def expected_recs(self):
return [x[0] for x in addons if x[0] not in self.ids] return [x[0] for x in addons if x[0] not in self.ids]


def test_build_recs(self): def test_build_recs(self):
recs = RecommendedCollection.build_recs(self.ids) eq_(RecommendedCollection.build_recs(self.ids), self.expected_recs())
eq_(recs, self.expected_recs())


@mock.patch('bandwagon.models.AddonRecommendation.scores') @mock.patch('bandwagon.models.AddonRecommendation.scores')
def test_no_dups(self, scores): def test_no_dups(self, scores):
Expand Down

0 comments on commit f3db530

Please sign in to comment.