Skip to content

Commit

Permalink
Add tests for prescriptions
Browse files Browse the repository at this point in the history
* Prescription test work in progress

* Removed model with prescription stub

* Add test for prescription

* Add test for prescription

* Add test for prescription

* Add test for prescription

* Fix checkstyle

* Renamed parser test files

* Add tests for Prescription model

* Rename packages

* Add code quality improvements
  • Loading branch information
huyuxin0429 authored Nov 6, 2021
1 parent 7633504 commit 750d80b
Show file tree
Hide file tree
Showing 16 changed files with 589 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -40,9 +41,15 @@ public class AddPrescriptionCommand extends AppointmentCommand {
+ "Volume: %2$s\nDuration: %3$s";
public static final String MESSAGE_DUPLICATE_MEDICINE =
"This medicine already exists in the prescription for this appointment";
public static final String MESSAGE_FIELD_TOO_LONG =
"Medicine name can only be 20 characters long. Volume field can only be 20 characters long. "
+ "Duration field can only be 40 characters long.";
private static final String MESSAGE_FIELD_TOO_LONG =
"Medicine name can only be %1$s characters long. \nVolume field can only be %2$s characters long. "
+ "\nDuration field can only be %3$s characters long.";

public static final String INPUT_TOO_LONG_ERROR_MESSAGE = String.format(MESSAGE_FIELD_TOO_LONG,
Prescription.MEDICINE_CHAR_LENGTH_LIMIT,
Prescription.VOLUME_CHAR_LENGTH_LIMIT,
Prescription.DURATION_CHAR_LENGTH_LIMIT);

private static Logger logger = Logger.getLogger("AddPrescriptionCommand");

private final Index targetAppointmentIndex;
Expand All @@ -60,9 +67,9 @@ public AddPrescriptionCommand(Index targetAppointmentIndex, String medicine, Str
requireNonNull(volume);
requireNonNull(duration);
this.targetAppointmentIndex = targetAppointmentIndex;
this.volume = volume;
this.medicine = medicine;
this.duration = duration;
this.volume = volume.toLowerCase();
this.medicine = medicine.toLowerCase();
this.duration = duration.toLowerCase();
}

@Override
Expand All @@ -76,9 +83,9 @@ public CommandResult execute(Model model) throws CommandException {
+ Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX);
throw new CommandException(Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX);
}

assert (targetAppointmentIndex.getZeroBased() >= 0
&& targetAppointmentIndex.getZeroBased() < lastShownList.size());
Appointment appointmentToMakePrescription = lastShownList.get(targetAppointmentIndex.getZeroBased());

Prescription prescriptionToAdd = new Prescription(medicine, volume, duration);

if (appointmentToMakePrescription.containsPrescription(prescriptionToAdd)) {
Expand All @@ -87,15 +94,30 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_MEDICINE);
}

if (volume.length() > 20 || medicine.length() > 20 || duration.length() > 40) {
logger.log(Level.WARNING, "prescription adding error, "
+ MESSAGE_FIELD_TOO_LONG);
throw new CommandException(MESSAGE_FIELD_TOO_LONG);
if (volume.length() > Prescription.VOLUME_CHAR_LENGTH_LIMIT
|| medicine.length() > Prescription.MEDICINE_CHAR_LENGTH_LIMIT
|| duration.length() > Prescription.DURATION_CHAR_LENGTH_LIMIT) {
logger.log(Level.WARNING, INPUT_TOO_LONG_ERROR_MESSAGE);
throw new CommandException(INPUT_TOO_LONG_ERROR_MESSAGE);
}

model.addPrescription(appointmentToMakePrescription, prescriptionToAdd);
model.updateFilteredAppointmentList(Model.PREDICATE_SHOW_ALL_APPOINTMENTS);
logger.log(Level.INFO, "prescription adding success");
return new CommandResult(String.format(MESSAGE_SUCCESS, medicine, volume, duration));
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AddPrescriptionCommand that = (AddPrescriptionCommand) o;
return Objects.equals(targetAppointmentIndex, that.targetAppointmentIndex)
&& Objects.equals(medicine, that.medicine) && Objects.equals(volume, that.volume)
&& Objects.equals(duration, that.duration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

import seedu.docit.commons.core.Messages;
import seedu.docit.commons.core.index.Index;
Expand Down Expand Up @@ -32,6 +34,9 @@ public class DeletePrescriptionCommand extends AppointmentCommand {
public static final String MESSAGE_DELETE_PRESCRIPTION_SUCCESS = "Deleted prescription: \nMedicine: %1$s\n\n"
+ "from %2$s's appointment.";

private static Logger logger = Logger.getLogger("DeletePrescriptionCommand");


private final Index targetAppointmentIndex;
private final String targetMedicineName;

Expand All @@ -46,22 +51,29 @@ public DeletePrescriptionCommand(Index targetAppointmentIndex, String targetMedi
}

@Override public CommandResult execute(Model model) throws CommandException {
logger.log(Level.INFO, "going to start deleting prescription");
requireNonNull(model);
List<Appointment> lastShownList = model.getFilteredAppointmentList();

if (targetAppointmentIndex.getZeroBased() >= lastShownList.size()) {
logger.log(Level.WARNING, "deleting prescription failed, "
+ Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX);
throw new CommandException(Messages.MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX);
}
assert (targetAppointmentIndex.getZeroBased() >= 0
&& targetAppointmentIndex.getZeroBased() < lastShownList.size());

Appointment appointmentToTarget = lastShownList.get(targetAppointmentIndex.getZeroBased());
try {
model.deletePrescription(appointmentToTarget, targetMedicineName);
logger.log(Level.INFO, "deleting prescription success");
return new CommandResult(String.format(MESSAGE_DELETE_PRESCRIPTION_SUCCESS,
targetMedicineName, appointmentToTarget.getPatient().getName()));
} catch (MedicineNotFoundException e) {
logger.log(Level.WARNING, "deleting prescription failed, "
+ e.getMessage());
throw new CommandException(e.getMessage());
}

}

@Override
Expand All @@ -77,8 +89,4 @@ public boolean equals(Object o) {
&& Objects.equals(targetMedicineName, that.targetMedicineName);
}

@Override
public int hashCode() {
return Objects.hash(targetAppointmentIndex, targetMedicineName);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public AppointmentCommand parseAppointmentCommand(String commandWord, String arg
case SortAppointmentsCommand.COMMAND_WORD:
return new SortAppointmentsCommand();
case AddPrescriptionCommand.COMMAND_WORD:
return new AddPrescriptionCommandParser().parse(arguments);
return new AddPrescriptionCommandParser().parseAppointmentCommand(arguments);
case DeletePrescriptionCommand.COMMAND_WORD:
return new DeletePrescriptionCommandParser().parse(arguments);
return new DeletePrescriptionCommandParser().parseAppointmentCommand(arguments);
default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

import seedu.docit.commons.core.index.Index;
import seedu.docit.logic.commands.prescription.AddPrescriptionCommand;
import seedu.docit.logic.parser.AppointmentParser;
import seedu.docit.logic.parser.ArgumentMultimap;
import seedu.docit.logic.parser.ArgumentTokenizer;
import seedu.docit.logic.parser.CliSyntax;
import seedu.docit.logic.parser.Parser;
import seedu.docit.logic.parser.ParserUtil;
import seedu.docit.logic.parser.exceptions.ParseException;


/**
* Parses input arguments and creates a new AddPrescriptionCommand object
*/
public class AddPrescriptionCommandParser implements Parser<AddPrescriptionCommand> {
public class AddPrescriptionCommandParser implements AppointmentParser<AddPrescriptionCommand> {
public static final String EMPTY_FIELD_ERROR_MESSAGE = "Medicine/Duration/Volume fields cannot be blank.";

/**
* Parses the given {@code String} of arguments in the context of the AddPrescriptionCommand and returns an
Expand All @@ -25,30 +26,24 @@ public class AddPrescriptionCommandParser implements Parser<AddPrescriptionComma
* @throws ParseException when the input is invalid
*/
@Override
public AddPrescriptionCommand parse(String args) throws ParseException {
public AddPrescriptionCommand parseAppointmentCommand(String args) throws ParseException {
Index index;
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args,
CliSyntax.PREFIX_NAME, CliSyntax.PREFIX_DURATION, CliSyntax.PREFIX_VOLUME);

if (!ParserUtil.hasAllPrefixes(argMultimap, CliSyntax.PREFIX_VOLUME,
CliSyntax.PREFIX_NAME, CliSyntax.PREFIX_DURATION)) {
CliSyntax.PREFIX_NAME, CliSyntax.PREFIX_DURATION) || argMultimap.getPreamble().isBlank()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPrescriptionCommand.MESSAGE_USAGE));
}

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPrescriptionCommand.MESSAGE_USAGE), pe);
}

Index appointmentIndex = ParserUtil.parseIndex(argMultimap.getPreamble());
String medicineName = argMultimap.getValue(CliSyntax.PREFIX_NAME).get();
String duration = argMultimap.getValue(CliSyntax.PREFIX_DURATION).get();
String volume = argMultimap.getValue(CliSyntax.PREFIX_VOLUME).get();
return new AddPrescriptionCommand(index, medicineName, volume, duration);


if (medicineName.isBlank() || duration.isBlank() || volume.isBlank()) {
throw new ParseException(EMPTY_FIELD_ERROR_MESSAGE);
}
return new AddPrescriptionCommand(appointmentIndex, medicineName, volume, duration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,42 @@

import seedu.docit.commons.core.index.Index;
import seedu.docit.logic.commands.prescription.DeletePrescriptionCommand;
import seedu.docit.logic.parser.AppointmentParser;
import seedu.docit.logic.parser.ArgumentMultimap;
import seedu.docit.logic.parser.ArgumentTokenizer;
import seedu.docit.logic.parser.CliSyntax;
import seedu.docit.logic.parser.Parser;
import seedu.docit.logic.parser.ParserUtil;
import seedu.docit.logic.parser.exceptions.ParseException;

public class DeletePrescriptionCommandParser implements Parser<DeletePrescriptionCommand> {
public class DeletePrescriptionCommandParser implements AppointmentParser<DeletePrescriptionCommand> {
public static final String EMPTY_MEDICINE_FIELD_ERROR_MESSAGE = "Medicine fields cannot be blank.";
/**
* Parses the given {@code String} of arguments in the context of the DeletePrescriptionCommand and returns a
* DeletePrescriptionCommand object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
public DeletePrescriptionCommand parse(String args) throws ParseException {
public DeletePrescriptionCommand parseAppointmentCommand(String args) throws ParseException {
Index index;
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, CliSyntax.PREFIX_NAME);

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeletePrescriptionCommand.MESSAGE_USAGE), pe);
}

if (!ParserUtil.hasAllPrefixes(argMultimap, CliSyntax.PREFIX_NAME)
|| argMultimap.getPreamble().isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeletePrescriptionCommand.MESSAGE_USAGE));
}

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw pe;
}

String medicineName = argMultimap.getValue(CliSyntax.PREFIX_NAME).get();
return new DeletePrescriptionCommand(index, medicineName);

if (medicineName.isBlank()) {
throw new ParseException(EMPTY_MEDICINE_FIELD_ERROR_MESSAGE);
}
return new DeletePrescriptionCommand(index, medicineName);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void addPrescription(Prescription prescription) throws DuplicatePrescript
*/
public void removePrescription(String medicineName) throws MedicineNotFoundException {
if (!this.prescriptions.removeIf(p -> p.hasSameMedicalName(
new Prescription(medicineName, "", "")))) {
new Prescription(medicineName, "default", "default")))) {
throw new MedicineNotFoundException();
}
Set<Prescription> p = new HashSet<>();
Expand Down
Loading

0 comments on commit 750d80b

Please sign in to comment.