def test_berth_is_available_rejected_new_lease(date, inactive_status): with freeze_time(date): old_lease = BerthLeaseFactory( status=LeaseStatus.PAID, start_date=calculate_berth_lease_start_date() - relativedelta(years=1), end_date=calculate_berth_lease_end_date() - relativedelta(years=1), ) BerthLeaseFactory( status=inactive_status, berth=old_lease.berth, customer=old_lease.customer ) # Need to fetch the berth from the DB to get the annotated value assert Berth.objects.get(id=old_lease.berth_id).is_available
def test_order_type_renew_berth_order(berth, customer_profile): BerthLeaseFactory( customer=customer_profile, berth=berth, start_date="2020-05-10", end_date="2020-05-15", ) lease = BerthLeaseFactory( customer=customer_profile, berth=berth, start_date="2020-06-10", end_date="2020-06-15", ) order = OrderFactory(lease=lease) assert order.lease_order_type == LeaseOrderType.RENEW_BERTH_ORDER
def test_get_harbor_available_active_berths(api_client, pier, available): harbor = pier.harbor # Add an unavailable berth BerthFactory(pier=pier, is_active=False) # Add a berth and assign it to a lease BerthLeaseFactory(berth=BerthFactory(pier=pier)) query = """ { harbor(id: "%s") { properties { numberOfPlaces numberOfFreePlaces } } } """ % (to_global_id(HarborNode._meta.name, harbor.id), ) executed = api_client.execute(query) assert executed["data"] == { "harbor": { "properties": { "numberOfPlaces": 2, "numberOfFreePlaces": 0 } } }
def test_update_boats_berth_applications(berth_application_with_customer): # Renaming for simplicity application = berth_application_with_customer boat_type = BoatTypeFactory(id=application.boat_type_id) application.boat_name = "New name" application.boat_registration_number = "NUMBER" application.status = ApplicationStatus.OFFER_SENT BerthLeaseFactory( application=application, customer=application.customer, status=LeaseStatus.PAID, boat=BoatFactory( owner=application.customer, registration_number="NUMBER", name="Old name", boat_type=boat_type, ), ) application.save() assert application.customer.boats.count() == 1 call_command("create_boat_for_existing_applications") assert application.customer.boats.count() == 1 boat = application.customer.boats.first() assert boat.registration_number == application.boat_registration_number assert boat.name == application.boat_name == "New name"
def test_create_order_berth_product(api_client): berth_lease = BerthLeaseFactory( start_date=calculate_season_start_date(), berth__berth_type__mooring_type=7 ) customer_id = to_global_id(ProfileNode, berth_lease.customer.id) lease_id = to_global_id(BerthLeaseNode, berth_lease.id) expected_product = BerthProduct.objects.get_in_range( berth_lease.berth.berth_type.width, get_berth_lease_pricing_category(berth_lease), ) expected_product_id = to_global_id(BerthProductNode, expected_product.id) variables = { "leaseId": lease_id, } assert Order.objects.count() == 0 executed = api_client.execute(CREATE_ORDER_MUTATION, input=variables) assert Order.objects.count() == 1 assert executed["data"]["createOrder"]["order"].pop("id") is not None assert executed["data"]["createOrder"]["order"] == { "price": str( expected_product.price_for_tier(berth_lease.berth.pier.price_tier) ), "taxPercentage": str(expected_product.tax_percentage), "customer": {"id": customer_id}, "product": {"id": expected_product_id}, "lease": {"id": variables["leaseId"]}, }
def test_berth_applications_statuses_filter_empty_list(berth_application, api_client): berth_application.lease = BerthLeaseFactory() berth_application.status = ApplicationStatus.HANDLED berth_application.save() empty_filter_str = "" query = BERTH_APPLICATIONS_WITH_STATUSES_FILTER_QUERY % empty_filter_str executed = api_client.execute(query) assert executed["data"] == { "berthApplications": { "edges": [{ "node": { "id": to_global_id(BerthApplicationNode._meta.name, berth_application.id), "status": ApplicationStatus.HANDLED.name, } }] } }
def test_reject_offer(user_api_client): customer = CustomerProfileFactory() due_date = date.today() + relativedelta(days=14) berth_switch_offer = BerthSwitchOfferFactory( customer=customer, due_date=due_date, status=OfferStatus.OFFERED, lease=BerthLeaseFactory( customer=customer, start_date=calculate_berth_lease_start_date(), end_date=calculate_berth_lease_end_date(), status=LeaseStatus.PAID, ), ) variables = { # "offerId": to_global_id(BerthSwitchOfferNode, berth_switch_offer.id), "offerNumber": berth_switch_offer.offer_number, "isAccepted": False, } user_api_client.execute(ACCEPT_BERTH_SWITCH_OFFER_MUTATION, input=variables) berth_switch_offer.refresh_from_db() berth_switch_offer.lease.refresh_from_db() assert berth_switch_offer.status == OfferStatus.REJECTED assert berth_switch_offer.lease.status == LeaseStatus.PAID assert berth_switch_offer.application.status == ApplicationStatus.REJECTED assert BerthLease.objects.all().count() == 1
def test_accept_offer_invalid_status(user_api_client, is_accepted, initial_status): customer = CustomerProfileFactory() due_date = date.today() + relativedelta(days=14) berth_switch_offer = BerthSwitchOfferFactory( customer=customer, due_date=due_date, status=initial_status, lease=BerthLeaseFactory( customer=customer, start_date=calculate_berth_lease_start_date(), end_date=calculate_berth_lease_end_date(), status=LeaseStatus.PAID, ), ) variables = { # "offerId": to_global_id(BerthSwitchOfferNode, berth_switch_offer.id), "offerNumber": berth_switch_offer.offer_number, "isAccepted": is_accepted, } user_api_client.execute(ACCEPT_BERTH_SWITCH_OFFER_MUTATION, input=variables) berth_switch_offer.refresh_from_db() berth_switch_offer.lease.refresh_from_db() assert berth_switch_offer.status == initial_status assert berth_switch_offer.lease.status == LeaseStatus.PAID new_lease = BerthLease.objects.exclude( id=berth_switch_offer.lease_id).first() assert new_lease is None
def test_update_berth_application_no_customer_id( api_client, berth_application_with_customer, status): berth_application_with_customer.lease = BerthLeaseFactory() berth_application_with_customer.status = status berth_application_with_customer.save() application_id = to_global_id(BerthApplicationNode, berth_application_with_customer.id) variables = { "id": application_id, "customerId": None, } executed = api_client.execute(UPDATE_BERTH_APPLICATION_MUTATION, input=variables) if status == ApplicationStatus.PENDING: assert executed == { "data": { "updateBerthApplication": { "berthApplication": { "id": application_id, "customer": None } } } } else: assert_in_errors( "Customer cannot be disconnected from processed applications", executed)
def _get_berth_order_context(subject: str = "Berth order"): customer = CustomerProfileFactory.build() order = OrderFactory.build( customer=customer, product=BerthProductFactory.build(), lease=BerthLeaseFactory.build( customer=customer, # Fixed to a harbor with a real image berth__pier__harbor__image_file="/img/helsinki_harbors/41189.jpg", ), price=Decimal("100"), tax_percentage=Decimal("24.00"), ) optional_services = [ OrderLineFactory.build( order=order, product__service=ProductServiceType.OPTIONAL_SERVICES()[0], price=random_price(), ), OrderLineFactory.build( order=order, product__service=ProductServiceType.OPTIONAL_SERVICES()[1], price=random_price(), ), ] return _get_order_context(subject, order, optional_services)
def test_order_line_product_price_for_company(company_customer): berth_lease = BerthLeaseFactory(customer=company_customer) product = AdditionalProductFactory( price_unit=PriceUnits.AMOUNT, period=PeriodType("year"), service=ProductServiceType.PARKING_PERMIT, ) order = OrderFactory( lease=berth_lease, price=Decimal("0.00"), tax_percentage=Decimal("0.00"), product=None, ) order_line = OrderLineFactory(product=product, order=order) expected_price = (calculate_product_partial_year_price( product.price_value, order_line.order.lease.start_date, order_line.order.lease.end_date, ) * 2) # expect double price assert order_line.price == expected_price assert order_line.tax_percentage == product.tax_percentage
def test_order_line_product_price(period, customer_profile): service = (random.choice(list(ProductServiceType)) if period == "season" else random.choice(ProductServiceType.OPTIONAL_SERVICES())) product = AdditionalProductFactory(price_unit=PriceUnits.AMOUNT, period=PeriodType(period), service=service) order_line = OrderLineFactory( product=product, order__customer=customer_profile, order__lease=BerthLeaseFactory(customer=customer_profile), ) if product.period == PeriodType.MONTH: expected_price = calculate_product_partial_month_price( product.price_value, order_line.order.lease.start_date, order_line.order.lease.end_date, ) elif product.period == PeriodType.SEASON: expected_price = product.price_value elif product.period == PeriodType.YEAR: expected_price = calculate_product_partial_year_price( product.price_value, order_line.order.lease.start_date, order_line.order.lease.end_date, ) assert order_line.price == expected_price assert order_line.tax_percentage == product.tax_percentage
def test_create_boats_berth_applications(berth_application_with_customer): # Renaming for simplicity application = berth_application_with_customer BoatTypeFactory(id=application.boat_type_id) application.status = ApplicationStatus.OFFER_SENT BerthLeaseFactory( application=application, customer=application.customer, status=LeaseStatus.PAID, boat=None, ) application.save() assert application.customer.boats.count() == 0 call_command("create_boat_for_existing_applications") application.refresh_from_db() assert application.customer.boats.count() == 1 boat = application.customer.boats.first() assert boat.registration_number == application.boat_registration_number assert boat.name == application.boat_name assert boat.model == application.boat_model assert boat.width == application.boat_width assert boat.length == application.boat_length assert application.lease.boat == boat
def test_get_berth_with_leases(api_client, berth): berth_lease = BerthLeaseFactory(berth=berth) query = """ { berth(id: "%s") { leases { edges { node { id } } } } } """ % to_global_id(BerthNode._meta.name, berth.id) executed = api_client.execute(query) assert executed["data"]["berth"] == { "leases": { "edges": [{ "node": { "id": to_global_id(BerthLeaseNode._meta.name, berth_lease.id) } }] } }
def test_order_cannot_change_berth_lease(berth_lease): order = OrderFactory(lease=berth_lease) with pytest.raises(ValidationError) as exception: order.lease = BerthLeaseFactory(customer=berth_lease.customer) order.save() errors = str(exception.value) assert "Cannot change the lease associated with this order" in errors
def test_order_manager_only_berth_orders(): OrderFactory(lease=BerthLeaseFactory()) OrderFactory(lease=WinterStorageLeaseFactory()) orders = Order.objects.berth_orders() assert orders.count() == 1 for order in orders: assert order.lease.berth is not None
def test_order_manager_only_winter_storage_orders(): OrderFactory(lease=BerthLeaseFactory()) OrderFactory(lease=WinterStorageLeaseFactory()) orders = Order.objects.winter_storage_orders() assert orders.count() == 1 for order in Order.objects.winter_storage_orders(): assert order.lease.place is not None
def test_create_berth_switch_offer_old_lease(api_client, berth_application, berth): berth_lease = BerthLeaseFactory( start_date=calculate_season_start_date(), end_date=calculate_season_end_date(), status=LeaseStatus.PAID, ) BerthLeaseFactory( customer=berth_lease.customer, start_date=calculate_season_start_date(), end_date=calculate_season_end_date(), status=LeaseStatus.PAID, ) berth_application.customer = berth_lease.customer berth_application.berth_switch = BerthSwitchFactory( berth=berth_lease.berth) berth_application.save() variables = { "applicationId": to_global_id(BerthApplicationNode, berth_application.id), "newBerthId": to_global_id(BerthNode, berth.id), "oldLeaseId": to_global_id(BerthLeaseNode, berth_lease.id), } executed = api_client.execute(CREATE_BERTH_SWITCH_OFFER_MUTATION, input=variables) assert executed["data"]["createBerthSwitchOffer"]["berthSwitchOffer"] == { "status": OfferStatus.DRAFTED.name, "dueDate": None, "application": { "id": variables["applicationId"], "status": "OFFER_GENERATED" }, "customer": { "id": to_global_id(ProfileNode, berth_lease.customer.id) }, "lease": { "id": to_global_id(BerthLeaseNode, berth_lease.id) }, "berth": { "id": variables["newBerthId"] }, }
def test_create_berth_switch_offer_missing_application_switch( api_client, berth_application, berth): berth_lease = BerthLeaseFactory(start_date=calculate_season_start_date(), end_date=calculate_season_end_date()) berth_application.customer = berth_lease.customer berth_application.berth_switch = None berth_application.save() berth_lease.status = LeaseStatus.PAID berth_lease.save() variables = { "applicationId": to_global_id(BerthApplicationNode, berth_application.id), "newBerthId": to_global_id(BerthNode, berth.id), } executed = api_client.execute(CREATE_BERTH_SWITCH_OFFER_MUTATION, input=variables) assert_in_errors("Application must be a switch application", executed)
def test_berth_is_not_available_renew_pending(date): with freeze_time(date): lease = BerthLeaseFactory( status=LeaseStatus.PAID, start_date=calculate_berth_lease_start_date() - relativedelta(years=1), end_date=calculate_berth_lease_end_date() - relativedelta(years=1), ) # Need to fetch the berth from the DB to get the annotated value assert not Berth.objects.get(id=lease.berth_id).is_available
def test_berth_is_not_available_auto_renew_last_season(superuser_api_client, berth): start_date = calculate_berth_lease_start_date() end_date = calculate_berth_lease_end_date() start_date = start_date.replace(year=start_date.year - 1) end_date = end_date.replace(year=end_date.year - 1) BerthLeaseFactory( berth=berth, start_date=start_date, end_date=end_date, status=LeaseStatus.PAID, ) assert not Berth.objects.get(id=berth.id).is_available
def test_order_winter_storage_product_berth_lease_raise_error(): lease = BerthLeaseFactory() with pytest.raises(ValidationError) as exception: OrderFactory(product=WinterStorageProductFactory(), lease=lease, customer=lease.customer) errors = str(exception.value) assert ( "A WinterStorageProduct must be associated with a WinterStorageLease" in errors)
def test_berth_is_available_one_paid_lease(freeze_date, expected_is_available): with freeze_time(freeze_date): lease = BerthLeaseFactory( status=LeaseStatus.PAID, start_date=date(2021, 6, 10), end_date=date(2021, 9, 14), ) # Need to fetch the berth from the DB to get the annotated value assert ( Berth.objects.get(id=lease.berth_id).is_available == expected_is_available )
def test_order_set_status_no_application(berth): lease = BerthLeaseFactory(berth=berth, application=None, status=LeaseStatus.OFFERED) order = OrderFactory( lease=lease, status=OrderStatus.OFFERED, ) order.set_status(OrderStatus.PAID) assert order.status == OrderStatus.PAID assert order.lease.status == LeaseStatus.PAID
def _get_berth_switch_offer_context(subject: str = "Berth offer"): customer = CustomerProfileFactory.build() offer = BerthSwitchOfferFactory.build( customer=customer, application=BerthApplicationFactory.build(customer=customer), berth=BerthFactory.build(), lease=BerthLeaseFactory.build( customer=customer, # Fixed to a harbor with a real image berth__pier__harbor__image_file="/img/helsinki_harbors/41189.jpg", ), ) return _get_offer_context(subject, offer)
def test_berth_applications_statuses_filter_empty(berth_application, api_client): berth_application.lease = BerthLeaseFactory() berth_application.status = ApplicationStatus.HANDLED berth_application.save() status_enum_str = ApplicationStatus.PENDING.name query = BERTH_APPLICATIONS_WITH_STATUSES_FILTER_QUERY % status_enum_str executed = api_client.execute(query) assert executed["data"] == {"berthApplications": {"edges": []}}
def test_create_berth_switch_offer_refresh_profile(api_client, berth_application, berth): faker = Faker("fi_FI") berth_lease = BerthLeaseFactory( start_date=calculate_season_start_date(), end_date=calculate_season_end_date(), status=LeaseStatus.PAID, ) berth_application.customer = berth_lease.customer berth_application.berth_switch = BerthSwitchFactory( berth=berth_lease.berth) berth_application.save() first_name = faker.first_name() last_name = faker.last_name() email = faker.email() phone = faker.phone_number() data = { "id": to_global_id(ProfileNode, berth_lease.customer.id), "first_name": first_name, "last_name": last_name, "primary_email": { "email": email }, "primary_phone": { "phone": phone }, } variables = { "applicationId": to_global_id(BerthApplicationNode, berth_application.id), "newBerthId": to_global_id(BerthNode, berth.id), "profileToken": "profile-token", } with mock.patch( "customers.services.profile.requests.post", side_effect=mocked_response_profile(count=0, data=data, use_edges=False), ): executed = api_client.execute( CREATE_BERTH_SWITCH_OFFER_MUTATION_CUSTOMER_FIELDS, input=variables) assert executed["data"]["createBerthSwitchOffer"]["berthSwitchOffer"] == { "customerFirstName": first_name, "customerLastName": last_name, "customerEmail": email, "customerPhone": phone, }
def test_order_type_additional_product_order(berth, customer_profile): lease = BerthLeaseFactory( customer=customer_profile, berth=berth, start_date="2020-06-10", end_date="2020-06-15", ) order = OrderFactory( order_type=OrderType.ADDITIONAL_PRODUCT_ORDER, lease=lease, price=random_price(), tax_percentage=random_tax(), ) assert order.lease_order_type == LeaseOrderType.INVALID
def test_reject_berth_application_fails_for_lease(api_client, berth_application, customer_profile): variables = { "id": to_global_id(BerthApplicationNode, berth_application.id), } BerthLeaseFactory(application=berth_application) executed = api_client.execute(REJECT_BERTH_APPLICATION_MUTATION, input=variables) assert_in_errors("Application has a lease", executed) assert (BerthApplication.objects.filter( status=ApplicationStatus.NO_SUITABLE_BERTHS).count() == 0)
def test_berth_is_available_last_season_invalid_status( superuser_api_client, berth, status ): start_date = calculate_berth_lease_start_date() end_date = calculate_berth_lease_end_date() start_date = start_date.replace(year=start_date.year - 1) end_date = end_date.replace(year=end_date.year - 1) BerthLeaseFactory( berth=berth, start_date=start_date, end_date=end_date, status=LeaseStatus(status), ) assert Berth.objects.get(id=berth.id).is_available