Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace generated raw sql: fix error on empty addons list (bug 1197471) #680

Merged
merged 1 commit into from Aug 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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