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)
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)
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
def test_deserialize_ingredient_serializer(ingredient_data): """ IngredientSerializer should deserialize ingredient instance correctly """ deserialized_data = IngredientSerializer(data=ingredient_data) assert deserialized_data.is_valid()