def test_should_return_correct_end_time_of_current_time(self): start_time = timezone.make_aware(datetime(2021, 11, 15)) end_time = get_end_time(start_time, 6) permit = ParkingPermitFactory( contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=6, ) self.assertEqual( permit.current_period_end_time, timezone.make_aware( datetime(CURRENT_YEAR, 2, 14, 23, 59, 59, 999999)), ) start_time = timezone.make_aware(datetime(2021, 11, 20)) end_time = get_end_time(start_time, 6) permit = ParkingPermitFactory( contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=6, ) self.assertEqual( permit.current_period_end_time, timezone.make_aware( datetime(CURRENT_YEAR, 2, 19, 23, 59, 59, 999999)), )
def test_should_raise_error_when_end_primary_vehicle_permit_with_active_secondary_vehicle_permit( self, ): primary_start_time = timezone.make_aware(datetime(2021, 11, 15)) primary_end_time = get_end_time(primary_start_time, 6) primary_vehicle_permit = ParkingPermitFactory( customer=self.customer, primary_vehicle=True, contract_type=ContractType.FIXED_PERIOD, status=ParkingPermitStatus.VALID, start_time=primary_start_time, end_time=primary_end_time, month_count=6, ) secondary_start_time = timezone.make_aware(datetime( CURRENT_YEAR, 1, 1)) secondary_end_time = get_end_time(secondary_start_time, 2) ParkingPermitFactory( customer=self.customer, contract_type=ContractType.FIXED_PERIOD, status=ParkingPermitStatus.VALID, start_time=secondary_start_time, end_time=secondary_end_time, month_count=2, ) with self.assertRaises(PermitCanNotBeEnded): primary_vehicle_permit.end_permit( ParkingPermitEndType.AFTER_CURRENT_PERIOD)
def test_parking_permit_change_price_list_when_prices_go_down(self): zone_a_product_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, zone_a_product_list) zone_b_product_list = [ [(date(2021, 1, 1), date(2021, 6, 30)), Decimal("30")], [(date(2021, 7, 1), date(2021, 12, 31)), Decimal("40")], ] self._create_zone_products(self.zone_b, zone_b_product_list) high_emission_vehicle = VehicleFactory() 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, vehicle=high_emission_vehicle, contract_type=ContractType.FIXED_PERIOD, status=ParkingPermitStatus.VALID, start_time=start_time, end_time=end_time, month_count=12, ) with freeze_time(datetime(2021, 4, 15)): price_change_list = permit.get_price_change_list(self.zone_b, True) self.assertEqual(len(price_change_list), 2) self.assertEqual(price_change_list[0]["product"], "Pysäköintialue B") self.assertEqual(price_change_list[0]["previous_price"], Decimal("20")) self.assertEqual(price_change_list[0]["new_price"], Decimal("15")) self.assertEqual(price_change_list[0]["price_change"], Decimal("-5")) self.assertEqual(price_change_list[0]["price_change_vat"], Decimal("-1.2")) self.assertEqual(price_change_list[0]["month_count"], 2) self.assertEqual(price_change_list[0]["start_date"], date(2021, 5, 1)) self.assertEqual(price_change_list[0]["end_date"], date(2021, 6, 30)) self.assertEqual(price_change_list[1]["product"], "Pysäköintialue B") self.assertEqual(price_change_list[1]["previous_price"], Decimal("30")) self.assertEqual(price_change_list[1]["new_price"], Decimal("20")) self.assertEqual(price_change_list[1]["price_change"], Decimal("-10")) self.assertEqual(price_change_list[1]["price_change_vat"], Decimal("-2.4")) self.assertEqual(price_change_list[1]["month_count"], 6) self.assertEqual(price_change_list[1]["start_date"], date(2021, 7, 1)) self.assertEqual(price_change_list[1]["end_date"], date(2021, 12, 31))
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_products_with_quantities_should_return_products_with_quantities_for_fix_period_with_mid_month_start( self, ): product_detail_list = [ [(date(2021, 1, 10), date(2021, 5, 9)), Decimal("30")], [(date(2021, 5, 10), date(2021, 8, 9)), Decimal("30")], [(date(2021, 8, 10), date(2021, 12, 31)), Decimal("30")], ] products = self._create_zone_products(self.zone_a, product_detail_list) start_time = timezone.make_aware(datetime(2021, 2, 15)) end_time = get_end_time(start_time, 10) # ends at 2021-2-14, 23:59 permit = ParkingPermitFactory( parking_zone=self.zone_a, contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=10, ) products_with_quantities = permit.get_products_with_quantities() self.assertEqual(products_with_quantities[0][0].id, products[0].id) self.assertEqual(products_with_quantities[0][1], 3) self.assertEqual(products_with_quantities[1][0].id, products[1].id) self.assertEqual(products_with_quantities[1][1], 3) self.assertEqual(products_with_quantities[2][0].id, products[2].id) self.assertEqual(products_with_quantities[2][1], 4)
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_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_should_return_correct_months_left(self): start_time = timezone.make_aware(datetime(2021, 9, 15)) end_time = get_end_time(start_time, 6) fixed_period_permit_started_2_months_ago = ParkingPermitFactory( contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=6, ) self.assertEqual(fixed_period_permit_started_2_months_ago.months_left, 3) start_time = timezone.make_aware(datetime(2021, 11, 16)) end_time = get_end_time(start_time, 6) fixed_period_permit_start_tomorrow = ParkingPermitFactory( contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=6, ) self.assertEqual(fixed_period_permit_start_tomorrow.months_left, 6) start_time = timezone.make_aware(datetime(2019, 11, 15)) end_time = get_end_time(start_time, 6) fixed_period_permit_started_2_years_ago = ParkingPermitFactory( contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=6, ) self.assertEqual(fixed_period_permit_started_2_years_ago.months_left, 0) start_time = timezone.make_aware(datetime(2019, 11, 15)) open_ended_permit_started_two_years_ago = ParkingPermitFactory( contract_type=ContractType.OPEN_ENDED, start_time=start_time, ) self.assertEqual(open_ended_permit_started_two_years_ago.months_left, None)
def test_should_set_end_time_to_now_if_end_permit_immediately(self): start_time = timezone.make_aware(datetime(2021, 11, 15)) end_time = get_end_time(start_time, 6) permit = ParkingPermitFactory( contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=6, ) permit.end_permit(ParkingPermitEndType.IMMEDIATELY) self.assertEqual( permit.end_time, timezone.make_aware(datetime(2021, 11, 20, 12, 10, 50)))
def test_should_set_end_time_to_period_end_if_end_permit_after_current_period( self): start_time = timezone.make_aware(datetime(2021, 11, 15)) end_time = get_end_time(start_time, 6) permit = ParkingPermitFactory( contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=6, ) permit.end_permit(ParkingPermitEndType.AFTER_CURRENT_PERIOD) self.assertEqual( permit.end_time, timezone.make_aware(datetime(2021, 12, 14, 23, 59, 59, 999999)), )
def test_create_renewable_order_should_raise_error_for_draft_permits(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 permit = ParkingPermitFactory( parking_zone=self.zone, customer=self.customer, contract_type=ContractType.FIXED_PERIOD, status=ParkingPermitStatus.DRAFT, start_time=start_time, end_time=end_time, month_count=6, ) Order.objects.create_for_permits([permit]) with freeze_time(timezone.make_aware(datetime(CURRENT_YEAR, 5, 5))): with self.assertRaises(OrderCreationFailed): Order.objects.create_renewal_order(self.customer)
def test_get_products_with_quantities_should_raise_error_when_products_does_not_cover_permit_duration( self, ): product_detail_list = [ [(date(2021, 1, 10), date(2021, 5, 9)), Decimal("30")], [(date(2021, 5, 10), date(2021, 10, 9)), Decimal("30")], ] self._create_zone_products(self.zone_a, product_detail_list) start_time = timezone.make_aware(datetime(2021, 2, 15)) end_time = get_end_time(start_time, 10) # ends at 2021-2-14, 23:59 permit = ParkingPermitFactory( parking_zone=self.zone_a, contract_type=ContractType.FIXED_PERIOD, start_time=start_time, end_time=end_time, month_count=10, ) with self.assertRaises(ProductCatalogError): permit.get_products_with_quantities()
def test_create_for_customer_should_create_order_with_items(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 permit = ParkingPermitFactory( parking_zone=self.zone, 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_items = order.order_items.all().order_by("-quantity") self.assertEqual(order_items.count(), 2) self.assertEqual(order_items[0].unit_price, Decimal(30)) self.assertEqual(order_items[0].quantity, 4) self.assertEqual(order_items[1].unit_price, Decimal(50)) self.assertEqual(order_items[1].quantity, 2)
def test_parking_permit_change_price_list_when_prices_go_up(self): zone_a_product_list = [ [(date(CURRENT_YEAR, 1, 1), date(CURRENT_YEAR, 6, 30)), Decimal("20")], [(date(CURRENT_YEAR, 7, 1), date(CURRENT_YEAR, 12, 31)), Decimal("30")], ] self._create_zone_products(self.zone_a, zone_a_product_list) zone_b_product_list = [ [(date(CURRENT_YEAR, 1, 1), date(CURRENT_YEAR, 6, 30)), Decimal("30")], [(date(CURRENT_YEAR, 7, 1), date(CURRENT_YEAR, 12, 31)), Decimal("40")], ] self._create_zone_products(self.zone_b, zone_b_product_list) start_time = timezone.make_aware(datetime(CURRENT_YEAR, 1, 1)) end_time = get_end_time(start_time, 12) 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( customer=self.customer, parking_zone=self.zone_a, vehicle=low_emission_vehicle, contract_type=ContractType.FIXED_PERIOD, status=ParkingPermitStatus.VALID, start_time=start_time, end_time=end_time, month_count=12, ) with freeze_time(datetime(CURRENT_YEAR, 4, 15)): price_change_list = permit.get_price_change_list( self.zone_b, False) self.assertEqual(len(price_change_list), 2) self.assertEqual(price_change_list[0]["product"], "Pysäköintialue B") self.assertEqual(price_change_list[0]["previous_price"], Decimal("10")) self.assertEqual(price_change_list[0]["new_price"], Decimal("30")) self.assertEqual(price_change_list[0]["price_change"], Decimal("20")) self.assertEqual(price_change_list[0]["price_change_vat"], Decimal("4.8")) self.assertEqual(price_change_list[0]["month_count"], 2) self.assertEqual(price_change_list[0]["start_date"], date(CURRENT_YEAR, 5, 1)) self.assertEqual(price_change_list[0]["end_date"], date(CURRENT_YEAR, 6, 30)) self.assertEqual(price_change_list[1]["product"], "Pysäköintialue B") self.assertEqual(price_change_list[1]["previous_price"], Decimal("15")) self.assertEqual(price_change_list[1]["new_price"], Decimal("40")) self.assertEqual(price_change_list[1]["price_change"], Decimal("25")) self.assertEqual(price_change_list[1]["price_change_vat"], Decimal("6")) self.assertEqual(price_change_list[1]["month_count"], 6) self.assertEqual(price_change_list[1]["start_date"], date(CURRENT_YEAR, 7, 1)) self.assertEqual(price_change_list[1]["end_date"], date(CURRENT_YEAR, 12, 31))