def setUp(self): Commission_plan.objects.create(lower_percentage=2.5, upper_percentage=10.5, min_value=5000.00) Commission_plan.objects.create(lower_percentage=1.5, upper_percentage=5, min_value=4500) Sellers.objects.create(name="Ricardo Almeida", address="Rua abc, 213", phone="11932455678", age=35, email="*****@*****.**", cpf="65478932102", plan=Commission_plan.objects.get(id=1)) Sellers.objects.create(name="José Vendedor", address="Rua Rasa, 01", phone="5547993548264", age=42, email="*****@*****.**", cpf="456123987", plan=Commission_plan.objects.get(id=2)) Sales.objects.create(sellers_id=Sellers.objects.get(id=1), amount=1238.00, month=1, commission=round( Sales.calc_commission(self, 1, 1238.00), 2)) Sales.objects.create(sellers_id=Sellers.objects.get(id=2), amount=10950.00, month=1, commission=round( Sales.calc_commission(self, 2, 10950.00), 2))
def month_commission(request): if request.method == "POST": try: calc = Sales() calculated_amount = calc.calc_commission( request.data["sellers_id"], request.data["amount"]) except: return Response({"message": "Bad request. Please check syntax and try again"}, status=status.HTTP_400_BAD_REQUEST) request.data["commission"] = calculated_amount serializer = SalesSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response({"id": serializer.data["id"], "commission": serializer.data["commission"], }, status=status.HTTP_201_CREATED) return Response({"message": "Bad request. Please check syntax and try again"}, status=status.HTTP_400_BAD_REQUEST)
def test_calc_commission(self): self.assertEqual(Sales.calc_commission(self, 1, 1238.00), round(Decimal(30.95), 2)) self.assertEqual(Sales.calc_commission(self, 2, 10950.00), round(Decimal(547.50), 2))