Skip to content

Commit

Permalink
Merge pull request #680 from magopian/1197471-recs-check-addons-posted
Browse files Browse the repository at this point in the history
Replace generated raw sql: fix error on empty addons list (bug 1197471)
  • Loading branch information
magopian committed Aug 25, 2015
2 parents c3bd9c0 + bb6e0bd commit 6f63a7d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 4 additions & 5 deletions apps/bandwagon/models.py
Expand Up @@ -564,11 +564,10 @@ def save(self, **kw):
def set_addons(self, addon_ids):
# SyncedCollections are only written once so we don't need to deal with
# updates or deletes.
cursor = connection.cursor()
values = ['(%s,%s)' % (addon, self.id) for addon in addon_ids]
cursor.execute("""
INSERT INTO synced_addons_collections (addon_id, collection_id)
VALUES %s""" % ','.join(values))
relations = [
SyncedCollectionAddon(addon_id=addon_id, collection_id=self.pk)
for addon_id in addon_ids]
SyncedCollectionAddon.objects.bulk_create(relations)
if not self.addon_index:
self.addon_index = self.make_index(addon_ids)
self.save()
Expand Down
22 changes: 21 additions & 1 deletion apps/bandwagon/tests/test_models.py
Expand Up @@ -10,7 +10,8 @@
from access.models import Group
from addons.models import Addon, AddonRecommendation
from bandwagon.models import (Collection, CollectionAddon, CollectionUser,
CollectionWatcher, RecommendedCollection)
CollectionWatcher, RecommendedCollection,
SyncedCollection)
from devhub.models import ActivityLog
from bandwagon import tasks
from users.models import UserProfile
Expand Down Expand Up @@ -228,3 +229,22 @@ def test_no_dups(self, scores):
recs = RecommendedCollection.build_recs([7, 3, 8])
# 3 should not be in the list since we already have it.
eq_(recs, [1, 2])


class TestSyncedCollection(amo.tests.TestCase):
fixtures = ['base/addon-recs']
ids = (5299, 1843, 2464, 7661, 5369)

def test_set_addons(self):
synced_collection = SyncedCollection.objects.create()
synced_collection.set_addons(self.ids)
synced_collection.reload()
assert tuple(
synced_collection.addons.values_list('id', flat=True)) == self.ids

def test_set_addons_empty_list(self):
"""This is really to make sure it doesn't blow up: see bug 1197471."""
synced_collection = SyncedCollection.objects.create()
synced_collection.set_addons([])
synced_collection.reload()
assert synced_collection.addons.count() == 0

0 comments on commit 6f63a7d

Please sign in to comment.