def test_get_unused_order_items_return_unused_items(self): product_detail_list = [ [(date(2021, 1, 1), date(2021, 6, 30)), Decimal("20")], [(date(2021, 7, 1), date(2021, 12, 31)), Decimal("30")], ] self._create_zone_products(self.zone_a, product_detail_list) start_time = timezone.make_aware(datetime(2021, 1, 1)) end_time = get_end_time(start_time, 12) permit = ParkingPermitFactory( customer=self.customer, parking_zone=self.zone_a, contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=12, ) Order.objects.create_for_permits([permit]) permit.refresh_from_db() permit.status = ParkingPermitStatus.VALID permit.save() with freeze_time(datetime(2021, 4, 15)): unused_items = permit.get_unused_order_items() self.assertEqual(len(unused_items), 2) self.assertEqual(unused_items[0][0].unit_price, Decimal("20")) self.assertEqual(unused_items[0][1], 2) self.assertEqual(unused_items[0][2], (date(2021, 5, 1), date(2021, 6, 30))) self.assertEqual(unused_items[1][0].unit_price, Decimal("30")) self.assertEqual(unused_items[1][1], 6) self.assertEqual(unused_items[1][2], (date(2021, 7, 1), date(2021, 12, 31)))
def test_get_refund_amount_for_unused_items_should_return_correct_total( self): product_detail_list = [ [(date(2021, 1, 1), date(2021, 6, 30)), Decimal("20")], [(date(2021, 7, 1), date(2021, 12, 31)), Decimal("30")], ] self._create_zone_products(self.zone_a, product_detail_list) start_time = timezone.make_aware(datetime(2021, 1, 1)) end_time = get_end_time(start_time, 12) permit = ParkingPermitFactory( customer=self.customer, parking_zone=self.zone_a, contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=12, ) order = Order.objects.create_for_permits([permit]) order.status = OrderStatus.CONFIRMED order.save() permit.refresh_from_db() permit.status = ParkingPermitStatus.VALID permit.save() with freeze_time(datetime(2021, 4, 15)): refund_amount = permit.get_refund_amount_for_unused_items() self.assertEqual(refund_amount, Decimal("220"))
def test_set_month_count_to_1_for_open_ended_contract(self): customer = CustomerFactory(first_name="Customer a", last_name="") permit = ParkingPermitFactory(customer=customer, contract_type=FIXED_PERIOD) data = {"month_count": 3, "contract_type": OPEN_ENDED} permit_id = str(permit.id) CustomerPermit(customer.id).update(data, permit_id=permit_id) permit.refresh_from_db() self.assertEqual(permit.month_count, 1)
def test_primary_permit_can_have_max_12_month(self): customer = CustomerFactory(first_name="Customer", last_name="") permit = ParkingPermitFactory(customer=customer, contract_type=FIXED_PERIOD) data = {"month_count": 13, "contract_type": FIXED_PERIOD} permit_id = str(permit.id) CustomerPermit(customer.id).update(data, permit_id=permit_id) permit.refresh_from_db() self.assertEqual(permit.month_count, 12)
def test_second_permit_can_have_upto_12_month_if_primary_is_open_ended(self): customer = CustomerFactory() ParkingPermitFactory(customer=customer) secondary = ParkingPermitFactory(customer=customer, primary_vehicle=False) data = {"month_count": 12, "contract_type": FIXED_PERIOD} permit_id = str(secondary.id) CustomerPermit(customer.id).update(data, permit_id=permit_id) secondary.refresh_from_db() self.assertEqual(secondary.month_count, 12)
def test_create_renewable_order_should_create_renewal_order(self): start_time = timezone.make_aware(datetime(CURRENT_YEAR, 3, 15)) end_time = get_end_time(start_time, 6) # end at CURRENT_YEAR-09-14 23:59 high_emission_vehicle = VehicleFactory( power_type=VehiclePowerType.BENSIN, emission=100, euro_class=6, emission_type=EmissionType.WLTP, ) low_emission_vehicle = VehicleFactory( power_type=VehiclePowerType.BENSIN, emission=70, euro_class=6, emission_type=EmissionType.WLTP, ) LowEmissionCriteriaFactory( start_date=start_time, end_date=end_time, nedc_max_emission_limit=None, wltp_max_emission_limit=80, euro_min_class_limit=6, power_type=low_emission_vehicle.power_type, ) permit = ParkingPermitFactory( parking_zone=self.zone, vehicle=high_emission_vehicle, customer=self.customer, contract_type=ContractType.FIXED_PERIOD, status=ParkingPermitStatus.DRAFT, start_time=start_time, end_time=end_time, month_count=6, ) order = Order.objects.create_for_permits([permit]) order.status = OrderStatus.CONFIRMED order.save() permit.refresh_from_db() permit.status = ParkingPermitStatus.VALID permit.vehicle = low_emission_vehicle permit.save() with freeze_time(timezone.make_aware(datetime(CURRENT_YEAR, 5, 5))): new_order = Order.objects.create_renewal_order(self.customer) order_items = new_order.order_items.all().order_by("start_date") self.assertEqual(order_items.count(), 2) self.assertEqual(order_items[0].unit_price, Decimal(15)) self.assertEqual(order_items[0].payment_unit_price, Decimal(-15)) self.assertEqual(order_items[0].quantity, 2) self.assertEqual(order_items[1].unit_price, Decimal(25)) self.assertEqual(order_items[1].payment_unit_price, Decimal(-25)) self.assertEqual(order_items[1].quantity, 2)
def test_order_view_should_update_order_and_permits_status(self): talpa_order_id = "D86CA61D-97E9-410A-A1E3-4894873B1B35" permit_1 = ParkingPermitFactory(status=ParkingPermitStatus.PAYMENT_IN_PROGRESS) permit_2 = ParkingPermitFactory(status=ParkingPermitStatus.PAYMENT_IN_PROGRESS) order = OrderFactory(talpa_order_id=talpa_order_id, status=OrderStatus.DRAFT) order.permits.add(permit_1, permit_2) url = reverse("parking_permits:order-notify") data = {"eventType": "PAYMENT_PAID", "orderId": talpa_order_id} response = self.client.post(url, data) self.assertEqual(response.status_code, 200) order.refresh_from_db() permit_1.refresh_from_db() permit_2.refresh_from_db() self.assertEqual(order.status, OrderStatus.CONFIRMED) self.assertEqual(permit_1.status, ParkingPermitStatus.VALID) self.assertEqual(permit_1.status, ParkingPermitStatus.VALID)
def test_second_permit_can_not_have_permit_more_then_primary_if_primary_is_fixed_period( self, ): customer = CustomerFactory() ParkingPermitFactory( customer=customer, status=VALID, month_count=5, contract_type=FIXED_PERIOD, end_time=get_end_time(next_day(), 5), ) secondary = ParkingPermitFactory( customer=customer, primary_vehicle=False, contract_type=FIXED_PERIOD ) data = {"month_count": 12, "contract_type": FIXED_PERIOD} permit_id = str(secondary.id) CustomerPermit(customer.id).update(data, permit_id=permit_id) secondary.refresh_from_db() self.assertEqual(secondary.month_count, 5)
def test_secondary_permit_can_be_only_fixed_if_primary_is_fixed_period(self): customer = CustomerFactory(first_name="Customer 1", last_name="") ParkingPermitFactory( customer=customer, status=VALID, contract_type=FIXED_PERIOD ) secondary = ParkingPermitFactory( customer=customer, primary_vehicle=False, contract_type=FIXED_PERIOD ) permit_id = str(secondary.id) msg = "Only FIXED_PERIOD is allowed" with self.assertRaisesMessage(InvalidContractType, msg): data = {"contract_type": OPEN_ENDED} CustomerPermit(customer.id).update(data, permit_id=permit_id) data1 = {"contract_type": FIXED_PERIOD} CustomerPermit(customer.id).update(data1, permit_id=permit_id) secondary.refresh_from_db() self.assertEqual(secondary.contract_type, FIXED_PERIOD)
def test_secondary_permit_can_be_either_open_ended_or_fixed_if_primary_is_open_ended( self, ): customer = CustomerFactory(first_name="Fake", last_name="") ParkingPermitFactory(customer=customer, status=VALID) secondary = ParkingPermitFactory( customer=customer, primary_vehicle=False, ) permit_id = str(secondary.id) data = {"contract_type": OPEN_ENDED} CustomerPermit(customer.id).update(data, permit_id=permit_id) secondary.refresh_from_db() self.assertEqual(secondary.contract_type, OPEN_ENDED) data1 = {"contract_type": FIXED_PERIOD} CustomerPermit(customer.id).update(data1, permit_id=permit_id) secondary.refresh_from_db() self.assertEqual(secondary.contract_type, FIXED_PERIOD)