예제 #1
0
    def test_validate_tag(self):
        """
        Test whether custom validate_tag method returns a tag instance or
        raises a serializers.ValidationError.
        """
        feed = Feed.objects.get(name=self.feedname)
        tag = Tag.objects.get(name="Tag1")
        data = {'tag': tag, 'feed': feed}
        tagging_serializer = TaggingSerializer(data=data)
        tagging_serializer.context['request'] = mock.Mock()
        tagging_serializer.context['request'].user = self.user

        tag_inst = tagging_serializer.validate_tag(tag.id)
        self.assertEqual(tag, tag_inst)

        with self.assertRaises(serializers.ValidationError):
            tagging_serializer.validate_tag('')  # error if no id is passed

        with self.assertRaises(serializers.ValidationError):
            tagging_serializer.validate_tag(tag.id +
                                            1)  # error if tag not found in DB

        with self.assertRaises(serializers.ValidationError):
            other_user = User.objects.get(username=self.other_username)
            tagging_serializer.context['request'].user = other_user
            tagging_serializer.validate_tag(
                tag.id)  # error if users doesn't own tag
    def test_validate_tag(self):
        """
        Test whether custom validate_tag method returns a tag instance or
        raises a serializers.ValidationError.
        """
        feed = Feed.objects.get(name=self.feedname)
        tag = Tag.objects.get(name="Tag1")
        data = {'tag': tag, 'feed': feed}
        tagging_serializer = TaggingSerializer(data=data)
        tagging_serializer.context['request'] = mock.Mock()
        tagging_serializer.context['request'].user = self.user

        tag_inst = tagging_serializer.validate_tag(tag.id)
        self.assertEqual(tag, tag_inst)

        with self.assertRaises(serializers.ValidationError):
            tagging_serializer.validate_tag('') # error if no id is passed

        with self.assertRaises(serializers.ValidationError):
            tagging_serializer.validate_tag(tag.id + 1) # error if tag not found in DB

        with self.assertRaises(serializers.ValidationError):
            other_user = User.objects.get(username=self.other_username)
            tagging_serializer.context['request'].user = other_user
            tagging_serializer.validate_tag(tag.id) # error if users doesn't own tag
예제 #3
0
 def test_create(self):
     """
     Test whether overriden 'create' method raises a ValidationError when a new
     tagging already exists in the DB.
     """
     feed = Feed.objects.get(name=self.feedname)
     tag = Tag.objects.get(name="Tag1")
     data = {'tag': tag, 'feed': feed}
     tagging_serializer = TaggingSerializer(data=data)
     with self.assertRaises(serializers.ValidationError):
         tagging_serializer.create(data)
 def test_create(self):
     """
     Test whether overriden 'create' method raises a ValidationError when a new
     tagging already exists in the DB.
     """
     feed = Feed.objects.get(name=self.feedname)
     tag = Tag.objects.get(name="Tag1")
     data = {'tag': tag, 'feed': feed}
     tagging_serializer = TaggingSerializer(data=data)
     with self.assertRaises(serializers.ValidationError):
         tagging_serializer.create(data)