Coverage for app/tests/delivery_fee/calculation_steps/test_delivery_distance_fee.py: 100%
39 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-11-27 09:26 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-11-27 09:26 +0000
1import pytest
2from app.delivery_fee.models import DeliveryFee, OrderInfo
3from app.delivery_fee.fee_calculation_steps import (
4 DeliveryDistanceFee,
5)
6import app.delivery_fee.settings as settings
9@pytest.fixture
10def delivery_distance_fee():
11 return DeliveryDistanceFee(settings.DELIVERY_DISTANCE_CONFIG_OPTIONS)
14def test__delivery_distance_fee_not_applied(delivery_distance_fee: DeliveryDistanceFee):
15 expected_fee = 0
17 order_info = OrderInfo(
18 cart_value=0,
19 delivery_distance=0,
20 number_of_items=0,
21 time="2024-01-15T13:00:00Z"
22 )
23 delivery_fee = delivery_distance_fee.calculate(order_info)
25 # Delivery distance is 0, so no surcharge is applied.
26 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee)
29def test__delivery_distance_fee_applied_for_first_1km_for_low_value(delivery_distance_fee: DeliveryDistanceFee):
30 expected_fee = settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.delivery_distance_surcharge_for_low_threshold
32 order_info = OrderInfo(
33 cart_value=0,
34 delivery_distance=1,
35 number_of_items=0,
36 time="2024-01-15T13:00:00Z"
37 )
38 delivery_fee = delivery_distance_fee.calculate(order_info)
40 # Delivery distance is 0, so no surcharge is applied.
41 assert delivery_fee == DeliveryFee(
42 delivery_fee=expected_fee)
45def test__delivery_distance_fee_applied_for_first_1km_for_edge_value(delivery_distance_fee: DeliveryDistanceFee):
46 expected_fee = settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.delivery_distance_surcharge_for_low_threshold
48 order_info = OrderInfo(
49 cart_value=0,
50 delivery_distance=settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.delivery_distance_low_threshold,
51 number_of_items=0,
52 time="2024-01-15T13:00:00Z"
53 )
54 delivery_fee = delivery_distance_fee.calculate(order_info)
56 # Delivery distance is 1km, so surcharge is applied.
57 assert delivery_fee == DeliveryFee(
58 delivery_fee=expected_fee)
61def test__delivery_distance_fee_applied_for_first_1km_with_additional_fee(delivery_distance_fee: DeliveryDistanceFee):
62 expected_fee = settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.delivery_distance_surcharge_for_low_threshold + \
63 settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.additional_fee
65 order_info = OrderInfo(
66 cart_value=0,
67 delivery_distance=settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.delivery_distance_low_threshold + 1,
68 number_of_items=0,
69 time="2024-01-15T13:00:00Z"
70 )
71 delivery_fee = delivery_distance_fee.calculate(order_info)
73 # Delivery distance is 1.5km, so surcharge is applied.
74 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee)
77def test__delivery_distance_fee_applied_for_first_1km_with_additional_fee_for_edge_value(delivery_distance_fee: DeliveryDistanceFee):
78 expected_fee = settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.delivery_distance_surcharge_for_low_threshold + \
79 settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.additional_fee
81 order_info = OrderInfo(
82 cart_value=0,
83 delivery_distance=delivery_distance_fee.config_options.delivery_distance_low_threshold +
84 settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.additional_fee_applied_per_meters_traveled,
85 number_of_items=0,
86 time="2024-01-15T13:00:00Z"
87 )
88 delivery_fee = delivery_distance_fee.calculate(order_info)
90 # Delivery distance is 1.5km, surcharge should be 3 euros.
91 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee)
94def test__delivery_distance_fee_applied_for_first_1km_with_additional_fee_for_higher_value(delivery_distance_fee: DeliveryDistanceFee):
95 additional_fee_multiplier = 5
96 expected_fee = (settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.delivery_distance_surcharge_for_low_threshold +
97 (settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.additional_fee*additional_fee_multiplier))
99 order_info = OrderInfo(
100 cart_value=0,
101 delivery_distance=(settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.delivery_distance_low_threshold +
102 (settings.DELIVERY_DISTANCE_CONFIG_OPTIONS.additional_fee_applied_per_meters_traveled*additional_fee_multiplier)),
103 number_of_items=0,
104 time="2024-01-15T13:00:00Z"
105 )
107 delivery_distance_fee = DeliveryDistanceFee()
108 delivery_fee = delivery_distance_fee.calculate(order_info)
109 # delivery fee should be 5*1€ + 2€ = 7€
110 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee)