def test_can_add_remove_collection_badges_via_serializer(self): """ The CollectionSerializer should be able to update an existing collection's badge list """ collection = BackpackCollection.objects.first() self.assertEqual(len(self.collection.cached_badgeinstances()), 0) serializer = CollectionSerializerV1( collection, data={'badges': [{'id': self.local_badge_instance_1.entity_id}, {'id': self.local_badge_instance_2.entity_id}]}, partial=True ) serializer.is_valid(raise_exception=True) serializer.save() self.assertEqual(collection.cached_badgeinstances().count(), 2) self.assertEqual([i.entity_id for i in collection.cached_badgeinstances()], [self.local_badge_instance_1.entity_id, self.local_badge_instance_2.entity_id]) serializer = CollectionSerializerV1( collection, data={'badges': [{'id': self.local_badge_instance_2.entity_id}, {'id': self.local_badge_instance_3.entity_id}]}, partial=True ) serializer.is_valid(raise_exception=True) serializer.save() self.assertEqual(collection.cached_badgeinstances().count(), 2) self.assertEqual([i.entity_id for i in collection.cached_badgeinstances()], [self.local_badge_instance_2.entity_id, self.local_badge_instance_3.entity_id])
def test_can_publish_unpublish_collection_serializer(self): """ The CollectionSerializer should be able to update/delete a collection's share hash via update method. """ collection = BackpackCollection.objects.first() self.assertIn(collection.share_url, ('', None)) serializer = CollectionSerializerV1( collection, data={'published': True}, partial=True ) serializer.is_valid(raise_exception=True) serializer.save() self.assertNotEqual(collection.share_url, '') self.assertTrue(collection.published) serializer = CollectionSerializerV1( collection, data={'published': False}, partial=True ) serializer.is_valid(raise_exception=True) serializer.save() self.assertFalse(collection.published) self.assertIn(collection.share_url, ('', None))
def xit_test_can_update_badge_description_in_collection_via_detail_api(self): self.assertEqual(self.collection.cached_badgeinstances().count(), 0) serializer = CollectionSerializerV1( self.collection, data={'badges': [{'id': self.local_badge_instance_1.pk}, {'id': self.local_badge_instance_2.pk}]}, partial=True ) serializer.is_valid(raise_exception=True) serializer.save() self.assertEqual(self.collection.cached_badgeinstances().count(), 2) self.client.force_authenticate(user=self.user) response = self.client.put( '/v1/earner/collections/{}/badges/{}'.format(self.collection.entity_id, self.local_badge_instance_1.pk), data={'id': 1, 'description': 'A cool badge.'}, format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(response.data, {'id': self.local_badge_instance_1.pk, 'description': 'A cool badge.'}) obj = BackpackCollectionBadgeInstance.objects.get(collection=self.collection, instance_id=self.local_badge_instance_1.pk) self.assertEqual(obj.description, 'A cool badge.')
def test_can_define_collection_serializer(self): """ A new collection may be created directly via serializer. """ data = { 'name': 'Fruity Collection', 'description': 'Apples and Oranges', 'badges': [{'id': self.local_badge_instance_1.entity_id}, {'id': self.local_badge_instance_2.entity_id, 'description': 'A cool badge'}] } serializer = CollectionSerializerV1(data=data, context={'user': self.user}) serializer.is_valid(raise_exception=True) collection = serializer.save() self.assertIsNotNone(collection.pk) self.assertEqual(collection.name, data['name']) self.assertEqual(collection.cached_badgeinstances().count(), 2)