Coverage for app/tests/delivery_fee/calculation_steps/test_number_of_items_fee.py: 100%

53 statements  

« 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 NumberOfItemsFee, 

5) 

6from app.delivery_fee import settings as settings 

7 

8 

9@pytest.fixture 

10def number_of_items_fee(): 

11 return NumberOfItemsFee(settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS) 

12 

13 

14def test__number_of_items_fee_for_no_surcharge(number_of_items_fee: NumberOfItemsFee): 

15 expected_fee = 0 

16 

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 = number_of_items_fee.calculate(order_info) 

24 

25 # Number of items is 0, so no surcharge is applied. 

26 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee) 

27 

28 

29def test__number_of_items_fee_for_no_surcharge_with_edge_value(number_of_items_fee: NumberOfItemsFee): 

30 expected_fee = 0 

31 

32 order_info = OrderInfo( 

33 cart_value=0, 

34 delivery_distance=0, 

35 number_of_items=settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.number_of_items_surcharge_threshold, 

36 time="2024-01-15T13:00:00Z" 

37 ) 

38 delivery_fee = number_of_items_fee.calculate(order_info) 

39 

40 # Number of items is 4, so no surcharge is applied. 

41 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee) 

42 

43 

44def test__number_of_items_fee_no_surcharge_lower_than_threshold(number_of_items_fee: NumberOfItemsFee): 

45 expected_fee = 0 

46 order_info = OrderInfo( 

47 cart_value=0, 

48 delivery_distance=0, 

49 number_of_items=1, 

50 time="2024-01-15T13:00:00Z" 

51 ) 

52 delivery_fee = number_of_items_fee.calculate(order_info) 

53 

54 # Number of items is 1, so surcharge is applied. 

55 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee) 

56 

57 

58def test__number_of_items_fee_no_surcharge_applied(number_of_items_fee: NumberOfItemsFee): 

59 expected_fee = settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.surcharge_per_item_over_threshold 

60 

61 order_info = OrderInfo( 

62 cart_value=0, 

63 delivery_distance=0, 

64 number_of_items=settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.number_of_items_surcharge_threshold + 1, 

65 time="2024-01-15T13:00:00Z" 

66 ) 

67 delivery_fee = number_of_items_fee.calculate(order_info) 

68 

69 # Number of items is 5, so surcharge is applied. 

70 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee) 

71 

72 

73def test__number_of_items_fee_bulk_charge_not_applied_for_edge_before_bulk(number_of_items_fee: NumberOfItemsFee): 

74 expected_fee = (settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.surcharge_per_item_over_threshold * 

75 (settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.bulk_charge_threshold - 

76 settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.number_of_items_surcharge_threshold)) 

77 

78 order_info = OrderInfo( 

79 cart_value=0, 

80 delivery_distance=0, 

81 number_of_items=settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.bulk_charge_threshold, 

82 time="2024-01-15T13:00:00Z" 

83 ) 

84 delivery_fee = number_of_items_fee.calculate(order_info) 

85 

86 # Number of items is 12, so surcharge is applied. Bulk charge 

87 # is not applied since number of items is not greater than 12. 

88 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee) 

89 

90 

91def test__number_of_items_fee_bulk_charge_not_applied_for_items_below_threshold(number_of_items_fee: NumberOfItemsFee): 

92 number_of_items = 11 

93 items_over_surcharge_threshold = (number_of_items - 

94 settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.number_of_items_surcharge_threshold) 

95 expected_fee = (items_over_surcharge_threshold * 

96 settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.surcharge_per_item_over_threshold) 

97 

98 order_info = OrderInfo( 

99 cart_value=0, 

100 delivery_distance=0, 

101 number_of_items=number_of_items, 

102 time="2024-01-15T13:00:00Z" 

103 ) 

104 delivery_fee = number_of_items_fee.calculate(order_info) 

105 

106 # Number of items is 11, so surcharge is applied. 

107 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee) 

108 

109 

110def test__number_of_items_fee_bulk_charge_applied_for_just_over_threshold(number_of_items_fee: NumberOfItemsFee): 

111 number_of_items = 13 

112 items_over_surcharge_threshold = (number_of_items - 

113 settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.number_of_items_surcharge_threshold) 

114 expected_fee = ((items_over_surcharge_threshold * 

115 settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.surcharge_per_item_over_threshold) + 

116 settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.bulk_charge) 

117 order_info = OrderInfo( 

118 cart_value=0, 

119 delivery_distance=0, 

120 number_of_items=13, 

121 time="2024-01-15T13:00:00Z" 

122 ) 

123 delivery_fee = number_of_items_fee.calculate(order_info) 

124 # Number of items is 13, so surcharge is applied. Bulk charge 

125 # is applied since number of items is greater than 12. 

126 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee) 

127 

128 

129def test__number_of_items_fee_bulk_charge_applied_for_bigger_number_of_items(number_of_items_fee: NumberOfItemsFee): 

130 number_of_items = 100 

131 items_over_surcharge_threshold = (number_of_items - 

132 settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.number_of_items_surcharge_threshold) 

133 expected_fee = ((items_over_surcharge_threshold * 

134 settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.surcharge_per_item_over_threshold) + 

135 settings.NUMBER_OF_ITEMS_CONFIG_OPTIONS.bulk_charge) 

136 order_info = OrderInfo( 

137 cart_value=0, 

138 delivery_distance=0, 

139 number_of_items=number_of_items, 

140 time="2024-01-15T13:00:00Z" 

141 ) 

142 delivery_fee = number_of_items_fee.calculate(order_info) 

143 

144 # Bulk charge and surcharge is applied. 

145 assert delivery_fee == DeliveryFee(delivery_fee=expected_fee)