Coverage for app/tests/delivery_fee/model/test_order_info.py: 100%

43 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-11-27 09:26 +0000

1import pytest 

2from fastapi.testclient import TestClient 

3from http import HTTPStatus 

4from app.main import app 

5 

6 

7client = TestClient(app) 

8 

9 

10edge_cases_for_numeric_fields = [-1, -10, 100.5, None, 

11 [1, 2, 3], [], {}, {"a": 1, "b": 2}, "invalid"] 

12 

13 

14######################################################################################### 

15# Test cart_value field 

16######################################################################################### 

17 

18def test__cart_value_with_valid_data(): 

19 # Try with valid data 

20 string_res = client.post("/api/delivery/calculate_delivery_fee/", 

21 json={ 

22 "cart_value": 0, 

23 "delivery_distance": 0, 

24 "number_of_items": 0, 

25 "time": "2024-01-15T13:00:00Z" 

26 }) 

27 assert string_res.status_code == HTTPStatus.OK 

28 

29 

30@pytest.mark.parametrize("cart_value", edge_cases_for_numeric_fields) 

31def test__cart_value__with_invalid_data_types(cart_value): 

32 # Try with each of the cart_value values in the list above 

33 string_res = client.post("/api/delivery/calculate_delivery_fee/", 

34 json={ 

35 "cart_value": cart_value, 

36 "delivery_distance": 0, 

37 "number_of_items": 0, 

38 "time": "2024-01-15T13:00:00Z" 

39 }) 

40 assert string_res.status_code == HTTPStatus.UNPROCESSABLE_ENTITY 

41 

42 

43def test__cart_value__edge_case_boolean(): 

44 # Try boolean 

45 bool_res = client.post("/api/delivery/calculate_delivery_fee/", 

46 json={ 

47 "cart_value": True, 

48 "delivery_distance": 0, 

49 "number_of_items": 0, 

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

51 }) 

52 # Expecting ok since it is quite common to interpret 

53 # True as 1 and False as 0 

54 assert bool_res.status_code == HTTPStatus.OK 

55 

56 

57######################################################################################### 

58# Test delivery_distance field 

59######################################################################################### 

60 

61def test__delivery_distance_with_valid_data(): 

62 # Try with valid data 

63 string_res = client.post("/api/delivery/calculate_delivery_fee/", 

64 json={ 

65 "cart_value": 0, 

66 "delivery_distance": 0, 

67 "number_of_items": 0, 

68 "time": "2024-01-15T13:00:00Z" 

69 }) 

70 assert string_res.status_code == HTTPStatus.OK 

71 

72 

73@pytest.mark.parametrize("delivery_distance", edge_cases_for_numeric_fields) 

74def test__delivery_distance__with_invalid_data_types(delivery_distance): 

75 # Try with each of the delivery_distance values in the list above 

76 string_res = client.post("/api/delivery/calculate_delivery_fee/", 

77 json={ 

78 "cart_value": 0, 

79 "delivery_distance": delivery_distance, 

80 "number_of_items": 0, 

81 "time": "2024-01-15T13:00:00Z" 

82 }) 

83 assert string_res.status_code == HTTPStatus.UNPROCESSABLE_ENTITY 

84 

85 

86def test__delivery_distance__edge_case_boolean(): 

87 # Try boolean 

88 bool_res = client.post("/api/delivery/calculate_delivery_fee/", 

89 json={ 

90 "cart_value": 0, 

91 "delivery_distance": False, 

92 "number_of_items": 0, 

93 "time": "2024-01-15T13:00:00Z" 

94 }) 

95 # Expecting ok since it is quite common to interpret 

96 # True as 1 and False as 0 

97 assert bool_res.status_code == HTTPStatus.OK 

98 

99 

100######################################################################################### 

101# Test number_of_items field 

102######################################################################################### 

103 

104def test__number_of_items_with_valid_data(): 

105 # Try with valid data 

106 string_res = client.post("/api/delivery/calculate_delivery_fee/", 

107 json={ 

108 "cart_value": 0, 

109 "delivery_distance": 0, 

110 "number_of_items": 0, 

111 "time": "2024-01-15T13:00:00Z" 

112 }) 

113 assert string_res.status_code == HTTPStatus.OK 

114 

115 

116@pytest.mark.parametrize("number_of_items", edge_cases_for_numeric_fields) 

117def test__number_of_items__with_invalid_data_types(number_of_items): 

118 # Try with each of the number_of_items values in the list above 

119 response = client.post("/api/delivery/calculate_delivery_fee/", 

120 json={ 

121 "cart_value": 0, 

122 "delivery_distance": 0, 

123 "number_of_items": number_of_items, 

124 "time": "2024-01-15T13:00:00Z" 

125 }) 

126 assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY 

127 

128 

129def test__number_of_items__edge_case_boolean(): 

130 # Try boolean 

131 bool_res = client.post("/api/delivery/calculate_delivery_fee/", 

132 json={ 

133 "cart_value": 0, 

134 "delivery_distance": 0, 

135 "number_of_items": False, 

136 "time": "2024-01-15T13:00:00Z" 

137 }) 

138 # Expecting ok since it is quite common to interpret 

139 # True as 1 and False as 0 

140 assert bool_res.status_code == HTTPStatus.OK 

141 

142 

143######################################################################################### 

144# Test time field 

145######################################################################################### 

146 

147def test__time_with_valid_data(): 

148 # Try with valid data 

149 string_res = client.post("/api/delivery/calculate_delivery_fee/", 

150 json={ 

151 "cart_value": 0, 

152 "delivery_distance": 0, 

153 "number_of_items": 0, 

154 "time": "2024-01-15T13:00:00Z" 

155 }) 

156 assert string_res.status_code == HTTPStatus.OK 

157 

158 

159@pytest.mark.parametrize("time", [edge_cases_for_numeric_fields]+[True]) 

160def test__time__invalid_data_types(time): 

161 # Try with each of the time values in the list above 

162 string_res = client.post("/api/delivery/calculate_delivery_fee/", 

163 json={ 

164 "cart_value": 0, 

165 "delivery_distance": 0, 

166 "number_of_items": 0, 

167 "time": time 

168 }) 

169 assert string_res.status_code == HTTPStatus.UNPROCESSABLE_ENTITY