From ba08cfcc821901e3ecf1d7f19a4a15a32893fe78 Mon Sep 17 00:00:00 2001 From: yhanyi Date: Sat, 9 Nov 2024 17:49:05 +0800 Subject: [PATCH] Add tests for code coverage --- .../logic/commands/ImportCommandTest.java | 23 +++-- .../ImportConsultCommandTest.java | 86 +++++++++++++++++-- 2 files changed, 95 insertions(+), 14 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java index 28c3cdae82b..2be3625fb9a 100644 --- a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java @@ -309,16 +309,23 @@ protected Path resolveFilePath(String filepath) { @Test public void execute_absolutePath() throws IOException, CommandException { - // Create file using system-independent path construction - Path absolutePath = testDir.toAbsolutePath().resolve("absolute.csv"); - try (FileWriter writer = new FileWriter(absolutePath.toFile())) { - writer.write("Name,Phone,Email,Courses\n"); - writer.write("John Doe,12345678,john@example.com,CS2103T\n"); - } + // Create file using platform-independent path handling + Path absolutePath = testDir.toAbsolutePath().normalize().resolve("absolute.csv"); + Files.createDirectories(absolutePath.getParent()); + + Files.writeString(absolutePath, + "Name,Phone,Email,Courses\n" + + "John Doe,12345678,john@example.com,CS2103T\n" + ); filesToCleanup.add(absolutePath); - ImportCommand importCommand = new ImportCommand(absolutePath.normalize().toString()); - CommandResult result = importCommand.execute(model); + ImportCommand testCommand = new ImportCommand(absolutePath.toString()) { + @Override + protected Path resolveFilePath(String filepath) { + return Paths.get(filepath).normalize(); + } + }; + CommandResult result = testCommand.execute(model); assertEquals(String.format(ImportCommand.MESSAGE_SUCCESS, 1, 0), result.getFeedbackToUser()); } diff --git a/src/test/java/seedu/address/logic/commands/consultation/ImportConsultCommandTest.java b/src/test/java/seedu/address/logic/commands/consultation/ImportConsultCommandTest.java index f0840490f63..51aa34a44ce 100644 --- a/src/test/java/seedu/address/logic/commands/consultation/ImportConsultCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/consultation/ImportConsultCommandTest.java @@ -3,6 +3,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.logic.commands.consultation.ImportConsultCommand.MESSAGE_EMPTY_FILE; +import static seedu.address.logic.commands.consultation.ImportConsultCommand.MESSAGE_INVALID_FILE; import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalStudents.ALICE; @@ -22,6 +24,7 @@ import org.junit.jupiter.api.io.TempDir; import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.CommandType; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.consultation.Consultation; import seedu.address.model.student.Name; @@ -109,6 +112,11 @@ public void equals() { assertFalse(command1.equals(command2)); } + @Test + public void getCommandType() { + assertEquals(CommandType.CONSULT, importCommand.getCommandType()); + } + @Test public void execute_emptyFile_throwsCommandException() throws IOException { // Create a custom ImportConsultCommand that uses testDir instead of data dir @@ -121,7 +129,7 @@ protected Path resolveFilePath(String filepath) { createCsvFile(""); filesToCleanup.add(testCsvPath); assertThrows(CommandException.class, - ImportConsultCommand.MESSAGE_EMPTY_FILE, () -> testCommand.execute(modelStub)); + MESSAGE_EMPTY_FILE, () -> testCommand.execute(modelStub)); } @Test @@ -233,13 +241,70 @@ public void resolveFilePath_homeDirectory() { assertEquals(expected.normalize(), actual.normalize()); } - private void createCsvFile(String content) throws IOException { - // Ensure parent directories exist - Files.createDirectories(testCsvPath.getParent()); + @Test + public void execute_onlyEmptyLines_throwsCommandException() throws IOException { + ImportConsultCommand testCommand = new ImportConsultCommand(testCsvPath.toString()) { + @Override + protected Path resolveFilePath(String filepath) { + return testCsvPath; + } + }; + createCsvFile(VALID_HEADER + "\n\n \n\t\n"); + filesToCleanup.add(testCsvPath); + assertThrows(CommandException.class, + MESSAGE_EMPTY_FILE, () -> testCommand.execute(modelStub)); + } - // Create and write file - Files.writeString(testCsvPath, content); + @Test + public void execute_incompleteLine_recordsError() throws Exception { + ImportConsultCommand testCommand = new ImportConsultCommand(testCsvPath.toString()) { + @Override + protected Path resolveFilePath(String filepath) { + return testCsvPath; + } + }; + createCsvFile(VALID_HEADER + "\n2024-10-20,14:00"); filesToCleanup.add(testCsvPath); + + CommandResult result = testCommand.execute(modelStub); + assertTrue(result.getFeedbackToUser().contains("error.csv")); + assertEquals(0, modelStub.consultations.size()); + } + + @Test + public void testEscapeSpecialCharacters() { + // Test null input + assertEquals("", importCommand.escapeSpecialCharacters(null)); + + // Test normal string + String normal = "Regular text"; + assertEquals(normal, importCommand.escapeSpecialCharacters(normal)); + + // Test string with quotes and commas + String complex = "Text with \"quotes\" and ,commas,"; + String escaped = importCommand.escapeSpecialCharacters(complex); + assertTrue(escaped.startsWith("\"")); + assertTrue(escaped.endsWith("\"")); + assertTrue(escaped.contains("\"\"quotes\"\"")); + } + + @Test + public void testUnescapeSpecialCharacters() { + // Test null input + assertEquals("", importCommand.unescapeSpecialCharacters(null)); + + // Test normal string + String normal = "Regular text"; + assertEquals(normal, importCommand.unescapeSpecialCharacters(normal)); + + // Test quoted string + String quoted = "\"Text with \"\"quotes\"\" and ,commas,\""; + String unescaped = importCommand.unescapeSpecialCharacters(quoted); + assertEquals("Text with \"quotes\" and ,commas,", unescaped); + + // Test string without quotes + String noQuotes = "Text without quotes"; + assertEquals(noQuotes, importCommand.unescapeSpecialCharacters(noQuotes)); } @Test @@ -260,6 +325,15 @@ public void execute_dataDirectoryPath_success() throws IOException, CommandExcep assertEquals(String.format(ImportConsultCommand.MESSAGE_SUCCESS, 1, 0), result.getFeedbackToUser()); } + private void createCsvFile(String content) throws IOException { + // Ensure parent directories exist + Files.createDirectories(testCsvPath.getParent()); + + // Create and write file + Files.writeString(testCsvPath, content); + filesToCleanup.add(testCsvPath); + } + private class ModelStubWithConsultations extends ModelStub { private final List consultations = new ArrayList<>();