Skip to content

Add unit tests for CsvTableFormatter FINAL - #128

Merged
MichaelMcKibbin merged 4 commits into
mainfrom
CsvTableFormatterTest-2
Dec 3, 2025
Merged

Add unit tests for CsvTableFormatter FINAL#128
MichaelMcKibbin merged 4 commits into
mainfrom
CsvTableFormatterTest-2

Conversation

@SunflowerRays

Copy link
Copy Markdown
Collaborator

This test class includes various unit tests for the CsvTableFormatter.

SunflowerRays and others added 2 commits December 3, 2025 21:23
This test class includes various unit tests for the CsvTableFormatter.
- Updated tests to use new CsvTableFormatter API
- Tests now use formatTable(List<Row> rows, int limit) signature
- Tests use default constructor or custom newline constructor
- All 19 tests pass successfully
@edsonesf

edsonesf commented Dec 3, 2025

Copy link
Copy Markdown
Collaborator

I was reading #124 and #126, a few suggestions to match in the methods changed.

@edsonesf

edsonesf commented Dec 3, 2025

Copy link
Copy Markdown
Collaborator

@MichaelMcKibbin

Copy link
Copy Markdown
Owner

Right now the tests included in the PR don’t quite match the current project API (constructor signatures, method parameters, header handling, etc.), so they’re failing during CI. Because we’re so close to the submission deadline, it’s a bit risky to debug a full test suite from scratch.

To keep things simple and still get decent coverage, I’d suggest starting with a very small set of focused tests. These exercise the main behaviour of the formatter without relying on unsupported signatures or assumptions.

Try these. I think they will run ok and get us started.

/**
* Helper to read all rows from a CsvReader.
*/
private List readAll(CsvReader reader) throws Exception {
List rows = new ArrayList<>();
Row row;
while ((row = reader.readRow()) != null) {
rows.add(row);
}
return rows;
}

@Test
void formatTable_withHeadersAndRows_producesBorderedTable() throws Exception {
    String csv = """
            name,age,city
            Alice,30,Dublin
            Bob,25,Cork
            """;

    CsvConfig config = CsvConfig.builder()
            .setHasHeader(true)
            .build();

    String table;
    try (CsvReader reader = new CsvReader(new VirtualReader(csv), config)) {
        List<Row> rows = readAll(reader);

        // Use the reader-based constructor so formatter picks up the same newline
        CsvTableFormatter formatter = new CsvTableFormatter(reader);

        table = formatter.formatTable(rows);
    }

    assertNotNull(table);
    assertFalse(table.isEmpty(), "Table output should not be empty");

    // Basic structure checks
    assertTrue(table.startsWith("+"), "Table should start with a separator line");
    assertTrue(table.trim().endsWith("+"), "Table should end with a separator line");
    assertTrue(table.contains("|"), "Table should use '|' as column borders");

    // Headers present
    assertTrue(table.contains("name"), "Header 'name' should be present");
    assertTrue(table.contains("age"), "Header 'age' should be present");
    assertTrue(table.contains("city"), "Header 'city' should be present");

    // Data present
    assertTrue(table.contains("Alice"), "Data row should contain 'Alice'");
    assertTrue(table.contains("Bob"), "Data row should contain 'Bob'");
}

@Test
void formatTable_emptyList_returnsEmptyString() {
    CsvTableFormatter formatter = new CsvTableFormatter("\n");
    String table = formatter.formatTable(List.of());

    assertNotNull(table);
    assertEquals("", table, "Empty input should produce an empty string");
}

@Test
void formatRow_multilineCell_producesMultipleLinesWithBorders() throws Exception {
    String csv = """
        name,comment
        Alice,"Hello
        World"
        """;

    CsvConfig config = CsvConfig.builder()
            .setHasHeader(true)
            .build();

    String formatted;
    try (CsvReader reader = new CsvReader(new VirtualReader(csv), config)) {
        // Directly read all data rows (header is handled inside CsvReader)
        List<Row> rows = readAll(reader);
        assertEquals(1, rows.size(), "Expected one data row");

        CsvTableFormatter formatter = new CsvTableFormatter(reader);
        formatted = formatter.formatRow(rows.get(0));
    }

    assertNotNull(formatted);

    // It should contain both lines of the multi-line cell
    assertTrue(formatted.contains("Hello"), "First line of cell should appear");
    assertTrue(formatted.contains("World"), "Second line of cell should appear");

    // Split on line breaks and check borders
    String[] lines = formatted.split("\\R");
    assertTrue(lines.length >= 2, "Multiline cell should produce multiple lines");

    for (String line : lines) {
        assertTrue(line.startsWith("|"), "Each formatted line should start with '|'");
        assertTrue(line.endsWith("|"), "Each formatted line should end with '|'");
    }
}

=========================

@edsonesf

edsonesf commented Dec 3, 2025

Copy link
Copy Markdown
Collaborator

Ammended the testing a bit as per @MichaelMcKibbin suggestions above.

- Replace 18 tests with 3 focused tests covering core functionality
- Add readAll() helper method for cleaner row reading
- Use VirtualReader for in-memory CSV testing
- Tests: table formatting with headers, empty list handling, multiline cells
- All tests pass successfully
@edsonesf

edsonesf commented Dec 3, 2025

Copy link
Copy Markdown
Collaborator

Fixed CsvTableFormatterTest to match the actual CsvTableFormatter API and pass CI.
Replaced unmacthing tests with 3 simplified tests cover basic functionality:

  • table formatting with headers and rows,
  • empty list handling, and
  • multiline cell rendering.

Updated constructors to use CsvTableFormatter(CsvReader reader), instead of no-arg constructor, removed the unsupported int limit parameter from formatTable() calls, added a readAll() helper method for cleaner row reading, and used VirtualReader for in-memory CSV testing.

All tests now pass locally and in CI.

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.group5.csv.io.CsvTableFormatterTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.082 s -- in com.group5.csv.io.CsvTableFormatterTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- jacoco:0.8.12:report (report) @ csv-data-processor ---
[INFO] Loading execution data file /home/edson/Documents/ATU/SoftwareDevelopment/ATU-SoftDev-Grp5Project/target/jacoco.exec
[INFO] Analyzed bundle 'csv-data-processor' with 51 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.086 s
[INFO] Finished at: 2025-12-03T23:12:58Z
[INFO] ------------------------------------------------------------------------

@MichaelMcKibbin MichaelMcKibbin left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@MichaelMcKibbin
MichaelMcKibbin merged commit a306352 into main Dec 3, 2025
5 checks passed
@MichaelMcKibbin
MichaelMcKibbin deleted the CsvTableFormatterTest-2 branch December 3, 2025 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants