Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY1819S2#89 from wfxronald/docs
Browse files Browse the repository at this point in the history
Update the tests for remall and others. Closes nus-cs2103-AY1819S2#77
  • Loading branch information
sickerin authored Apr 12, 2019
2 parents c24e86e + 1b2782f commit 222c86f
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 4 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ test {
}

if (runNonGuiTests) {
test.include 'giatros/**'
test.include 'seedu/giatros/**'
}

if (runGuiTests) {
test.include 'systemtests/**'
test.include 'giatros/ui/**'
test.include 'seedu/giatros/ui/**'
}

if (!runGuiTests) {
test.exclude 'giatros/ui/**'
test.exclude 'seedu/giatros/ui/**'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public AddallCommand(Index index, Set<Allergy> allergies) {
this.allergies = allergies;
}

// Overloaded constructor if only one allergy is given
public AddallCommand(Index index, Allergy allergy) {
requireAllNonNull(index, allergy);

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/seedu/giatros/logic/commands/RemallCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ public RemallCommand(Index index, Set<Allergy> allergies) {
this.allergies = allergies;
}

// Overloaded constructor if only one allergy is given
public RemallCommand(Index index, Allergy allergy) {
requireAllNonNull(index, allergy);

Set<Allergy> allergies = new HashSet<>();
allergies.add(allergy);

this.index = index;
this.allergies = allergies;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
List<Patient> lastShownList = model.getFilteredPatientList();
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/seedu/giatros/logic/commands/AddallCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ public void execute_addAllergyUnfilteredList_success() {
assertCommandSuccess(addallCommand, model, commandHistory, expectedMessage, expectedModel);
}

@Test
public void execute_addDuplicateAllergyUnfilteredList_success() {
Patient firstPatient = model.getFilteredPatientList().get(INDEX_FIRST_PATIENT.getZeroBased());
Patient editedPatient = new PatientBuilder(firstPatient).build();

AddallCommand addallCommand = new AddallCommand(INDEX_FIRST_PATIENT, firstPatient.getAllergies());

String expectedMessage = String.format(AddallCommand.MESSAGE_ADD_ALLERGY_FAILURE, firstPatient);

Model expectedModel = new ModelManager(new GiatrosBook(model.getGiatrosBook()), new UserPrefs());
expectedModel.setPatient(firstPatient, editedPatient);
expectedModel.commitGiatrosBook();

assertCommandSuccess(addallCommand, model, commandHistory, expectedMessage, expectedModel);
}

@Test
public void execute_filteredList_success() {
showPatientAtIndex(model, INDEX_FIRST_PATIENT);
Expand Down
204 changes: 204 additions & 0 deletions src/test/java/seedu/giatros/logic/commands/RemallCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package seedu.giatros.logic.commands;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static seedu.giatros.logic.commands.CommandTestUtil.VALID_ALLERGY_AMY;
import static seedu.giatros.logic.commands.CommandTestUtil.VALID_ALLERGY_BOB;
import static seedu.giatros.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.giatros.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.giatros.logic.commands.CommandTestUtil.showPatientAtIndex;
import static seedu.giatros.testutil.TypicalIndexes.INDEX_FIRST_PATIENT;
import static seedu.giatros.testutil.TypicalIndexes.INDEX_SECOND_PATIENT;
import static seedu.giatros.testutil.TypicalPatients.getTypicalGiatrosBook;

import java.util.HashSet;

import org.junit.Test;

import seedu.giatros.commons.core.Messages;
import seedu.giatros.commons.core.index.Index;
import seedu.giatros.logic.CommandHistory;
import seedu.giatros.model.GiatrosBook;
import seedu.giatros.model.Model;
import seedu.giatros.model.ModelManager;
import seedu.giatros.model.UserPrefs;
import seedu.giatros.model.allergy.Allergy;
import seedu.giatros.model.patient.Patient;
import seedu.giatros.testutil.PatientBuilder;

public class RemallCommandTest {

private Model model = new ModelManager(getTypicalGiatrosBook(), new UserPrefs());
private CommandHistory commandHistory = new CommandHistory();

@Test
public void execute_removeAllergyUnfilteredList_success() {
Patient firstPatient = model.getFilteredPatientList().get(INDEX_FIRST_PATIENT.getZeroBased());
Patient editedPatient = new Patient(firstPatient.getName(), firstPatient.getPhone(), firstPatient.getEmail(),
firstPatient.getAddress(), new HashSet<>());

RemallCommand remallCommand = new RemallCommand(INDEX_FIRST_PATIENT, firstPatient.getAllergies());

String expectedMessage = String.format(RemallCommand.MESSAGE_REMOVE_ALLERGY_SUCCESS, editedPatient);

Model expectedModel = new ModelManager(new GiatrosBook(model.getGiatrosBook()), new UserPrefs());
expectedModel.setPatient(firstPatient, editedPatient);
expectedModel.commitGiatrosBook();

assertCommandSuccess(remallCommand, model, commandHistory, expectedMessage, expectedModel);
}

@Test
public void execute_removeNonExistentAllergyUnfilteredList_success() {
Patient firstPatient = model.getFilteredPatientList().get(INDEX_FIRST_PATIENT.getZeroBased());
Patient editedPatient = new PatientBuilder(firstPatient).build();

RemallCommand remallCommand = new RemallCommand(INDEX_FIRST_PATIENT, new Allergy("randomAllergy"));

String expectedMessage = String.format(RemallCommand.MESSAGE_REMOVE_ALLERGY_FAILURE, editedPatient);

Model expectedModel = new ModelManager(new GiatrosBook(model.getGiatrosBook()), new UserPrefs());
expectedModel.setPatient(firstPatient, editedPatient);
expectedModel.commitGiatrosBook();

assertCommandSuccess(remallCommand, model, commandHistory, expectedMessage, expectedModel);
}

@Test
public void execute_filteredList_success() {
showPatientAtIndex(model, INDEX_FIRST_PATIENT);

Patient firstPatient = model.getFilteredPatientList().get(INDEX_FIRST_PATIENT.getZeroBased());
Patient editedPatient = new Patient(firstPatient.getName(), firstPatient.getPhone(), firstPatient.getEmail(),
firstPatient.getAddress(), new HashSet<>());

RemallCommand remallCommand = new RemallCommand(INDEX_FIRST_PATIENT, firstPatient.getAllergies());

String expectedMessage = String.format(RemallCommand.MESSAGE_REMOVE_ALLERGY_SUCCESS, editedPatient);

Model expectedModel = new ModelManager(new GiatrosBook(model.getGiatrosBook()), new UserPrefs());
expectedModel.setPatient(firstPatient, editedPatient);
expectedModel.commitGiatrosBook();

assertCommandSuccess(remallCommand, model, commandHistory, expectedMessage, expectedModel);
}

@Test
public void execute_invalidPatientIndexUnfilteredList_failure() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPatientList().size() + 1);
RemallCommand remallCommand = new RemallCommand(outOfBoundIndex, new Allergy(VALID_ALLERGY_BOB));

assertCommandFailure(remallCommand, model, commandHistory, Messages.MESSAGE_INVALID_PATIENT_DISPLAYED_INDEX);
}

/**
* Edit filtered list where index is larger than size of filtered list,
* but smaller than size of giatros book
*/
@Test
public void execute_invalidPatientIndexFilteredList_failure() {
showPatientAtIndex(model, INDEX_FIRST_PATIENT);
Index outOfBoundIndex = INDEX_SECOND_PATIENT;
// ensures that outOfBoundIndex is still in bounds of giatros book list
assertTrue(outOfBoundIndex.getZeroBased() < model.getGiatrosBook().getPatientList().size());

RemallCommand remallCommand = new RemallCommand(outOfBoundIndex, new Allergy(VALID_ALLERGY_BOB));

assertCommandFailure(remallCommand, model, commandHistory, Messages.MESSAGE_INVALID_PATIENT_DISPLAYED_INDEX);
}

@Test
public void executeUndoRedo_validIndexUnfilteredList_success() throws Exception {
Patient patientToModify = model.getFilteredPatientList().get(INDEX_FIRST_PATIENT.getZeroBased());
Patient modifiedPatient = new Patient(patientToModify.getName(), patientToModify.getPhone(),
patientToModify.getEmail(), patientToModify.getAddress(), new HashSet<>());

RemallCommand remallCommand = new RemallCommand(INDEX_FIRST_PATIENT, patientToModify.getAllergies());

Model expectedModel = new ModelManager(model.getGiatrosBook(), new UserPrefs());
expectedModel.setPatient(patientToModify, modifiedPatient);
expectedModel.commitGiatrosBook();

// remall -> first patient allergy changed
remallCommand.execute(model, commandHistory);

// undo -> reverts giatrosbook back to previous state and filtered patient list to show all patients
expectedModel.undoGiatrosBook();
assertCommandSuccess(new UndoCommand(), model, commandHistory, UndoCommand.MESSAGE_SUCCESS, expectedModel);

// redo -> same first patient modified again
expectedModel.redoGiatrosBook();
assertCommandSuccess(new RedoCommand(), model, commandHistory, RedoCommand.MESSAGE_SUCCESS, expectedModel);
}

@Test
public void executeUndoRedo_invalidIndexUnfilteredList_failure() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPatientList().size() + 1);
RemallCommand remallCommand = new RemallCommand(outOfBoundIndex, new Allergy("randomAllergy"));

// execution failed -> giatros book state not added into model
assertCommandFailure(remallCommand, model, commandHistory, Messages.MESSAGE_INVALID_PATIENT_DISPLAYED_INDEX);

// single giatros book state in model -> undoCommand and redoCommand fail
assertCommandFailure(new UndoCommand(), model, commandHistory, UndoCommand.MESSAGE_FAILURE);
assertCommandFailure(new RedoCommand(), model, commandHistory, RedoCommand.MESSAGE_FAILURE);
}

/**
* 1. Modifies {@code Patient#allergy} from a filtered list.
* 2. Undo the modification.
* 3. The unfiltered list should be shown now. Verify that the index of the previously modified patient in the
* unfiltered list is different from the index at the filtered list.
* 4. Redo the modification. This ensures {@code RedoCommand} modifies the patient object regardless of indexing.
*/
@Test
public void executeUndoRedo_validIndexFilteredList_samePatientDeleted() throws Exception {
showPatientAtIndex(model, INDEX_SECOND_PATIENT);

Patient patientToModify = model.getFilteredPatientList().get(INDEX_FIRST_PATIENT.getZeroBased());
Patient modifiedPatient = new Patient(patientToModify.getName(), patientToModify.getPhone(),
patientToModify.getEmail(), patientToModify.getAddress(), new HashSet<>());

RemallCommand remallCommand = new RemallCommand(INDEX_FIRST_PATIENT, patientToModify.getAllergies());
Model expectedModel = new ModelManager(model.getGiatrosBook(), new UserPrefs());

expectedModel.setPatient(patientToModify, modifiedPatient);
expectedModel.commitGiatrosBook();

// remall -> modifies second patient in unfiltered patient list / first patient in filtered patient list
remallCommand.execute(model, commandHistory);

// undo -> reverts giatrosbook back to previous state and filtered patient list to show all patients
expectedModel.undoGiatrosBook();
assertCommandSuccess(new UndoCommand(), model, commandHistory, UndoCommand.MESSAGE_SUCCESS, expectedModel);

// redo -> modifies same second patient in unfiltered patient list
expectedModel.redoGiatrosBook();
assertCommandSuccess(new RedoCommand(), model, commandHistory, RedoCommand.MESSAGE_SUCCESS, expectedModel);
}

@Test
public void equals() {
final RemallCommand standardCommand = new RemallCommand(INDEX_FIRST_PATIENT, new Allergy(VALID_ALLERGY_AMY));

// same values -> returns true
RemallCommand commandWithSameValues = new RemallCommand(INDEX_FIRST_PATIENT, new Allergy(VALID_ALLERGY_AMY));
assertTrue(standardCommand.equals(commandWithSameValues));

// same object -> returns true
assertTrue(standardCommand.equals(standardCommand));

// null -> returns false
assertFalse(standardCommand.equals(null));

// different types -> returns false
assertFalse(standardCommand.equals(new ClearCommand()));

// different index -> returns false
assertFalse(standardCommand.equals(new RemallCommand(INDEX_SECOND_PATIENT, new Allergy(VALID_ALLERGY_AMY))));

// different descriptor -> returns false
assertFalse(standardCommand.equals(new RemallCommand(INDEX_FIRST_PATIENT, new Allergy(VALID_ALLERGY_BOB))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public void parse_invalidFormat_failure() {
String input = PREFIX_ALLERGY + " " + nonEmptyAllergy;
assertParseFailure(parser, input, expectedMessage);

// no allergy provided
input = INDEX_FIRST_PATIENT.getOneBased() + " ";
assertParseFailure(parser, input, String.format(AddallCommand.MESSAGE_INCORRECT_ALLERGY));

// no prefix provided
input = INDEX_FIRST_PATIENT.getOneBased() + " " + nonEmptyAllergy;
assertParseFailure(parser, input, expectedMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import seedu.giatros.logic.commands.HistoryCommand;
import seedu.giatros.logic.commands.ListCommand;
import seedu.giatros.logic.commands.RedoCommand;
import seedu.giatros.logic.commands.RemallCommand;
import seedu.giatros.logic.commands.SelectCommand;
import seedu.giatros.logic.commands.UndoCommand;
import seedu.giatros.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -124,7 +125,14 @@ public void parseCommand_addall() throws Exception {
AddallCommand command = (AddallCommand) parser.parseCommand(AddallCommand.COMMAND_WORD + " "
+ INDEX_FIRST_PATIENT.getOneBased() + " " + PREFIX_ALLERGY + allergy.allergyName);
assertEquals(new AddallCommand(INDEX_FIRST_PATIENT, allergy), command);
assertTrue(parser.parseCommand("addall 1 y/Allergy") instanceof AddallCommand);
}

@Test
public void parseCommand_remall() throws Exception {
final Allergy allergy = new Allergy("someAllergy");
RemallCommand command = (RemallCommand) parser.parseCommand(RemallCommand.COMMAND_WORD + " "
+ INDEX_FIRST_PATIENT.getOneBased() + " " + PREFIX_ALLERGY + allergy.allergyName);
assertEquals(new RemallCommand(INDEX_FIRST_PATIENT, allergy), command);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package seedu.giatros.logic.parser;

import static seedu.giatros.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.giatros.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.giatros.logic.parser.CliSyntax.PREFIX_ALLERGY;
import static seedu.giatros.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.giatros.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.giatros.testutil.TypicalIndexes.INDEX_FIRST_PATIENT;

import org.junit.Test;

import seedu.giatros.commons.core.index.Index;
import seedu.giatros.logic.commands.RemallCommand;
import seedu.giatros.model.allergy.Allergy;

public class RemallCommandParserTest {

private RemallCommandParser parser = new RemallCommandParser();
private final String nonEmptyAllergy = "someAllergy";

@Test
public void parse_allFieldsPresent_success() {
// adding non-empty allergy
Index index = INDEX_FIRST_PATIENT;
String input = index.getOneBased() + " " + PREFIX_ALLERGY + nonEmptyAllergy;
assertParseSuccess(parser, input, new RemallCommand(INDEX_FIRST_PATIENT, new Allergy(nonEmptyAllergy)));
}

@Test
public void parse_invalidFormat_failure() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemallCommand.MESSAGE_USAGE);

// no parameters provided
assertParseFailure(parser, "", expectedMessage);

// no index provided
String input = PREFIX_ALLERGY + " " + nonEmptyAllergy;
assertParseFailure(parser, input, expectedMessage);

// no allergy provided
input = INDEX_FIRST_PATIENT.getOneBased() + " ";
assertParseFailure(parser, input, String.format(RemallCommand.MESSAGE_INCORRECT_ALLERGY));

// no prefix provided
input = INDEX_FIRST_PATIENT.getOneBased() + " " + nonEmptyAllergy;
assertParseFailure(parser, input, expectedMessage);

// invalid prefix specified
input = INDEX_FIRST_PATIENT.getOneBased() + " " + PREFIX_ADDRESS + " " + nonEmptyAllergy;
assertParseFailure(parser, input, expectedMessage);

}

}

0 comments on commit 222c86f

Please sign in to comment.