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

130 statements  

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

1"""Tests for the CarFactory class.""" 

2import unittest 

3from unittest.mock import patch, MagicMock 

4from datetime import datetime 

5from car_factory import CarFactory 

6 

7# import engines 

8from engine.capulet_engine import CapuletEngine 

9from engine.willoughby_engine import WilloughbyEngine 

10from engine.sternman_engine import SternmanEngine 

11# import batteries 

12from battery.spindler import SpindlerBattery 

13from battery.nubbin import NubbinBattery 

14 

15 

16class TestCarFactory(unittest.TestCase): 

17 """Tests the CarFactory class. 

18 

19 Tests the creation of specialised instances of the Car class. 

20 """ 

21 @patch('car_factory.Car', autospec=True) 

22 @patch('car_factory.CapuletEngine') 

23 @patch('car_factory.SpindlerBattery') 

24 def test_create_calliope(self, mock_battery, mock_engine, mock_car): 

25 """Tests the creation of a Calliope car.""" 

26 # Arrange Mock calliope car 

27 factory = CarFactory() 

28 mock_car.return_value = mock_car 

29 current_date = datetime.today().date() 

30 last_service_date = current_date.replace(year=current_date.year - 3) 

31 current_mileage = 0 

32 last_service_mileage = 0 

33 

34 mock_engine.return_value = MagicMock() 

35 mock_battery.return_value = MagicMock() 

36 # create calliope car 

37 car = factory.create_calliope(current_date, 

38 last_service_date, 

39 current_mileage, 

40 last_service_mileage) 

41 

42 # Assert 

43 mock_car.assert_called_once_with(mock_engine.return_value, mock_battery.return_value) 

44 self.assertEqual(car, mock_car) 

45 

46 

47 def test_calliope_components(self): 

48 """Tests the creation of a Calliope engine and battery.""" 

49 # Arrange 

50 factory = CarFactory() 

51 current_date = datetime.today().date() 

52 last_service_date = current_date.replace(year=current_date.year - 3) 

53 current_mileage = 0 

54 last_service_mileage = 0 

55 

56 car = factory.create_calliope(current_date, 

57 last_service_date, 

58 current_mileage, 

59 last_service_mileage) 

60 

61 self.assertIsInstance(car.engine, CapuletEngine) 

62 self.assertIsInstance(car.battery, SpindlerBattery) 

63 

64 

65 

66 @patch('car_factory.Car', autospec=True) 

67 @patch('car_factory.WilloughbyEngine') 

68 @patch('car_factory.SpindlerBattery') 

69 def test_create_glissade(self, mock_battery, mock_engine, mock_car): 

70 """Tests the creation of a Glissade car.""" 

71 # Arrange Mock glisade car 

72 factory = CarFactory() 

73 mock_car.return_value = mock_car 

74 current_date = datetime.today().date() 

75 last_service_date = current_date.replace(year=current_date.year - 3) 

76 current_mileage = 0 

77 last_service_mileage = 0 

78 

79 mock_engine.return_value = MagicMock() 

80 mock_battery.return_value = MagicMock() 

81 

82 # create glissade car 

83 car = factory.create_glissade(current_date, 

84 last_service_date, 

85 current_mileage, 

86 last_service_mileage) 

87 # Assert 

88 mock_car.assert_called_once_with(mock_engine.return_value, mock_battery.return_value) 

89 self.assertEqual(car, mock_car) 

90 

91 def test_glissade_components(self): 

92 """Tests the creation of a glissade engine and battery.""" 

93 # Arrange 

94 factory = CarFactory() 

95 current_date = datetime.today().date() 

96 last_service_date = current_date.replace(year=current_date.year - 3) 

97 current_mileage = 0 

98 last_service_mileage = 0 

99 

100 car = factory.create_glissade(current_date, 

101 last_service_date, 

102 current_mileage, 

103 last_service_mileage) 

104 

105 self.assertIsInstance(car.engine, WilloughbyEngine) 

106 self.assertIsInstance(car.battery, SpindlerBattery) 

107 

108 @patch('car_factory.Car', autospec=True) 

109 @patch('car_factory.SternmanEngine') 

110 @patch('car_factory.SpindlerBattery') 

111 def test_create_palindrome(self, mock_battery, mock_engine, mock_car): 

112 """Tests the creation of a Palindrome car.""" 

113 # Arrange Mock sternman car 

114 factory = CarFactory() 

115 mock_car.return_value = mock_car 

116 current_date = datetime.today().date() 

117 last_service_date = current_date.replace(year=current_date.year - 3) 

118 warning_light_is_on = False 

119 

120 mock_engine.return_value = MagicMock() 

121 mock_battery.return_value = MagicMock() 

122 # create palindrome car 

123 car = factory.create_palindrome(current_date, 

124 last_service_date, 

125 warning_light_is_on) 

126 

127 # Assert 

128 mock_car.assert_called_once_with(mock_engine.return_value, mock_battery.return_value) 

129 self.assertEqual(car, mock_car) 

130 

131 def test_palindrome_components(self): 

132 """Tests the creation of  

133 engine and battery components of palindrome.""" 

134 # Arrange 

135 factory = CarFactory() 

136 current_date = datetime.today().date() 

137 last_service_date = current_date.replace(year=current_date.year - 3) 

138 warning_light_is_on = False 

139 

140 car = factory.create_palindrome(current_date, 

141 last_service_date, 

142 warning_light_is_on) 

143 

144 self.assertIsInstance(car.engine, SternmanEngine) 

145 self.assertIsInstance(car.battery, SpindlerBattery) 

146 

147 @patch('car_factory.Car', autospec=True) 

148 @patch('car_factory.WilloughbyEngine') 

149 @patch('car_factory.NubbinBattery') 

150 def test_create_rorschach(self, mock_battery, mock_engine, mock_car): 

151 """Tests the creation of a Rorschach car.""" 

152 # Arrange Mock rorschach car 

153 factory = CarFactory() 

154 mock_car.return_value = mock_car 

155 current_date = datetime.today().date() 

156 last_service_date = current_date.replace(year=current_date.year - 3) 

157 current_mileage = 0 

158 last_service_mileage = 0 

159 

160 mock_engine.return_value = MagicMock() 

161 mock_battery.return_value = MagicMock() 

162 

163 # create rorschach car 

164 car = factory.create_rorschach(current_date, 

165 last_service_date, 

166 current_mileage, 

167 last_service_mileage) 

168 

169 # Assert 

170 mock_car.assert_called_once_with(mock_engine.return_value, mock_battery.return_value) 

171 self.assertEqual(car, mock_car) 

172 

173 def test_rorschach_components(self): 

174 """Tests the creation of the appropriate engine and battery for rorschach.""" 

175 # Arrange 

176 factory = CarFactory() 

177 current_date = datetime.today().date() 

178 last_service_date = current_date.replace(year=current_date.year - 3) 

179 current_mileage = 0 

180 last_service_mileage = 0 

181 

182 car = factory.create_rorschach(current_date, 

183 last_service_date, 

184 current_mileage, 

185 last_service_mileage) 

186 

187 self.assertIsInstance(car.engine, WilloughbyEngine) 

188 self.assertIsInstance(car.battery, NubbinBattery) 

189 

190 @patch('car_factory.Car', autospec=True) 

191 @patch('car_factory.CapuletEngine') 

192 @patch('car_factory.NubbinBattery') 

193 def test_create_thovex(self, mock_battery, mock_engine, mock_car): 

194 """Tests the creation of a Thovex car.""" 

195 # Arrange Mock thovex car 

196 factory = CarFactory() 

197 mock_car.return_value = mock_car 

198 current_date = datetime.today().date() 

199 last_service_date = current_date.replace(year=current_date.year - 3) 

200 current_mileage = 0 

201 last_service_mileage = 0 

202 mock_engine.return_value = MagicMock() 

203 mock_battery.return_value = MagicMock() 

204 

205 # create thovex car 

206 car = factory.create_thovex(current_date, 

207 last_service_date, 

208 current_mileage, 

209 last_service_mileage) 

210 

211 # Assert 

212 mock_car.assert_called_once_with(mock_engine.return_value, mock_battery.return_value) 

213 self.assertEqual(car, mock_car) 

214 

215 # test thovex components 

216 def test_thovex_components(self): 

217 """Tests the creation of a thovex engine and battery.""" 

218 # Arrange 

219 factory = CarFactory() 

220 current_date = datetime.today().date() 

221 last_service_date = current_date.replace(year=current_date.year - 3) 

222 current_mileage = 0 

223 last_service_mileage = 0 

224 

225 car = factory.create_thovex(current_date, 

226 last_service_date, 

227 current_mileage, 

228 last_service_mileage) 

229 

230 self.assertIsInstance(car.engine, CapuletEngine) 

231 self.assertIsInstance(car.battery, NubbinBattery) 

232 

233 

234 

235if __name__ == '__main__': 

236 unittest.main()