Skip to content

Commit

Permalink
add better messages to the CSVStorageTest
Browse files Browse the repository at this point in the history
  • Loading branch information
nck-mlcnv committed Aug 4, 2023
1 parent 793dcbb commit dac7816
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -128,7 +129,7 @@ public static Arguments createTestData1() {

Path testFileFolder = Path.of("src/test/resources/storage/csv_test_files/");

return Arguments.of(new Suite(List.of(m), metrics, testFileFolder));
return Arguments.of(Named.of(String.format("One simple tasks with one faulty entry. | ExpectedFolder: %s | Metrics: %s", testFileFolder, metrics.stream().map(Metric::getAbbreviation).toList()), new Suite(List.of(m), metrics, testFileFolder)));
}

@AfterEach
Expand All @@ -144,14 +145,15 @@ public static Stream<Arguments> data() {

@ParameterizedTest
@MethodSource("data")
@DisplayName("Test CSVStorage")
public void testCSVStorage(Suite suite) throws IOException {
for (Model m : suite.taskResults) {
// Metrics need to be set here, because the CSVStorage uses the manager to store the results
MetricManager.setMetrics(suite.metrics);

// Test Initialisation
assertDoesNotThrow(() -> storage = new CSVStorage(this.folder.toAbsolutePath().toString()));
assertTrue(Files.exists(this.suiteFolder));
assertDoesNotThrow(() -> storage = new CSVStorage(this.folder.toAbsolutePath().toString()), "Initialisation failed");
assertTrue(Files.exists(this.suiteFolder), String.format("Result folder (%s) doesn't exist", this.suiteFolder));
storage.storeResult(m);

List<Path> expectedFiles;
Expand All @@ -161,7 +163,7 @@ public void testCSVStorage(Suite suite) throws IOException {

for (Path expectedFile : expectedFiles) {
Path actualFile = suiteFolder.resolve(expectedFile.getFileName());
assertTrue(Files.exists(actualFile));
assertTrue(Files.exists(actualFile), String.format("File (%s) doesn't exist", actualFile));
assertDoesNotThrow(() -> compareCSVFiles(expectedFile, actualFile));
}
}
Expand All @@ -172,9 +174,9 @@ private void compareCSVFiles(Path expected, Path actual) throws IOException, Csv
CSVReader readerActual = new CSVReader(new FileReader(actual.toFile()))) {
String[] headerExpected = readerExpected.readNext();
String[] headerActual = readerActual.readNext();
assertEquals(headerExpected.length, headerActual.length);
assertEquals(headerExpected.length, headerActual.length, String.format("Headers don't match. Actual: %s, Expected: %s", Arrays.toString(headerActual), Arrays.toString(headerExpected)));
for (int i = 0; i < headerExpected.length; i++) {
assertEquals(headerExpected[i], headerActual[i]);
assertEquals(headerExpected[i], headerActual[i], String.format("Headers don't match. Actual: %s, Expected: %s", Arrays.toString(headerActual), Arrays.toString(headerExpected)));
}

List<String[]> expectedValues = new ArrayList<>(readerExpected.readAll());
Expand All @@ -188,11 +190,10 @@ private void compareCSVFiles(Path expected, Path actual) throws IOException, Csv
return true;
}).toList();

assertFalse(sameLines.isEmpty());
assertFalse(sameLines.isEmpty(), String.format("Line (%s) not found in actual file", Arrays.toString(expectedLine)));
actualValues.remove(sameLines.get(0));
}

assertTrue(actualValues.isEmpty());
assertTrue(actualValues.isEmpty(), String.format("Actual file contains more lines than expected. Lines: %s", actualValues.stream().map(x -> "[" + String.join(", ", x) + "]").collect(Collectors.joining("\n"))));
}
}

Expand Down

0 comments on commit dac7816

Please sign in to comment.