Skip to content

Commit

Permalink
Add tests for code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
yhanyi committed Nov 9, 2024
1 parent 623cf1d commit ba08cfc
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 14 deletions.
23 changes: 15 additions & 8 deletions src/test/java/seedu/address/logic/commands/ImportCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<Consultation> consultations = new ArrayList<>();

Expand Down

0 comments on commit ba08cfc

Please sign in to comment.