예제 #1
0
 def post(self, request):
     data = JSONParser().parse(request)
     serializer = IngredientSerializer(data=data)
     if serializer.is_valid():
         serializer.save()
         return JSONResponse(serializer.data,
                             status=status.HTTP_201_CREATED)
     return JSONResponse(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
예제 #2
0
 def patch(self, request, pk):
     try:
         data = Ingredient.objects.get(pk=pk)
     except Ingredient.DoesNotExist:
         return HttpResponse(status=status.HTTP_404_NOT_FOUND)
     parsed_data = JSONParser().parse(request)
     serializer = IngredientSerializer(data, data=parsed_data, partial=True)
     if serializer.is_valid():
         serializer.save()
         return JSONResponse(serializer.data)
     return JSONResponse(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
예제 #3
0
def test_ingredient_serializer_fields(data, is_valid):
    """
    test whether each data passed to serializer is valid
    """
    deserialized_data = IngredientSerializer(data=data)
    assert deserialized_data.is_valid() == is_valid
예제 #4
0
def test_deserialize_ingredient_serializer(ingredient_data):
    """
    IngredientSerializer should deserialize ingredient instance correctly
    """
    deserialized_data = IngredientSerializer(data=ingredient_data)
    assert deserialized_data.is_valid()