예제 #1
0
    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)))
예제 #2
0
    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_get_changed_reversion_comment(self):
     with reversion.create_revision():
         permit = ParkingPermitFactory(status=ParkingPermitStatus.DRAFT)
     with reversion.create_revision():
         permit.status = ParkingPermitStatus.VALID
         permit.save(update_fields=["status"])
         comment = get_reversion_comment(EventType.CHANGED, permit)
         self.assertEqual(comment, "CHANGED|Status: DRAFT --> VALID")
예제 #4
0
    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_get_changelogs(self):
        user = UserFactory()
        with reversion.create_revision():
            permit = ParkingPermitFactory()
            reversion.set_user(user)
            comment = get_reversion_comment(EventType.CREATED, permit)
            reversion.set_comment(comment)
        with reversion.create_revision():
            permit.status = ParkingPermitStatus.VALID
            permit.save(update_fields=["status"])
            comment = get_reversion_comment(EventType.CHANGED, permit)
            reversion.set_comment(comment)

        changelogs = get_obj_changelogs(permit)
        self.assertEqual(len(changelogs), 2)
        # most recent changelog is in the beginning of th elist
        self.assertEqual(changelogs[0]["event"], EventType.CHANGED)
        self.assertEqual(changelogs[1]["event"], EventType.CREATED)
예제 #6
0
 def test_create_renewable_order_should_raise_error_for_open_ended_permits(
         self):
     start_time = timezone.make_aware(datetime(CURRENT_YEAR, 3, 15))
     end_time = get_end_time(start_time, 6)  # end at 2022-09-14 23:59
     permit = ParkingPermitFactory(
         parking_zone=self.zone,
         customer=self.customer,
         contract_type=ContractType.OPEN_ENDED,
         status=ParkingPermitStatus.DRAFT,
         start_time=start_time,
         end_time=end_time,
         month_count=6,
     )
     Order.objects.create_for_permits([permit])
     permit.status = ParkingPermitStatus.VALID
     permit.save()
     with freeze_time(timezone.make_aware(datetime(CURRENT_YEAR, 5, 5))):
         with self.assertRaises(OrderCreationFailed):
             Order.objects.create_renewal_order(self.customer)
class UpdateCustomerPermitTestCase(TestCase):
    def setUp(self):
        self.cus_a = CustomerFactory(first_name="Firstname A", last_name="")
        self.cus_b = CustomerFactory(first_name="Firstname B", last_name="")

        self.c_a_draft = ParkingPermitFactory(
            customer=self.cus_a,
            status=DRAFT,
            address=self.cus_a.primary_address,
            parking_zone=self.cus_a.primary_address.zone,
        )
        self.c_a_can = ParkingPermitFactory(customer=self.cus_a, status=CLOSED)
        self.c_b_valid = ParkingPermitFactory(customer=self.cus_b, status=VALID)
        self.c_b_draft = ParkingPermitFactory(customer=self.cus_b, status=DRAFT)
        self.c_a_draft_sec = ParkingPermitFactory(
            customer=self.cus_a,
            status=DRAFT,
            primary_vehicle=False,
            address=self.cus_a.primary_address,
            parking_zone=self.cus_a.primary_address.zone,
        )

    def test_can_not_update_others_permit(self):
        data = {"consent_low_emission_accepted": True}
        with self.assertRaises(ObjectDoesNotExist):
            CustomerPermit(self.cus_a.id).update(data, self.c_b_draft.id)

    def test_can_update_consent_low_emission_accepted_for_a_permit(self):
        data = {"consent_low_emission_accepted": True}
        self.assertEqual(self.c_a_draft.consent_low_emission_accepted, False)
        res = CustomerPermit(self.cus_a.id).update(data, self.c_a_draft.id)
        self.assertEqual(res.consent_low_emission_accepted, True)

    def test_can_not_update_consent_low_emission_accepted_for_closed(
        self,
    ):
        data = {"consent_low_emission_accepted": True}
        with self.assertRaises(ObjectDoesNotExist):
            CustomerPermit(self.cus_a.id).update(data, self.c_a_can.id)

    def test_toggle_primary_vehicle_of_customer_a(self):
        data = {"primary_vehicle": True}
        self.assertEqual(self.c_a_draft.primary_vehicle, True)
        self.assertEqual(self.c_a_draft_sec.primary_vehicle, False)
        pri, sec = CustomerPermit(self.cus_a.id).update(data, self.c_a_can.id)

        # Check if they are same
        self.assertEqual(pri.id, self.c_a_draft.id)
        self.assertEqual(sec.id, self.c_a_draft_sec.id)

        self.assertEqual(pri.primary_vehicle, False)
        self.assertEqual(sec.primary_vehicle, True)

    def test_can_not_update_address_id_of_drafts_if_not_in_his_address(self):
        address = AddressFactory()
        data = {"address_id": str(address.id)}
        with self.assertRaisesMessage(InvalidUserAddress, "Invalid user address."):
            CustomerPermit(self.cus_a.id).update(data)

    def test_can_not_update_address_id_of_valid_if_not_in_his_address(self):
        data = {"address_id": str(self.cus_b.other_address.id)}
        with self.assertRaisesMessage(InvalidUserAddress, "Invalid user address."):
            CustomerPermit(self.cus_a.id).update(data)

    def test_can_update_zone_id_of_all_drafts_with_zone_that_either_of_his_address_has(
        self,
    ):
        sec_add_id = self.cus_a.other_address.id
        pri_add_id = self.cus_a.primary_address.id
        data = {"address_id": str(sec_add_id)}
        self.assertEqual(self.c_a_draft.address_id, pri_add_id)
        self.assertEqual(self.c_a_draft_sec.address_id, pri_add_id)
        results = CustomerPermit(self.cus_a.id).update(data)
        for result in results:
            self.assertEqual(result.address_id, str(sec_add_id))

    def test_can_not_update_zone_if_it_has_payment_in_progress_or_valid_primary_permit(
        self,
    ):
        for status in [PAYMENT_IN_PROGRESS, VALID]:
            self.c_a_draft.status = status
            self.c_a_draft.save(update_fields=["status"])
            data = {"address_id": str(self.cus_a.other_address.id)}
            msg = f"You can buy permit only for address {self.cus_a.primary_address}"
            with self.assertRaisesMessage(InvalidUserAddress, msg):
                CustomerPermit(self.cus_a.id).update(data)

    def test_all_draft_permit_to_have_same_immediately_start_type(self):
        tomorrow = next_day()
        data = {"start_type": IMMEDIATELY}
        permits = CustomerPermit(self.cus_a.id).update(data)
        for permit in permits:
            self.assertEqual(permit.start_type, IMMEDIATELY)
            self.assertGreaterEqual(permit.start_time, tomorrow)

    def test_draft_permits_to_start_after_three_days(self):
        after_3_days = get_future(3)
        utc_format = "%Y-%m-%dT%H:%M:%S.%fZ"
        data = {
            "start_type": FROM,
            "start_time": after_3_days.astimezone(tz.utc).strftime(utc_format),
        }
        permits = CustomerPermit(self.cus_a.id).update(data)
        for permit in permits:
            self.assertEqual(permit.start_type, FROM)
            self.assertGreaterEqual(permit.start_time, after_3_days)

    def test_draft_permits_to_be_max_2_weeks_in_future(self):
        after_3_weeks = get_end_time(next_day(), 3)
        utc_format = "%Y-%m-%dT%H:%M:%S.%fZ"
        data = {
            "start_type": FROM,
            "start_time": after_3_weeks.astimezone(tz.utc).strftime(utc_format),
        }
        permits = CustomerPermit(self.cus_a.id).update(data)
        time_after_2_weeks = get_end_time(next_day(), 2)
        for permit in permits:
            self.assertEqual(permit.start_type, FROM)
            self.assertLessEqual(permit.start_time, time_after_2_weeks)

    def test_should_have_same_contract_type_for_bulk_add(self):
        for contract in [OPEN_ENDED, FIXED_PERIOD]:
            data = {"contract_type": contract}
            permits = CustomerPermit(self.cus_a.id).update(data)
            for permit in permits:
                self.assertEqual(permit.contract_type, contract)
                self.assertEqual(permit.month_count, 1)

    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)

    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_non_draft_permit_contract_type_can_not_be_edited(self):
        customer = CustomerFactory(first_name="Customer 2", last_name="")
        permit = ParkingPermitFactory(customer=customer, status=VALID)
        data = {"contract_type": FIXED_PERIOD}
        permit_id = str(permit.id)
        msg = "This is not a draft permit and can not be edited"
        with self.assertRaisesMessage(NonDraftPermitUpdateError, msg):
            CustomerPermit(customer.id).update(data, permit_id=permit_id)

    def test_throw_error_for_missing_contract_type(self):
        msg = "Contract type is required"
        with self.assertRaisesMessage(InvalidContractType, msg):
            data = {"month_count": 1}
            CustomerPermit(self.cus_a.id).update(data, permit_id=str(self.c_a_draft.id))

    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_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_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_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)