Skip to content

Commit

Permalink
add import command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ravern committed Nov 11, 2023
1 parent 6778eec commit 718b81d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/test/data/ImportCommandTest/invalid.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name,phone,email,gpa,tags,studentNo,previousGrade
Jasmine David,9847283,jasmine_david@u.nus.edu,4.3,deansList;pastTA,A0123486A,D-
Sandeep Kopparthi,8753746,sandeep@u.nus.edu,5.0,pastTA,A0456123A,C-
Lim Boon Kong,9777777,boonkong@u.nus.edu,3.5.2,deansList,A0775848D,C
Mohammed Taufiq bin Rozaini,85535252,taufiqu.nus.edu,4.2,,A0483910A,A+
Empty file.
5 changes: 5 additions & 0 deletions src/test/data/ImportCommandTest/missing_fields.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
studentNo,name,phone,email,gpaa,previousGrade,tags
A0123486A,Jasmine David,98472983,jasmine_david@u.nus.edu,4.3,B+,deansList;pastTA
A0456123A,Sandeep Kopparthi,86753746,sandeep@u.nus.edu,5.0,B+,pastTA
A0775848D,Lim Boon Kong,97777777,boonkong@u.nus.edu,3.5,C,deansList
A0483910A,Mohammed Taufiq bin Rozaini,85535252,taufiq@u.nus.edu,4.2,A+,
5 changes: 5 additions & 0 deletions src/test/data/ImportCommandTest/partial_valid.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name,phone,email,gpa,tags,studentNo,previousGrade
Jasmine David,9847283,jasmine_david@u.nus.edu,4.3,deansList;pastTA,A0123486A,D-
Sandeep Kopparthi,8753746,sandeep@u.nus.edu,5.0,pastTA,A0456123A,C-
Lim Boon Kong,97777777,boonkong@u.nus.edu,3.5,deansList,A0775848D,C
Mohammed Taufiq bin Rozaini,85535252,taufiq@u.nus.edu,4.2,,A0483910A,A+
5 changes: 5 additions & 0 deletions src/test/data/ImportCommandTest/valid.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
studentNo,name,phone,email,gpa,previousGrade,tags
A0123486A,Jasmine David,98472983,jasmine_david@u.nus.edu,4.3,B+,deansList;pastTA
A0456123A,Sandeep Kopparthi,86753746,sandeep@u.nus.edu,5.0,B+,pastTA
A0775848D,Lim Boon Kong,97777777,boonkong@u.nus.edu,3.5,C,deansList
A0483910A,Mohammed Taufiq bin Rozaini,85535252,taufiq@u.nus.edu,4.2,A+,
110 changes: 110 additions & 0 deletions src/test/java/seedu/address/logic/commands/ImportCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package seedu.address.logic.commands;

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.CommandTestUtil.assertCommandFailure;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.attachment.Attachment;

public class ImportCommandTest {

private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "ImportCommandTest");
private static final Path VALID_CSV_FILE = TEST_DATA_FOLDER.resolve("valid.csv");
private static final Path PARTIAL_VALID_CSV_FILE = TEST_DATA_FOLDER.resolve("partial_valid.csv");
private static final Path INVALID_CSV_FILE = TEST_DATA_FOLDER.resolve("invalid.csv");
private static final Path MISSING_FIELDS_CSV_FILE = TEST_DATA_FOLDER.resolve("missing_fields.csv");
private static final Path INVALID_FORMAT_CSV_FILE = TEST_DATA_FOLDER.resolve("invalid_format.csv");

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_validCsv_importSuccessful() throws CommandException {
Attachment validCsv = new Attachment(VALID_CSV_FILE.toString());
CommandResult commandResult = new ImportCommand(validCsv).execute(model);

String expectedMessage = String.format(ImportCommand.MESSAGE_IMPORT_SUCCESS, 4);

assertEquals(expectedMessage, commandResult.getFeedbackToUser());
}

@Test
public void execute_partialValidCsv_importPartiallySuccessful() throws CommandException {
Attachment partialValidCsv = new Attachment(PARTIAL_VALID_CSV_FILE.toString());
CommandResult commandResult = new ImportCommand(partialValidCsv).execute(model);

String expectedMessage = String.format(ImportCommand.MESSAGE_IMPORT_SUCCESS, 2) + " "
+ String.format(ImportCommand.MESSAGE_IMPORT_FAILURE, 2, "2, 3");

assertEquals(expectedMessage, commandResult.getFeedbackToUser());
}

@Test
public void execute_invalidCsv_importFailure() throws CommandException {
Attachment invalidCsv = new Attachment(INVALID_CSV_FILE.toString());
CommandResult commandResult = new ImportCommand(invalidCsv).execute(model);

String expectedMessage = String.format(ImportCommand.MESSAGE_IMPORT_FAILURE, 4, "2, 3, 4, 5");

assertEquals(expectedMessage, commandResult.getFeedbackToUser());
}

@Test
public void execute_invalidFileFormatCsv_importFailure() throws CommandException {
Attachment invalidFileFormatCsv = new Attachment(INVALID_FORMAT_CSV_FILE.toString());
ImportCommand importCommand = new ImportCommand(invalidFileFormatCsv);

String expectedMessage = ImportCommand.MESSAGE_INVALID_FILE_FORMAT;

assertCommandFailure(importCommand, model, expectedMessage);
}

@Test
public void execute_missingFieldsCsv_importFailure() throws CommandException {
Attachment missingFieldsCsv = new Attachment(MISSING_FIELDS_CSV_FILE.toString());
ImportCommand importCommand = new ImportCommand(missingFieldsCsv);

String expectedMessage = String.format(ImportCommand.MESSAGE_MISSING_FIELDS, "gpa");

assertCommandFailure(importCommand, model, expectedMessage);
}

@Test
public void equals() {
Attachment firstAttachment = new Attachment(VALID_CSV_FILE.toString());
Attachment secondAttachment = new Attachment(INVALID_CSV_FILE.toString());

ImportCommand importFirstCommand = new ImportCommand(firstAttachment);
ImportCommand importSecondCommand = new ImportCommand(secondAttachment);

assertTrue(importFirstCommand.equals(importFirstCommand));

ImportCommand importFirstCommandCopy = new ImportCommand(firstAttachment);
assertTrue(importFirstCommand.equals(importFirstCommandCopy));

assertFalse(importFirstCommand.equals(1));

assertFalse(importFirstCommand.equals(null));

assertFalse(importFirstCommand.equals(importSecondCommand));
}

@Test
public void toStringMethod() {
Attachment attachment = new Attachment(VALID_CSV_FILE.toString());
ImportCommand importCommand = new ImportCommand(attachment);
String expected = ImportCommand.class.getCanonicalName() + "{attachment=" + attachment + "}";
assertEquals(expected, importCommand.toString());
}

}

0 comments on commit 718b81d

Please sign in to comment.