|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
5 | | -from backend.file_utils.runstrat_metrics import RunstratMetrics, compute_metrics |
| 5 | +from backend.file_utils.runstrat_metrics import ( |
| 6 | + RunstratMetrics, |
| 7 | + combine_metrics, |
| 8 | + compute_metrics, |
| 9 | + merge_csvs, |
| 10 | +) |
6 | 11 |
|
7 | 12 | FIELDNAMES = ["tests", "errors", "coverage", "total_time_sec"] |
8 | 13 |
|
@@ -30,6 +35,7 @@ def test_compute_metrics_single_row(tmp_path): |
30 | 35 | mean_coverage=0.75, |
31 | 36 | median_coverage=0.75, |
32 | 37 | total_time_sec=30.0, |
| 38 | + methods_with_results=1, |
33 | 39 | ) |
34 | 40 |
|
35 | 41 |
|
@@ -134,3 +140,113 @@ def test_compute_metrics_accepts_path_object(tmp_path): |
134 | 140 | result = compute_metrics(filepath) |
135 | 141 |
|
136 | 142 | assert result.total_tests == 1 |
| 143 | + |
| 144 | + |
| 145 | +def test_compute_metrics_counts_methods_with_results(tmp_path): |
| 146 | + filepath = write_metrics_csv( |
| 147 | + tmp_path, |
| 148 | + [ |
| 149 | + {"tests": 1, "errors": 0, "coverage": 0.1, "total_time_sec": 1.0}, |
| 150 | + {"tests": 2, "errors": 0, "coverage": 0.2, "total_time_sec": 1.0}, |
| 151 | + {"tests": 3, "errors": 0, "coverage": 0.3, "total_time_sec": 1.0}, |
| 152 | + ], |
| 153 | + ) |
| 154 | + |
| 155 | + result = compute_metrics(filepath) |
| 156 | + |
| 157 | + assert result.methods_with_results == 3 |
| 158 | + |
| 159 | + |
| 160 | +def test_combine_metrics_aggregates_across_files(tmp_path): |
| 161 | + csharp_dir = tmp_path / "csharp" |
| 162 | + csharp_dir.mkdir() |
| 163 | + java_dir = tmp_path / "java" |
| 164 | + java_dir.mkdir() |
| 165 | + |
| 166 | + file1 = write_metrics_csv( |
| 167 | + csharp_dir, |
| 168 | + [ |
| 169 | + {"tests": 5, "errors": 1, "coverage": 0.5, "total_time_sec": 10.0}, |
| 170 | + {"tests": 3, "errors": 0, "coverage": 0.7, "total_time_sec": 20.0}, |
| 171 | + ], |
| 172 | + ) |
| 173 | + file2 = write_metrics_csv( |
| 174 | + java_dir, |
| 175 | + [ |
| 176 | + {"tests": 4, "errors": 2, "coverage": 0.9, "total_time_sec": 15.0}, |
| 177 | + ], |
| 178 | + ) |
| 179 | + |
| 180 | + combined = combine_metrics([file1, file2]) |
| 181 | + |
| 182 | + assert combined.total_tests == 12 |
| 183 | + assert combined.total_errors == 3 |
| 184 | + assert combined.total_time_sec == pytest.approx(45.0) |
| 185 | + assert combined.methods_with_results == 3 |
| 186 | + assert combined.mean_coverage == pytest.approx(0.7) |
| 187 | + assert combined.median_coverage == pytest.approx(0.7) |
| 188 | + |
| 189 | + |
| 190 | +def test_combine_metrics_single_file_equals_compute_metrics(tmp_path): |
| 191 | + filepath = write_metrics_csv( |
| 192 | + tmp_path, |
| 193 | + [ |
| 194 | + {"tests": 5, "errors": 1, "coverage": 0.5, "total_time_sec": 10.0}, |
| 195 | + {"tests": 3, "errors": 0, "coverage": 0.9, "total_time_sec": 20.0}, |
| 196 | + ], |
| 197 | + ) |
| 198 | + |
| 199 | + assert combine_metrics([filepath]) == compute_metrics(filepath) |
| 200 | + |
| 201 | + |
| 202 | +def test_merge_csvs_preserves_header_and_rows(tmp_path): |
| 203 | + src1 = tmp_path / "src1" |
| 204 | + src1.mkdir() |
| 205 | + src2 = tmp_path / "src2" |
| 206 | + src2.mkdir() |
| 207 | + |
| 208 | + file1 = write_metrics_csv( |
| 209 | + src1, |
| 210 | + [{"tests": 1, "errors": 0, "coverage": 0.5, "total_time_sec": 1.0}], |
| 211 | + ) |
| 212 | + file2 = write_metrics_csv( |
| 213 | + src2, |
| 214 | + [ |
| 215 | + {"tests": 2, "errors": 1, "coverage": 0.6, "total_time_sec": 2.0}, |
| 216 | + {"tests": 3, "errors": 0, "coverage": 0.7, "total_time_sec": 3.0}, |
| 217 | + ], |
| 218 | + ) |
| 219 | + |
| 220 | + dest = tmp_path / "merged.csv" |
| 221 | + merge_csvs([file1, file2], dest) |
| 222 | + |
| 223 | + with open(dest, newline="") as f: |
| 224 | + reader = csv.DictReader(f) |
| 225 | + assert list(reader.fieldnames or []) == FIELDNAMES |
| 226 | + rows = list(reader) |
| 227 | + |
| 228 | + assert len(rows) == 3 |
| 229 | + assert rows[0]["tests"] == "1" |
| 230 | + assert rows[1]["tests"] == "2" |
| 231 | + assert rows[2]["tests"] == "3" |
| 232 | + |
| 233 | + |
| 234 | +def test_merge_csvs_then_compute_matches_combine(tmp_path): |
| 235 | + csharp_dir = tmp_path / "csharp" |
| 236 | + csharp_dir.mkdir() |
| 237 | + java_dir = tmp_path / "java" |
| 238 | + java_dir.mkdir() |
| 239 | + |
| 240 | + file1 = write_metrics_csv( |
| 241 | + csharp_dir, |
| 242 | + [{"tests": 5, "errors": 1, "coverage": 0.5, "total_time_sec": 10.0}], |
| 243 | + ) |
| 244 | + file2 = write_metrics_csv( |
| 245 | + java_dir, |
| 246 | + [{"tests": 4, "errors": 2, "coverage": 0.9, "total_time_sec": 15.0}], |
| 247 | + ) |
| 248 | + dest = tmp_path / "merged.csv" |
| 249 | + |
| 250 | + merge_csvs([file1, file2], dest) |
| 251 | + |
| 252 | + assert compute_metrics(dest) == combine_metrics([file1, file2]) |
0 commit comments