Coverage for tests\test_report.py: 100%

116 statements  

« prev     ^ index     » next       coverage.py v7.0.5, created at 2023-01-18 14:51 +0200

1"""Tests for CLI. 

2 

3Tests for command-line interface in report module, 

4""" 

5from unittest.mock import patch 

6 

7import pytest 

8from tabulate import tabulate 

9 

10from report.report import Cli, OrderedNamespace 

11import report.constant as c 

12 

13 

14class TestCli: 

15 

16 def test_cli_init(self): 

17 cli = Cli() 

18 assert len(cli._print_func) == 8 

19 

20 @pytest.fixture(scope="session") 

21 def report_files_empty(self, tmp_path_factory): 

22 # Create directory with necessary files. 

23 path = tmp_path_factory.mktemp("sub") 

24 abbr = path / "abbreviations.txt" 

25 abbr.write_text("") 

26 start = path / "start.log" 

27 start.write_text("") 

28 end = path / "end.log" 

29 end.write_text("") 

30 # Create empty directory. 

31 empty_dir = tmp_path_factory.mktemp("empty") 

32 

33 return [path, abbr, start, end, empty_dir] 

34 

35 @pytest.fixture(scope="session") 

36 def setup(self): 

37 cli = Cli() 

38 return cli 

39 

40 def test_get_path_to_file(self, report_files_empty, setup): 

41 result = setup.get_path_to_files(str(report_files_empty[0])) 

42 assert result == {"abbreviations": str(report_files_empty[1]), 

43 "start": str(report_files_empty[2]), 

44 "end": str(report_files_empty[3])} 

45 

46 def test_get_path_to_file_with_error1(self, report_files_empty, setup): 

47 with pytest.raises(NotADirectoryError): 

48 assert setup.get_path_to_files(str(report_files_empty[1])) 

49 

50 def test_get_path_to_file_with_error2(self, report_files_empty, setup): 

51 with pytest.raises(UserWarning): 

52 assert setup.get_path_to_files(str(report_files_empty[4])) 

53 

54 def test_get_path_to_file_with_error3(self, setup): 

55 with pytest.raises(FileNotFoundError): 

56 assert setup.get_path_to_files("no/such/directory") 

57 

58 def test_output_with_file_argument(self, setup, capsys): 

59 """Call with no argument 

60 

61 Args: 

62 capsys: 

63 pytest fixture. 

64 """ 

65 try: 

66 with patch("sys.argv", ["report.py", "-f", "./data"]): 

67 setup.cli() 

68 except SystemExit: 

69 output = capsys.readouterr().out.rstrip() 

70 assert "Report of Monaco 2018 racing." in output 

71 

72 @pytest.fixture(scope="session") 

73 def report_files(self, tmp_path_factory): 

74 path = tmp_path_factory.mktemp("sub") 

75 abbr = path / "abbreviations.txt" 

76 abbr.write_text("""DRR_Daniel Ricciardo_RED BULL RACING TAG HEUER""") 

77 start = path / "start.log" 

78 start.write_text("""DRR2018-05-24_12:11:24.067""") 

79 end = path / "end.log" 

80 end.write_text("""DRR2018-05-24_12:14:12.054""") 

81 return {"abbreviations": abbr, "start": start, "end": end} 

82 

83 @pytest.fixture(scope="session") 

84 def setup_with_race(self, setup, report_files): 

85 setup.create_race(report_files) 

86 return setup 

87 

88 @patch("report.report.Race.get_date_of_race", return_value="2019") 

89 def test_output_date(self, mock_obj, setup_with_race, capsys): 

90 """Call with no argument 

91 

92 Args: 

93 capsys: 

94 pytest fixture. 

95 """ 

96 setup_with_race.print_date_of_the_race() 

97 output = capsys.readouterr().out.rstrip() 

98 assert "\nDate of the race:\n\t2019" == output 

99 

100 @patch("report.report.Race.get_start_time_of_q1", return_value="12:00:00") 

101 def test_output_time(self, mock_obj, setup_with_race, capsys): 

102 """Call with no argument 

103 

104 Args: 

105 capsys: 

106 pytest fixture. 

107 """ 

108 setup_with_race.print_time() 

109 output = capsys.readouterr().out.rstrip() 

110 assert "\n1st qualification round started at:\n\t12:00:00" == output 

111 

112 @patch("report.report.Race.get_number_of_racers", return_value="19") 

113 def test_output_number_of_racers(self, mock_obj, setup_with_race, capsys): 

114 """Call with no argument 

115 

116 Args: 

117 capsys: 

118 pytest fixture. 

119 """ 

120 setup_with_race.print_number_of_racers() 

121 output = capsys.readouterr().out.rstrip() 

122 assert "\nNumber of the racers in the race:\n\t19" == output 

123 

124 @patch("report.report.Race.get_report", 

125 return_value=[["Sergio Perez", "FORCE INDIA MERCEDES", "00:01:12:333000"], 

126 ["Daniel Ricciardo", "RED BULL RACING TAG HEUER", "00:01:12:333000"], 

127 ]) 

128 def test_output_report(self, mock_obj, setup_with_race, capsys): 

129 """Call with no argument 

130 

131 Args: 

132 capsys: 

133 pytest fixture. 

134 """ 

135 setup_with_race.print_report() 

136 output = capsys.readouterr().out.rstrip() 

137 assert tabulate([["1", "Sergio Perez", 

138 "FORCE INDIA MERCEDES", 

139 "01:12:333"], 

140 ["2", "Daniel Ricciardo", 

141 "RED BULL RACING TAG HEUER", 

142 "01:12:333"], 

143 ], 

144 headers=["", "Full Name", "Team", "Q1 Time"], 

145 tablefmt=c.TABLE_FORMAT) in output 

146 

147 @patch("report.report.Race.get_racer", 

148 return_value=["SPF", "Sergio Perez", "FORCE INDIA MERCEDES", "01:12:333000"]) 

149 def test_output_racer(self, mock_obj, setup_with_race, capsys): 

150 """Call with no argument 

151 

152 Args: 

153 capsys: 

154 pytest fixture. 

155 """ 

156 setup_with_race.print_driver("Sergio Perez") 

157 output = capsys.readouterr().out.rstrip() 

158 assert tabulate([["SPF", "Sergio Perez", 

159 "FORCE INDIA MERCEDES", 

160 "01:12:333000"]], 

161 headers=["Abbr", "Full Name", "Team", "Q1 Time"], 

162 tablefmt=c.TABLE_FORMAT) in output 

163 

164 @patch("report.report.Race.get_names_of_racers") 

165 @pytest.mark.parametrize("order, result, mock_result", [("asc", 

166 [["Sergio Perez"], ["Esteban Ocon"]], 

167 ["Sergio Perez", "Esteban Ocon"]), 

168 ("desc", 

169 [["Esteban Ocon"], ["Sergio Perez"]], 

170 ["Esteban Ocon", "Sergio Perez"]) 

171 ]) 

172 def test_output_racers(self, mock_obj, setup_with_race, capsys, order, result, mock_result): 

173 """Call with no argument 

174 

175 Args: 

176 capsys: 

177 pytest fixture. 

178 """ 

179 mock_obj.return_value = mock_result 

180 setup_with_race.print_racers(order) 

181 output = capsys.readouterr().out.rstrip() 

182 assert tabulate(result, 

183 headers=["List of racers:"], 

184 tablefmt=c.TABLE_FORMAT) in output 

185 

186 @patch("report.report.Race.get_teams") 

187 @pytest.mark.parametrize("order, result, mock_result", [("asc", 

188 [["FERRARI"], ["RENAULT"]], 

189 ["FERRARI", "RENAULT"]), 

190 ("desc", 

191 [["RENAULT"], ["FERRARI"]], 

192 ["RENAULT", "FERRARI"]) 

193 ]) 

194 def test_output_teams(self, mock_obj, setup_with_race, capsys, order, result, mock_result): 

195 """Call with no argument 

196 

197 Args: 

198 capsys: 

199 pytest fixture. 

200 """ 

201 mock_obj.return_value = mock_result 

202 setup_with_race.print_teams(order) 

203 output = capsys.readouterr().out.rstrip() 

204 assert tabulate(result, 

205 headers=["List of teams:"], 

206 tablefmt=c.TABLE_FORMAT) in output 

207 

208 @patch("report.report.Race.get_racers_in_team") 

209 def test_output_racers_in_teams(self, mock_obj, report_files, setup_with_race, capsys): 

210 """Call with no argument 

211 

212 Args: 

213 capsys: 

214 pytest fixture. 

215 """ 

216 mock_obj.return_value = [["SPF", "Sergio Perez", "FORCE INDIA MERCEDES", "01:12:333000"], 

217 ["EOF", "Esteban Ocon", "FORCE INDIA MERCEDES", "01:12:555000"]] 

218 setup_with_race.print_racers_in_team("FORCE INDIA MERCEDES") 

219 output = capsys.readouterr().out.rstrip() 

220 assert tabulate([["SPF", "Sergio Perez", "FORCE INDIA MERCEDES", "01:12:333000"], 

221 ["EOF", "Esteban Ocon", "FORCE INDIA MERCEDES", "01:12:555000"]], 

222 headers=["Abbr", "Full Name", "Team", "Q1 Time"], 

223 tablefmt=c.TABLE_FORMAT) in output 

224 

225 @pytest.fixture(scope="session") 

226 def dict_path(self, tmp_path_factory): 

227 path = tmp_path_factory.mktemp("sub") 

228 abbr = path / "abbreviations.txt" 

229 abbr.write_text("""DRR_Daniel Ricciardo_RED BULL RACING TAG HEUER""") 

230 start = path / "start.log" 

231 start.write_text("""DRR2018-05-24_12:11:24.067""") 

232 end = path / "end.log" 

233 end.write_text("""DRR2018-05-24_12:14:12.054""") 

234 return path 

235 

236 def test_args_from_cli(self, dict_path, setup, capsys): 

237 """Call with no argument 

238 

239 Args: 

240 capsys: 

241 pytest fixture. 

242 """ 

243 with patch("sys.argv", ["report.py", "--files", f"{str(dict_path)}", "--report"]): 

244 setup.cli() 

245 assert len(setup.args.ordered()) == 1 

246