diff --git a/src/main/java/seedu/docit/logic/commands/AddMedicalEntryCommand.java b/src/main/java/seedu/docit/logic/commands/AddMedicalEntryCommand.java index 90acc7babe1..355224f6b92 100644 --- a/src/main/java/seedu/docit/logic/commands/AddMedicalEntryCommand.java +++ b/src/main/java/seedu/docit/logic/commands/AddMedicalEntryCommand.java @@ -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)); + } } diff --git a/src/main/java/seedu/docit/logic/commands/DeleteMedicalEntryCommand.java b/src/main/java/seedu/docit/logic/commands/DeleteMedicalEntryCommand.java index c0a755af670..b58b908a77f 100644 --- a/src/main/java/seedu/docit/logic/commands/DeleteMedicalEntryCommand.java +++ b/src/main/java/seedu/docit/logic/commands/DeleteMedicalEntryCommand.java @@ -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; @@ -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); @@ -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)); + } } diff --git a/src/main/java/seedu/docit/logic/parser/DeleteMedicalEntryCommandParser.java b/src/main/java/seedu/docit/logic/parser/DeleteMedicalEntryCommandParser.java index be7d482ae9f..ad01db6bc4e 100644 --- a/src/main/java/seedu/docit/logic/parser/DeleteMedicalEntryCommandParser.java +++ b/src/main/java/seedu/docit/logic/parser/DeleteMedicalEntryCommandParser.java @@ -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()); @@ -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); } } diff --git a/src/main/java/seedu/docit/model/patient/MedicalHistory.java b/src/main/java/seedu/docit/model/patient/MedicalHistory.java index caff102fc82..a4e39e08dfd 100644 --- a/src/main/java/seedu/docit/model/patient/MedicalHistory.java +++ b/src/main/java/seedu/docit/model/patient/MedicalHistory.java @@ -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()) { diff --git a/src/main/java/seedu/docit/model/patient/Patient.java b/src/main/java/seedu/docit/model/patient/Patient.java index fa3dadfd0c0..b328297f2a0 100644 --- a/src/main/java/seedu/docit/model/patient/Patient.java +++ b/src/main/java/seedu/docit/model/patient/Patient.java @@ -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); } diff --git a/src/main/java/seedu/docit/model/util/SampleDataUtil.java b/src/main/java/seedu/docit/model/util/SampleDataUtil.java index 596046c663a..6b71a3a7ae2 100644 --- a/src/main/java/seedu/docit/model/util/SampleDataUtil.java +++ b/src/main/java/seedu/docit/model/util/SampleDataUtil.java @@ -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, diff --git a/src/main/java/seedu/docit/storage/JsonAdaptedMedicalEntry.java b/src/main/java/seedu/docit/storage/JsonAdaptedMedicalEntry.java index 4c1dd2dabad..a53ac4428a7 100644 --- a/src/main/java/seedu/docit/storage/JsonAdaptedMedicalEntry.java +++ b/src/main/java/seedu/docit/storage/JsonAdaptedMedicalEntry.java @@ -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; } @@ -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); } - } diff --git a/src/test/java/seedu/docit/logic/commands/AddMedicalEntryCommandTest.java b/src/test/java/seedu/docit/logic/commands/AddMedicalEntryCommandTest.java new file mode 100644 index 00000000000..eb20fd5e808 --- /dev/null +++ b/src/test/java/seedu/docit/logic/commands/AddMedicalEntryCommandTest.java @@ -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); + } +} diff --git a/src/test/java/seedu/docit/logic/commands/DeleteMedicalEntryTest.java b/src/test/java/seedu/docit/logic/commands/DeleteMedicalEntryTest.java new file mode 100644 index 00000000000..4f231abcac6 --- /dev/null +++ b/src/test/java/seedu/docit/logic/commands/DeleteMedicalEntryTest.java @@ -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); + } + +} diff --git a/src/test/java/seedu/docit/logic/parser/AddMedicalEntryCommandParserTest.java b/src/test/java/seedu/docit/logic/parser/AddMedicalEntryCommandParserTest.java new file mode 100644 index 00000000000..45231892f85 --- /dev/null +++ b/src/test/java/seedu/docit/logic/parser/AddMedicalEntryCommandParserTest.java @@ -0,0 +1,54 @@ +package seedu.docit.logic.parser; + +import static seedu.docit.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.docit.logic.commands.CommandTestUtil.PREAMBLE_WHITESPACE; +import static seedu.docit.logic.parser.PatientCommandParserTestUtil.assertParseFailure; +import static seedu.docit.logic.parser.PatientCommandParserTestUtil.assertParseSuccess; +import static seedu.docit.testutil.TypicalIndexes.INDEX_FIRST_PATIENT; + +import org.junit.jupiter.api.Test; + +import seedu.docit.logic.commands.AddMedicalEntryCommand; +import seedu.docit.model.patient.MedicalHistory; + + + +public class AddMedicalEntryCommandParserTest { + private AddMedicalEntryCommandParser parser = new AddMedicalEntryCommandParser(); + + @Test + public void parsePatientCommand_validMedicalEntry_successFul() { + assertParseSuccess(parser, PREAMBLE_WHITESPACE + "1 m/diabetes", + new AddMedicalEntryCommand(INDEX_FIRST_PATIENT, new MedicalHistory("diabetes"))); + assertParseSuccess(parser, PREAMBLE_WHITESPACE + "1 m/random string", + new AddMedicalEntryCommand(INDEX_FIRST_PATIENT, new MedicalHistory("random string"))); + assertParseSuccess(parser, PREAMBLE_WHITESPACE + "1 m/ random string ", + new AddMedicalEntryCommand(INDEX_FIRST_PATIENT, new MedicalHistory("random string"))); + } + + @Test + public void parsePatientCommand_blankMedicalEntry_failure() { + String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddMedicalEntryCommand.MESSAGE_USAGE); + + String noEntry = ""; + String singleSpace = " "; + String noMedicalEntry = "1 "; + + assertParseFailure(parser, noEntry, expectedMessage); + assertParseFailure(parser, singleSpace, expectedMessage); + assertParseFailure(parser, noMedicalEntry, expectedMessage); + } + + @Test + public void parsePatientCommand_invalidMedicalEntry_failure() { + String wrongPrefixMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddMedicalEntryCommand.MESSAGE_USAGE); + String invalidMessage = MedicalHistory.MESSAGE_CONSTRAINTS; + + String incorrectPrefix = "1 i/diabetes"; + String invalidDescription = "1 m/@@@@"; + + assertParseFailure(parser, incorrectPrefix, wrongPrefixMessage); + assertParseFailure(parser, invalidDescription, invalidMessage); + } + +} diff --git a/src/test/java/seedu/docit/logic/parser/DeleteMedicalEntryCommandParserTest.java b/src/test/java/seedu/docit/logic/parser/DeleteMedicalEntryCommandParserTest.java new file mode 100644 index 00000000000..fd8871a831d --- /dev/null +++ b/src/test/java/seedu/docit/logic/parser/DeleteMedicalEntryCommandParserTest.java @@ -0,0 +1,43 @@ +package seedu.docit.logic.parser; + +import static seedu.docit.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.docit.logic.commands.CommandTestUtil.PREAMBLE_WHITESPACE; +import static seedu.docit.logic.parser.PatientCommandParserTestUtil.assertParseFailure; +import static seedu.docit.logic.parser.PatientCommandParserTestUtil.assertParseSuccess; +import static seedu.docit.testutil.TypicalIndexes.INDEX_FIRST_PATIENT; + +import org.junit.jupiter.api.Test; + +import seedu.docit.commons.core.index.Index; +import seedu.docit.logic.commands.DeleteMedicalEntryCommand; + +public class DeleteMedicalEntryCommandParserTest { + private DeleteMedicalEntryCommandParser parser = new DeleteMedicalEntryCommandParser(); + + @Test + public void parsePatientCommand_validDeletion_successFul() { + assertParseSuccess(parser, PREAMBLE_WHITESPACE + "1 i/1", + new DeleteMedicalEntryCommand(INDEX_FIRST_PATIENT, INDEX_FIRST_PATIENT)); + } + + @Test + public void parsePatientCommand_invalidPatientIndex_failure() { + Index invalidPatientIndex = Index.fromOneBased(Integer.MAX_VALUE); + Index negativePatientIndex = Index.fromOneBased(Integer.MIN_VALUE); + + String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, + DeleteMedicalEntryCommand.MESSAGE_USAGE); + + assertParseFailure(parser, invalidPatientIndex.getOneBased() + "i/1", expectedMessage); + assertParseFailure(parser, negativePatientIndex.getOneBased() + "i/1", expectedMessage); + } + + @Test + public void parsePatientCommand_invalidMedicalIndex_failure() { + Index invalidMedicalIndex = Index.fromOneBased(Integer.MIN_VALUE); + String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, + DeleteMedicalEntryCommand.MESSAGE_USAGE); + + assertParseFailure(parser, "1 i/" + invalidMedicalIndex.getOneBased(), expectedMessage); + } +} diff --git a/src/test/java/seedu/docit/model/person/MedicalHistoryTest.java b/src/test/java/seedu/docit/model/person/MedicalHistoryTest.java new file mode 100644 index 00000000000..d57a4f6d125 --- /dev/null +++ b/src/test/java/seedu/docit/model/person/MedicalHistoryTest.java @@ -0,0 +1,68 @@ +package seedu.docit.model.person; + +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.docit.testutil.Assert.assertThrows; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +import org.junit.jupiter.api.Test; + +import seedu.docit.model.patient.MedicalHistory; + +public class MedicalHistoryTest { + + @Test + public void constructor_null_sizeIsOne() { + MedicalHistory test = new MedicalHistory(null); + assertEquals(1, test.size()); + } + + @Test + public void constructor_validDescription_dateIsNow() { + MedicalHistory validMedicalHistory = new MedicalHistory("diabetes"); + MedicalHistory.MedicalEntry validMedicalEntry = validMedicalHistory.toList().get(0); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d MMM uuuu"); + assertEquals(LocalDate.now().format(formatter), validMedicalEntry.getDateString()); + } + + @Test + public void constructor_validDescription_descriptionIsStored() { + MedicalHistory validMedicalHistory = new MedicalHistory("diabetes"); + MedicalHistory.MedicalEntry validMedicalEntry = validMedicalHistory.toList().get(0); + assertEquals("diabetes", validMedicalEntry.getDescription()); + } + + @Test + public void isValidMedicalEntry() { + assertThrows(NullPointerException.class, () -> MedicalHistory.isValidMedicalEntry(null)); + + //invalid medical entry + assertFalse(MedicalHistory.isValidMedicalEntry("")); + assertFalse(MedicalHistory.isValidMedicalEntry("@@@@@@@@@@@")); + assertFalse(MedicalHistory.isValidMedicalEntry(";")); + assertFalse(MedicalHistory.isValidMedicalEntry(";/")); + + // valid medical entry - alphanumeric characters + assertTrue(MedicalHistory.isValidMedicalEntry("diabetes")); + assertTrue(MedicalHistory.isValidMedicalEntry("high blood pressure")); + assertTrue(MedicalHistory.isValidMedicalEntry("123456")); + assertTrue(MedicalHistory.isValidMedicalEntry("123456abc")); + } + + @Test + public void append_emptyMedicalHistory_returnsAppendedMedicalHistory() { + MedicalHistory toAppend = new MedicalHistory("diabetes"); + MedicalHistory result = MedicalHistory.EMPTY_MEDICAL_HISTORY.append(toAppend); + assertEquals(toAppend, result); + } + + @Test + public void delete_emptyMedicalHistory_returnsEmptyMedicalHistory() { + MedicalHistory deleted = MedicalHistory.EMPTY_MEDICAL_HISTORY.delete(0); + assertEquals(deleted, MedicalHistory.EMPTY_MEDICAL_HISTORY); + } + +} diff --git a/src/test/java/seedu/docit/model/person/PatientTest.java b/src/test/java/seedu/docit/model/person/PatientTest.java index b9c48cd5ce9..4b402f3d7c7 100644 --- a/src/test/java/seedu/docit/model/person/PatientTest.java +++ b/src/test/java/seedu/docit/model/person/PatientTest.java @@ -1,5 +1,6 @@ package seedu.docit.model.patient; +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.docit.logic.commands.CommandTestUtil.VALID_ADDRESS_BOB; @@ -11,6 +12,7 @@ import org.junit.jupiter.api.Test; +import seedu.docit.commons.core.index.Index; import seedu.docit.testutil.PatientBuilder; public class PatientTest { @@ -75,4 +77,51 @@ public void equals() { editedAlice = new PatientBuilder(ALICE).withAddress(VALID_ADDRESS_BOB).build(); assertFalse(ALICE.equals(editedAlice)); } + + // Testing if deleting from an EMPTY_MEDICAL_HISTORY returns an EMPTY_MEDICAL_HISTORY + @Test + public void deleteMedicalHistory_emptyMedicalHistory_returnsEmptyMedicalHistory() { + int len = ALICE.getMedicalHistory().size(); + + Patient editedPatient = new PatientBuilder(ALICE) + .withMedicalHistory("diabetes", "21 Oct 2021").build(); + + editedPatient = editedPatient.deleteMedicalHistory(Index.fromZeroBased(0)); + MedicalHistory editedMedicalHistory = editedPatient.getMedicalHistory(); + + assertEquals(MedicalHistory.EMPTY_MEDICAL_HISTORY, editedMedicalHistory); + } + + @Test + public void addMedicalHistory() { + Patient editedPatient = new PatientBuilder(ALICE).build(); + MedicalHistory toAdd = new MedicalHistory("diabetes"); + editedPatient = ALICE.addMedicalHistory(toAdd); + MedicalHistory controlTest = ALICE.getMedicalHistory(); + controlTest = controlTest.append(toAdd); + + assertEquals(controlTest, editedPatient.getMedicalHistory()); + } + + @Test + public void addMedicalHistory_emptyMedicalHistory_returnSameMedicalHistoryAdded() { + Patient editedPatient = new PatientBuilder(ALICE).build(); + MedicalHistory toAdd = new MedicalHistory("diabetes"); + editedPatient = ALICE.addMedicalHistory(MedicalHistory.EMPTY_MEDICAL_HISTORY); + + assertEquals(ALICE.getMedicalHistory(), editedPatient.getMedicalHistory()); + } + + @Test + public void hasEmptyMedicalHistory() { + int len = ALICE.getMedicalHistory().size(); + Patient editedPatient = new PatientBuilder(ALICE) + .withMedicalHistory("diabetes", "21 Oct 2021").build(); + + editedPatient = editedPatient.deleteMedicalHistory(Index.fromZeroBased(0)); + + assertTrue(editedPatient.hasEmptyMedicalHistory()); + } + + } diff --git a/src/test/java/seedu/docit/testutil/EditPatientDescriptorBuilder.java b/src/test/java/seedu/docit/testutil/EditPatientDescriptorBuilder.java index 364e770d8be..84bed2c0dc1 100644 --- a/src/test/java/seedu/docit/testutil/EditPatientDescriptorBuilder.java +++ b/src/test/java/seedu/docit/testutil/EditPatientDescriptorBuilder.java @@ -71,8 +71,7 @@ public EditPatientDescriptorBuilder withAddress(String address) { * Sets the {@code MedicalHistory} of the {@code EditPatientDescriptor} that we are building. */ public EditPatientDescriptorBuilder withMedicalHistory(String medicalHistory) { - MedicalHistory mh = MedicalHistory.generate(); - mh.add(medicalHistory); + MedicalHistory mh = MedicalHistory.generate(medicalHistory); descriptor.setMedicalHistory(mh); return this; } diff --git a/src/test/java/seedu/docit/testutil/PatientBuilder.java b/src/test/java/seedu/docit/testutil/PatientBuilder.java index 16699548a10..af92ab5ba9c 100644 --- a/src/test/java/seedu/docit/testutil/PatientBuilder.java +++ b/src/test/java/seedu/docit/testutil/PatientBuilder.java @@ -32,8 +32,7 @@ public PatientBuilder() { phone = new Phone(DEFAULT_PHONE); email = new Email(DEFAULT_EMAIL); address = new Address(DEFAULT_ADDRESS); - medicalHistory = MedicalHistory.generate(); - medicalHistory.add(DEFAULT_MEDICAL); + medicalHistory = MedicalHistory.generate(DEFAULT_MEDICAL); } /** @@ -91,8 +90,7 @@ public PatientBuilder withMedicalHistory(String medicalHistory) { * Sets the {@code MedicalHistory} of the {@code Patient} that we are building. */ public PatientBuilder withMedicalHistory(String medicalHistory, String date) { - this.medicalHistory = MedicalHistory.generate(); - this.medicalHistory.add(medicalHistory, date); + this.medicalHistory = MedicalHistory.generate(medicalHistory, date); return this; } diff --git a/src/test/java/seedu/docit/testutil/TypicalPatients.java b/src/test/java/seedu/docit/testutil/TypicalPatients.java index 9afcf114b33..967b2fae37a 100644 --- a/src/test/java/seedu/docit/testutil/TypicalPatients.java +++ b/src/test/java/seedu/docit/testutil/TypicalPatients.java @@ -13,6 +13,7 @@ import java.util.Arrays; import java.util.List; +import seedu.docit.commons.core.index.Index; import seedu.docit.model.AddressBook; import seedu.docit.model.patient.Patient; @@ -75,4 +76,18 @@ public static AddressBook getTypicalAddressBook() { public static List getTypicalPatients() { return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE)); } + + /** + * Makes the given {@code Patient} contain an {@code EMPTY_MEDICAL_HISTORY}. + * @param p patient to be edited. + * @return edited patient. + */ + public static Patient makeEmptyMedicalHistory(Patient p) { + Patient edited = p; + int countOfMedicalEntries = p.getMedicalHistory().size(); + for (int i = countOfMedicalEntries; i > 0; i--) { + edited = p.deleteMedicalHistory(Index.fromOneBased(i)); + } + return edited; + } }