Пример #1
0
 def test_remove_stock_ok(self):
     data = {'product': self.product.pk, 'quantity': 1}
     request = factory.post('api/v1/product/remove_stock', data)
     force_authenticate(request, user=self.user)
     response = remove_stock(request)
     product = Product.objects.get(pk=self.product.id)
     serializer = ProductSerializer(product)
     self.assertEqual(response.data, serializer.data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Пример #2
0
 def test_get_product(self):
     request = factory.get('api/v1/product/',)
     force_authenticate(request, user=self.user)
     view = ProductViewSet.as_view({'get': 'retrieve'})
     response = view(request, pk=self.product.id)
     product = Product.objects.get(pk=self.product.id)
     serializer = ProductSerializer(product)
     self.assertEqual(response.data, serializer.data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Пример #3
0
 def test_get_all(self):
     request = factory.get('api/v1/product/')
     force_authenticate(request, user=self.user)
     view = ProductViewSet.as_view({'get': 'list'})
     response = view(request)
     products = Product.objects.all()
     serializer = ProductSerializer(products, many=True)
     self.assertEqual(response.data['results'], serializer.data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Пример #4
0
    def post(self, request, format=None):
        try:
            product = Product.objects.get(id=request.data['product'])
            product.stock += int(request.data['quantity'])
            product.save()
            serializer = ProductSerializer(product)
            data = serializer.data
            status_req = status.HTTP_200_OK
        except Exception:
            data = {NOT_FOUND_MESSAGE}
            status_req = status.HTTP_404_NOT_FOUND

        return Response(data, status=status_req)
Пример #5
0
 def test_update(self):
     data = {
         'category': self.category.id, 'name': 'Produto Teste 2',
         'description': 'Lorem ipsum', 'value': 15.5, 'stock': 10
     }
     request = factory.post('api/v1/product/', data)
     force_authenticate(request, user=self.user)
     view = ProductViewSet.as_view({'post': 'update'})
     response = view(request, pk=self.product.id)
     product = Product.objects.get(pk=self.product.id)
     serializer = ProductSerializer(product)
     self.assertEqual(response.data, serializer.data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Пример #6
0
def remove_stock(request):
    try:
        product = Product.objects.get(id=request.data['product'])

        data = {INSUFICIENT_QUANTITY_MESSAGE}
        status_req = status.HTTP_400_BAD_REQUEST

        if product.stock >= int(request.data['quantity']):
            product.stock -= int(request.data['quantity'])
            product.save()
            serializer = ProductSerializer(product)
            data = serializer.data
            status_req = status.HTTP_200_OK

    except Exception:
        data = {NOT_FOUND_MESSAGE}
        status_req = status.HTTP_404_NOT_FOUND

    return Response(data, status=status_req)