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
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
1 change: 0 additions & 1 deletion apps/applications/tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
55 changes: 20 additions & 35 deletions apps/bandwagon/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
from django import test
from django.conf import settings

from nose.tools import eq_
from mock import Mock
from pyquery import PyQuery as pq
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))
Expand All @@ -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))

Expand All @@ -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.')

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

Expand Down Expand Up @@ -28,22 +29,19 @@ 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')
self.other = UserProfile.objects.exclude(id=self.user.id)[0]
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')

Expand All @@ -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)

Expand All @@ -68,22 +66,20 @@ 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)
assert c.uuid != ''
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)
Expand Down Expand Up @@ -111,15 +107,15 @@ 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)
eq_(get_addons(c), addons)
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)

Expand Down Expand Up @@ -181,21 +177,16 @@ 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)


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 = [], {}
Expand All @@ -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):
Expand Down

0 comments on commit f3db530

Please sign in to comment.