Skip to content

Commit

Permalink
Implement test cases for MedicalHistory suite of classes (#269)
Browse files Browse the repository at this point in the history
* Implement test cases for MedicalHistory class

* Add more test cases for new methods in Patient class

* Add AddMedicalEntryCommand test cases

* Add DeleteMedicalEntryCommand test cases

* Modify test case

* Add more test cases

* Add AddMedicalHistoryParserTest test cases

* Add testcase for AddMedicalEntryCommandParserTest

* Add success case for DeleteMedicalEntryCommandParserTest

* Add one failure test case for DeleteMedicalEntryCommandParserTest

* Add failure test case to DeleteMedicalEntryCommandTest

* Fix checkstyle

* Remove comment
  • Loading branch information
didymental authored Nov 6, 2021
1 parent dfe7068 commit 7633504
Show file tree
Hide file tree
Showing 16 changed files with 480 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public CommandResult execute(Model model) throws CommandException {

return new CommandResult(MESSAGE_SUCCESS + editedPatient);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AddMedicalEntryCommand // instanceof handles nulls
&& this.medicalHistory.equals(((AddMedicalEntryCommand) other).medicalHistory)
&& this.index.equals(((AddMedicalEntryCommand) other).index));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class DeleteMedicalEntryCommand extends PatientCommand {
+ PREFIX_INDEX + "1";

public static final String MESSAGE_SUCCESS = "Updated: \n";
public static final String MESSAGE_FAILURE_CANNOT_FIND =
"Doc'it cannot find the specified index of the medical history record from ";
public static final String MESSAGE_FAILURE_EMPTY = "No medical history record to delete from ";

private final Index patientIndex;
private final Index medicalIndex;
Expand Down Expand Up @@ -52,7 +55,11 @@ public CommandResult execute(Model model) throws CommandException {
Patient patientToEdit = lastShownList.get(patientIndex.getZeroBased());

if (patientToEdit.hasEmptyMedicalHistory()) {
throw new CommandException("No medical history record to delete from " + patientToEdit.getName() + ".");
throw new CommandException(MESSAGE_FAILURE_EMPTY + patientToEdit.getName() + ".");
}

if (patientToEdit.getMedicalHistory().size() < medicalIndex.getOneBased()) {
throw new CommandException(MESSAGE_FAILURE_CANNOT_FIND + patientToEdit.getName() + ".");
}

Patient editedPatient = patientToEdit.deleteMedicalHistory(medicalIndex);
Expand All @@ -64,4 +71,12 @@ public CommandResult execute(Model model) throws CommandException {

return new CommandResult(MESSAGE_SUCCESS + editedPatient);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeleteMedicalEntryCommand // instanceof handles nulls
&& this.patientIndex.equals(((DeleteMedicalEntryCommand) other).patientIndex)
&& this.medicalIndex.equals(((DeleteMedicalEntryCommand) other).medicalIndex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public DeleteMedicalEntryCommand parsePatientCommand(String args) throws ParseEx
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_INDEX);

if (!argMultimap.getValue(PREFIX_INDEX).isPresent()
|| argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
DeleteMedicalEntryCommand.MESSAGE_USAGE));
}

try {
patientIndex = ParserUtil.parseIndex(argMultimap.getPreamble());
medicalIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_INDEX).get());
Expand All @@ -34,12 +40,6 @@ public DeleteMedicalEntryCommand parsePatientCommand(String args) throws ParseEx
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteMedicalEntryCommand.MESSAGE_USAGE), pe);
}

if (!argMultimap.getValue(PREFIX_INDEX).isPresent()
|| argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
DeleteMedicalEntryCommand.MESSAGE_USAGE));
}

return new DeleteMedicalEntryCommand(patientIndex, medicalIndex);
}
}
22 changes: 22 additions & 0 deletions src/main/java/seedu/docit/model/patient/MedicalHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,28 @@ public static MedicalHistory generate() {
return mh;
}

/**
* Generates a Medical History that contains the given description and the date of record.
* @return a medical history object that contains nothing.
*/
public static MedicalHistory generate(String desc, String date) {
MedicalHistory mh = new MedicalHistory("");
mh.delete(0);
mh.add(desc, date);
return mh;
}

/**
* Generates a Medical History that contains the given description, with date of record being today.
* @return a medical history object that contains nothing.
*/
public static MedicalHistory generate(String desc) {
MedicalHistory mh = new MedicalHistory("");
mh.delete(0);
mh.add(desc);
return mh;
}

@Override
public String toString() { // to store the list into a CSV format
if (this.isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/docit/model/patient/Patient.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Patient deleteMedicalHistory(Index index) { // tell-don't-ask
return this;
}

if (this.medicalHistory.size() - 1 == 0) {
if (this.medicalHistory.size() - 1 == 0) { // deleting last time
return new Patient(name, phone, email, address, MedicalHistory.EMPTY_MEDICAL_HISTORY);
}

Expand Down
15 changes: 5 additions & 10 deletions src/main/java/seedu/docit/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ public class SampleDataUtil {
MedicalHistory.EMPTY_MEDICAL_HISTORY);

public static Patient[] getSamplePatients() {
MedicalHistory berniceMh = MedicalHistory.generate();
berniceMh.add("diabetes", "1 Oct 1999");
MedicalHistory berniceMh = MedicalHistory.generate("diabetes", "1 Oct 1999");

MedicalHistory charlotteMh = MedicalHistory.generate();
charlotteMh.add("scoliosis", "2 May 2000");
MedicalHistory charlotteMh = MedicalHistory.generate("scoliosis", "2 May 2000");

MedicalHistory davidMh = MedicalHistory.generate();
davidMh.add("stage 1a cancer", "5 Sep 2005");
MedicalHistory davidMh = MedicalHistory.generate("stage 1a cancer", "5 Sep 2005");

MedicalHistory irfanMh = MedicalHistory.generate();
irfanMh.add("high blood pressure", "8 Aug 2010");
MedicalHistory irfanMh = MedicalHistory.generate("high blood pressure", "8 Aug 2010");

MedicalHistory royMh = MedicalHistory.generate();
royMh.add("anxiety", "8 Aug 2017");
MedicalHistory royMh = MedicalHistory.generate("anxiety", "8 Aug 2017");

return new Patient[] {
patientAlex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ public JsonAdaptedMedicalEntry(@JsonProperty("description") String desc,
this.date = dateOfRecord;
}

/**
* Converts a given {@code MedicalEntry} into this class for Jackson use.
*/
public JsonAdaptedMedicalEntry(MedicalHistory.MedicalEntry source) {
this.description = source.getDescription();
this.date = source.getDateString();
}

public String getDescription() {
return description;
}
Expand All @@ -50,5 +42,4 @@ public MedicalHistory.MedicalEntry toModelType() throws IllegalValueException {
LocalDate dateOfRecord = LocalDate.parse(date, DateTimeFormatter.ofPattern("d MMM uuuu"));
return new MedicalHistory.MedicalEntry(description, dateOfRecord);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package seedu.docit.logic.commands;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.docit.testutil.Assert.assertThrows;
import static seedu.docit.testutil.TypicalIndexes.INDEX_FIRST_PATIENT;
import static seedu.docit.testutil.TypicalPatients.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.docit.commons.core.index.Index;
import seedu.docit.logic.commands.exceptions.CommandException;
import seedu.docit.model.AppointmentBook;
import seedu.docit.model.ArchivedAppointmentBook;
import seedu.docit.model.Model;
import seedu.docit.model.ModelManager;
import seedu.docit.model.UserPrefs;
import seedu.docit.model.patient.MedicalHistory;
import seedu.docit.model.patient.Patient;
import seedu.docit.testutil.PatientBuilder;
import seedu.docit.testutil.TypicalPatients;

public class AddMedicalEntryCommandTest {
private static final String DEFAULT_MEDICAL = "high blood pressure";

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

@Test
public void constructor_nullArguments_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AddMedicalEntryCommand(null, null));
}

@Test
public void execute_validMedicalEntry_addSuccessful() throws CommandException {
Patient editedPatient =
new PatientBuilder(getTypicalAddressBook().getPatientOfIndex(INDEX_FIRST_PATIENT)).build();

AddMedicalEntryCommand addMedicalEntryCommand = new AddMedicalEntryCommand(INDEX_FIRST_PATIENT,
new MedicalHistory(DEFAULT_MEDICAL));

CommandResult commandResult = addMedicalEntryCommand.execute(model);

CommandResult expectedCommandResult = new CommandResult(AddMedicalEntryCommand.MESSAGE_SUCCESS
+ editedPatient);

assertEquals(expectedCommandResult, commandResult);
}

@Test
public void execute_patientToAddDoesNotExist_throwsCommandException() throws CommandException {
int n = getTypicalAddressBook().getPatientList().size();
Index invalidIndex = Index.fromOneBased(n + 1);
Index otherInvalidIndex = Index.fromZeroBased(n);

AddMedicalEntryCommand addMedicalEntryCommand = new AddMedicalEntryCommand(invalidIndex,
new MedicalHistory(DEFAULT_MEDICAL));
AddMedicalEntryCommand otherAddMedicalEntryCommand = new AddMedicalEntryCommand(otherInvalidIndex,
new MedicalHistory(DEFAULT_MEDICAL));

assertThrows(CommandException.class, () -> addMedicalEntryCommand.execute(model));
assertThrows(CommandException.class, () -> otherAddMedicalEntryCommand.execute(model));
}

@Test
public void execute_validMedicalEntryFromEmptyMedicalHistory_addSuccessful() throws CommandException {
Patient targetPatient =
new PatientBuilder(getTypicalAddressBook().getPatientOfIndex(INDEX_FIRST_PATIENT)).build();

Patient editedPatient = TypicalPatients.makeEmptyMedicalHistory(targetPatient);

MedicalHistory toAdd = new MedicalHistory(DEFAULT_MEDICAL);

model.setPatient(targetPatient, editedPatient);
AddMedicalEntryCommand addMedicalEntryCommand = new AddMedicalEntryCommand(INDEX_FIRST_PATIENT,
toAdd);
CommandResult commandResult = addMedicalEntryCommand.execute(model);

// testing against really adding it Patient medical history
editedPatient = editedPatient.addMedicalHistory(toAdd);
CommandResult expectedCommandResult = new CommandResult(AddMedicalEntryCommand.MESSAGE_SUCCESS
+ editedPatient);

assertEquals(expectedCommandResult, commandResult);
}
}
105 changes: 105 additions & 0 deletions src/test/java/seedu/docit/logic/commands/DeleteMedicalEntryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package seedu.docit.logic.commands;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.docit.testutil.Assert.assertThrows;
import static seedu.docit.testutil.TypicalIndexes.INDEX_FIRST_PATIENT;
import static seedu.docit.testutil.TypicalPatients.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.docit.commons.core.index.Index;
import seedu.docit.logic.commands.exceptions.CommandException;
import seedu.docit.model.AppointmentBook;
import seedu.docit.model.ArchivedAppointmentBook;
import seedu.docit.model.Model;
import seedu.docit.model.ModelManager;
import seedu.docit.model.UserPrefs;
import seedu.docit.model.patient.Patient;
import seedu.docit.testutil.PatientBuilder;
import seedu.docit.testutil.TypicalPatients;

public class DeleteMedicalEntryTest {
private final Model model = new ModelManager(getTypicalAddressBook(), new AppointmentBook(),
new ArchivedAppointmentBook(), new UserPrefs());

@Test
public void constructor_nullArguments_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new DeleteMedicalEntryCommand(null, null));
}

@Test
public void execute_invalidPatientIndex_throwsCommandException() throws CommandException {
int numberOfPatients = getTypicalAddressBook().getPatientList().size();
int countOfMedicalEntriesFromFirstPatient = getTypicalAddressBook().getPatientOfIndex(Index.fromOneBased(1))
.getMedicalHistory().size();

Index validMedicalIndex = Index.fromOneBased(countOfMedicalEntriesFromFirstPatient);

Index invalidPatientIndex = Index.fromOneBased(numberOfPatients + 1);
Index otherInvalidPatientIndex = Index.fromZeroBased(numberOfPatients);

DeleteMedicalEntryCommand deleteMedicalEntryCommand =
new DeleteMedicalEntryCommand(invalidPatientIndex, validMedicalIndex);
DeleteMedicalEntryCommand otherDeleteMedicalEntryCommand =
new DeleteMedicalEntryCommand(otherInvalidPatientIndex, validMedicalIndex);

assertThrows(CommandException.class, () -> deleteMedicalEntryCommand.execute(model));
assertThrows(CommandException.class, () -> otherDeleteMedicalEntryCommand.execute(model));
}

// Test for patients who have an existing non-empty medical history but the medical entry specified does not exist
@Test
public void execute_medicalEntryDoesNotExist_throwsCommandException() throws CommandException {
Index validPatientIndex = INDEX_FIRST_PATIENT;
int countOfMedicalEntriesFromFirstPatient = getTypicalAddressBook().getPatientOfIndex(validPatientIndex)
.getMedicalHistory().size();

Index tooLargeMedical = Index.fromOneBased(countOfMedicalEntriesFromFirstPatient + 1);


DeleteMedicalEntryCommand deleteMedicalEntryCommand =
new DeleteMedicalEntryCommand(validPatientIndex, tooLargeMedical);

assertThrows(CommandException.class, () -> deleteMedicalEntryCommand.execute(model));
}

// Test for patients who have an empty medical history
@Test
public void execute_emptyMedicalHistory_throwsCommandException() throws CommandException {
Index validPatientIndex = INDEX_FIRST_PATIENT;
int countOfMedicalEntriesFromFirstPatient = getTypicalAddressBook().getPatientOfIndex(validPatientIndex)
.getMedicalHistory().size();

Patient targetPatient = getTypicalAddressBook().getPatientOfIndex(validPatientIndex);
Patient firstPatient = TypicalPatients.makeEmptyMedicalHistory(targetPatient);

model.setPatient(targetPatient, firstPatient);

Index tooLargeMedical = Index.fromOneBased(1);

DeleteMedicalEntryCommand deleteMedicalEntryCommand =
new DeleteMedicalEntryCommand(validPatientIndex, tooLargeMedical);

assertThrows(CommandException.class, () -> deleteMedicalEntryCommand.execute(model));
}

@Test
public void execute_validIndexValidPatientIndex_deleteSuccessful() throws CommandException {
Index validPatientIndex = INDEX_FIRST_PATIENT;
Patient firstPatient =
new PatientBuilder(getTypicalAddressBook().getPatientOfIndex(INDEX_FIRST_PATIENT)).build();
int countOfMedicalEntriesFromFirstPatient = firstPatient.getMedicalHistory().size();
Index validMedicalIndex = Index.fromOneBased(countOfMedicalEntriesFromFirstPatient);

DeleteMedicalEntryCommand deleteMedicalEntryCommand =
new DeleteMedicalEntryCommand(validPatientIndex, validMedicalIndex);
CommandResult commandResult = deleteMedicalEntryCommand.execute(model);

firstPatient = firstPatient.deleteMedicalHistory(validMedicalIndex);
CommandResult expectedCommandResult = new CommandResult(DeleteMedicalEntryCommand.MESSAGE_SUCCESS
+ firstPatient);

assertEquals(expectedCommandResult, commandResult);
}

}
Loading

0 comments on commit 7633504

Please sign in to comment.