def put(self, request, id): try: call = Call.objects.get(pk=id) except Call.DoesNotExist: return Response(data='{"Detail": "Call Id Not Found"}', status=status.HTTP_400_BAD_REQUEST) if (CallEnd.objects.filter(call_id=call).count() != 0): return Response(data='{"Detail": "Phone call already completed"}', status=status.HTTP_400_BAD_REQUEST) end_call = CallEnd(call_id=call) end_call.save() call_start = CallStart.objects.get(call_id=call) call.duration = end_call.timestamp - call_start.timestamp calc_price = CalcPrice(call_start.timestamp, end_call.timestamp, call.charge) call.price = calc_price.calculate_price() call.save() serializer = MonthBillSerializer({ 'destination': call_start.destination, 'date': call_start.timestamp.date(), 'time': call_start.timestamp.time(), 'duration': call.format_duration(), 'price': call.format_price() }) return Response(serializer.data, status=status.HTTP_201_CREATED)
def test_start_and_end_gt_start_and_end_standart_next_day(self): start_call = datetime(2018, 6, 18, 20, 43, 58, tzinfo=pytz.utc) end_call = datetime(2018, 6, 19, 23, 13, 43, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 93.6)
def test_start_lt_start_standard_end_lt_start_standard(self): start_call = datetime(2018, 6, 18, 3, 43, 58, tzinfo=pytz.utc) end_call = datetime(2018, 6, 18, 5, 33, 58, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 0.36)
def test_tariff_standard_end_next_day(self): start_call = datetime(2018, 6, 18, 14, 43, 58, tzinfo=pytz.utc) end_call = datetime(2018, 6, 19, 15, 13, 58, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 89.46)
def test_start_lt_start_standard_end_gt_end_standard_next_day(self): start_call = datetime(2018, 6, 18, 5, 23, 58, tzinfo=pytz.utc) end_call = datetime(2018, 6, 19, 23, 42, 58, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 173.16)
def test_when_call_is_reduced_tariff_and_same_hour(self): start_call = datetime(2018, 6, 18, 23, 0, 0, tzinfo=pytz.utc) end_call = datetime(2018, 6, 18, 23, 35, 0, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 0.36)
def test_when_tariff_standard_end_same_day(self): start_call = datetime(2018, 6, 18, 10, 43, 58, tzinfo=pytz.utc) end_call = datetime(2018, 6, 18, 11, 13, 58, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 3.06)
def test_when_call_is_tariff_standard_and_same_hour(self): start_call = datetime(2018, 6, 18, 10, 0, 0, tzinfo=pytz.utc) end_call = datetime(2018, 6, 18, 10, 35, 0, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 3.51)
def test_reduced_tariff_after_standard_across_day(self): start_call = datetime(2018, 6, 18, 22, 43, 58, tzinfo=pytz.utc) end_call = datetime(2018, 6, 18, 5, 35, 43, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 0.36)
def test_test_one_intervals_within_the_standard_next_day(self): start_call = datetime(2018, 6, 18, 20, 0, 0, tzinfo=pytz.utc) end_call = datetime(2018, 6, 20, 5, 0, 0, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 97.56)
def test_on_intervals_within_the_standard(self): start_call = datetime(2018, 6, 18, 20, 0, 0, tzinfo=pytz.utc) end_call = datetime(2018, 6, 19, 8, 0, 0, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 21.96)
def test_start_gt_end_standard_end_gt_start_standard_next_day(self): start_call = datetime(2018, 6, 18, 23, 0, 0, tzinfo=pytz.utc) end_call = datetime(2018, 6, 20, 8, 0, 0, tzinfo=pytz.utc) calc = CalcPrice(start_call, end_call, self.charge) self.assertEqual(calc.calculate_price(), 97.56)