Coverage for test/test_car/test_car.py: 99%

166 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-09 21:08 +0000

1"""Tests the creation of Car onjects.""" 

2import unittest 

3from unittest.mock import Mock 

4from datetime import datetime 

5 

6from car import Car 

7from serviceable import Serviceable 

8 

9from car_factory import CarFactory 

10factory = CarFactory() 

11 

12class TestCarImplementation(unittest.TestCase): 

13 """Tests the appropriate implementation of the car class.""" 

14 def setUp(self): 

15 """set up the test fixtures""" 

16 self.mock_engine = Mock() 

17 self.mock_battery = Mock() 

18 self.mock_tire = Mock() 

19 

20 self.car = Car(self.mock_engine, self.mock_battery, self.mock_tire) 

21 

22 def test_car_is_subclass_of_serviceable(self): 

23 """Test if Car is a subclass of Serviceable.""" 

24 self.assertTrue(issubclass(Car, Serviceable)) 

25 

26 def test_car_has_battery_attribute(self): 

27 """Test if Car has a battery attribute""" 

28 self.assertTrue(hasattr(self.car, 'battery'), 

29 "Car object doesn't have a battery attribute") 

30 

31 def test_car_has_engine_attribute(self): 

32 """Test if Car has an engine attribute""" 

33 self.assertTrue(hasattr(self.car, 'engine'), 

34 "Car object doesn't have an engine attribute") 

35 

36 def test_car_has_tire_attribute(self): 

37 """Test if Car has a tire attribute""" 

38 self.assertTrue(hasattr(self.car, 'tire'), 

39 "Car object doesn't have a tire attribute") 

40 

41 def test_car_has_needs_service_method(self): 

42 """Test if Car has a needs_service method""" 

43 self.assertTrue(hasattr(self.car, 'needs_service'), 

44 "Car object doesn't have a needs_service method.") 

45 

46class TestCalliope(unittest.TestCase): 

47 """Tests the servicing criteria of a calliope car""" 

48 def test_battery_needs_service(self): 

49 """Test Need for Calliope Car Servicing -> Consideration: Battery.""" 

50 today = datetime.today().date() 

51 last_service_date = today.replace(year=today.year - 3) 

52 current_mileage = 0 

53 last_service_mileage = 0 

54 

55 car = factory.create_calliope(today, last_service_date, 

56 current_mileage, 

57 last_service_mileage) 

58 self.assertTrue(car.needs_service()) 

59 

60 def test_battery_needs_no_service(self): 

61 """Test No Need For Calliope Car Servicing. -> Consideration: Battery.""" 

62 today = datetime.today().date() 

63 last_service_date = today.replace(year=today.year - 1) 

64 current_mileage = 0 

65 last_service_mileage = 0 

66 

67 car = factory.create_calliope(today, 

68 last_service_date, 

69 current_mileage, 

70 last_service_mileage) 

71 self.assertFalse(car.needs_service()) 

72 

73 def test_engine_needs_service(self): 

74 """Test Need for Calliope Car Servicing -> Consideration: Engine.""" 

75 today = datetime.today().date() 

76 last_service_date = datetime.today().date() 

77 current_mileage = 30001 

78 last_service_mileage = 0 

79 

80 car = factory.create_calliope(today, 

81 last_service_date, 

82 current_mileage, 

83 last_service_mileage) 

84 self.assertTrue(car.needs_service()) 

85 

86 def test_engine_needs_no_service(self): 

87 """Test No Need for Calliope Car Servicing -> Consideration: Engine.""" 

88 today = datetime.today().date() 

89 last_service_date = datetime.today().date() 

90 current_mileage = 30000 

91 last_service_mileage = 0 

92 

93 car = factory.create_calliope(today, 

94 last_service_date, 

95 current_mileage, 

96 last_service_mileage) 

97 self.assertFalse(car.needs_service()) 

98 

99 

100class TestGlissade(unittest.TestCase): 

101 """Tests the servicing criteria of a Glissade car.""" 

102 def test_battery_needs_service(self): 

103 """Test Need for Glissade Car Servicing -> Consideration: Battery""" 

104 today = datetime.today().date() 

105 last_service_date = today.replace(year=today.year - 3) 

106 current_mileage = 0 

107 last_service_mileage = 0 

108 

109 car = factory.create_glissade(today, 

110 last_service_date, 

111 current_mileage, 

112 last_service_mileage) 

113 self.assertTrue(car.needs_service()) 

114 

115 def test_battery_needs_no_service(self): 

116 """Test No Need For Glissade Car Servicing. -> Consideration: Battery""" 

117 today = datetime.today().date() 

118 last_service_date = today.replace(year=today.year - 1) 

119 current_mileage = 0 

120 last_service_mileage = 0 

121 

122 car = factory.create_glissade(today, 

123 last_service_date, 

124 current_mileage, 

125 last_service_mileage) 

126 self.assertFalse(car.needs_service()) 

127 

128 def test_engine_needs_service(self): 

129 """Test Need for Glissade Car Servicing -> Consideration: Engine""" 

130 today = datetime.today().date() 

131 last_service_date = datetime.today().date() 

132 current_mileage = 60001 

133 last_service_mileage = 0 

134 

135 car = factory.create_glissade(today, 

136 last_service_date, 

137 current_mileage, 

138 last_service_mileage) 

139 self.assertTrue(car.needs_service()) 

140 

141 def test_engine_needs_no_service(self): 

142 """Test No Need for Glissade Car Servicing -> Consideration: Engine.""" 

143 today = datetime.today().date() 

144 last_service_date = datetime.today().date() 

145 current_mileage = 60000 

146 last_service_mileage = 0 

147 

148 car = factory.create_glissade(today, 

149 last_service_date, 

150 current_mileage, 

151 last_service_mileage) 

152 self.assertFalse(car.needs_service()) 

153 

154 

155class TestPalindrome(unittest.TestCase): 

156 """Test Servicing criteria for a Palindrome Car""" 

157 def test_battery_needs_service(self): 

158 """Test Need for Palindrome Car Servicing -> Consideration: Battery.""" 

159 today = datetime.today().date() 

160 last_service_date = today.replace(year=today.year - 5) 

161 warning_light_is_on = False 

162 

163 car = factory.create_palindrome(today, 

164 last_service_date, 

165 warning_light_is_on) 

166 self.assertTrue(car.needs_service()) 

167 

168 def test_battery_needs_no_service(self): 

169 """Test No Need for Palindrome Car Servicing -> Consideration: Battery.""" 

170 today = datetime.today().date() 

171 last_service_date = today.replace(year=today.year - 1) 

172 warning_light_is_on = False 

173 

174 car = factory.create_palindrome(today, 

175 last_service_date, 

176 warning_light_is_on) 

177 

178 self.assertFalse(car.battery.needs_service()) 

179 

180 def test_engine_needs_service(self): 

181 """Test Need for Palindrome Car Servicing -> Consideration: Engine.""" 

182 today = datetime.today().date() 

183 last_service_date = datetime.today().date() 

184 warning_light_is_on = True 

185 

186 car = factory.create_palindrome(today, 

187 last_service_date, 

188 warning_light_is_on) 

189 self.assertTrue(car.needs_service()) 

190 

191 def test_engine_needs_no_service(self): 

192 """Test No Need for Palindrome Car Servicing -> Consideration: Engine.""" 

193 today = datetime.today().date() 

194 last_service_date = datetime.today().date() 

195 warning_light_is_on = False 

196 

197 car = factory.create_palindrome(today, 

198 last_service_date, 

199 warning_light_is_on) 

200 self.assertFalse(car.needs_service()) 

201 

202 

203class TestRorschach(unittest.TestCase): 

204 """Test Servicing criteria for a Rorschach Car.""" 

205 def test_battery_needs_service(self): 

206 """Test Need for Rorschach Car Servicing -> Consideration: Battery""" 

207 today = datetime.today().date() 

208 last_service_date = today.replace(year=today.year - 5) 

209 current_mileage = 0 

210 last_service_mileage = 0 

211 

212 car = factory.create_rorschach(today, 

213 last_service_date, 

214 current_mileage, last_service_mileage) 

215 self.assertTrue(car.needs_service()) 

216 

217 def test_battery_needs_no_service(self): 

218 """Test No Need for Rorschach Car Servicing -> Consideration: Battery.""" 

219 today = datetime.today().date() 

220 last_service_date = today.replace(year=today.year - 3) 

221 current_mileage = 0 

222 last_service_mileage = 0 

223 

224 car = factory.create_rorschach(today, 

225 last_service_date, 

226 current_mileage, 

227 last_service_mileage) 

228 self.assertFalse(car.needs_service()) 

229 

230 def test_engine_needs_service(self): 

231 """Test Need for Rorschach Car Servicing -> Consideration: Engine""" 

232 today = datetime.today().date() 

233 last_service_date = datetime.today().date() 

234 current_mileage = 60001 

235 last_service_mileage = 0 

236 

237 car = factory.create_rorschach(today, 

238 last_service_date, 

239 current_mileage, 

240 last_service_mileage) 

241 self.assertTrue(car.needs_service()) 

242 

243 def test_engine_needs_no_service(self): 

244 """Test No Need for Rorschach Car Servicing -> Consideration: Engine""" 

245 today = datetime.today().date() 

246 last_service_date = datetime.today().date() 

247 current_mileage = 60000 

248 last_service_mileage = 0 

249 

250 car = factory.create_rorschach(today, 

251 last_service_date, 

252 current_mileage, 

253 last_service_mileage) 

254 

255 self.assertFalse(car.needs_service()) 

256 

257 

258class TestThovex(unittest.TestCase): 

259 """Test Servicing criteria for a Thovex Car""" 

260 def test_battery_needs_service(self): 

261 """Test the Need for Thovex Car Servicing -> Consideration: Battery""" 

262 today = datetime.today().date() 

263 last_service_date = today.replace(year=today.year - 5) 

264 current_mileage = 0 

265 last_service_mileage = 0 

266 

267 car = factory.create_thovex(today, 

268 last_service_date, 

269 current_mileage, 

270 last_service_mileage) 

271 self.assertTrue(car.needs_service()) 

272 

273 def test_battery_should_not_be_serviced(self): 

274 """Test No Need for Thovex Car Servicing -> Consideration: Battery""" 

275 today = datetime.today().date() 

276 last_service_date = today.replace(year=today.year - 3) 

277 current_mileage = 0 

278 last_service_mileage = 0 

279 

280 car = factory.create_thovex(today, 

281 last_service_date, 

282 current_mileage, 

283 last_service_mileage) 

284 self.assertFalse(car.needs_service()) 

285 

286 def test_engine_needs_service(self): 

287 """Test the Need for Thovex Car Servicing -> Consideration: Engine""" 

288 today = datetime.today().date() 

289 last_service_date = datetime.today().date() 

290 current_mileage = 30001 

291 last_service_mileage = 0 

292 

293 car = factory.create_thovex(today, 

294 last_service_date, 

295 current_mileage, 

296 last_service_mileage) 

297 self.assertTrue(car.needs_service()) 

298 

299 def test_engine_needs_no_service(self): 

300 """Test No Need for Thovex Car Servicing -> Consideration: Engine""" 

301 today = datetime.today().date() 

302 last_service_date = datetime.today().date() 

303 current_mileage = 30000 

304 last_service_mileage = 0 

305 

306 car = factory.create_thovex(today, last_service_date, 

307 current_mileage, 

308 last_service_mileage) 

309 self.assertFalse(car.needs_service()) 

310 

311 

312if __name__ == '__main__': 

313 unittest.main()