Skip to content

Commit

Permalink
Merge pull request #185 from kylerwsm/master
Browse files Browse the repository at this point in the history
[v1.4.2] New test cases for DateOfBirth
  • Loading branch information
kthSim committed Apr 11, 2019
2 parents 7391320 + 80666a2 commit 2b9d55f
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public static DateOfBirth parseDob(String dob) throws ParseException {
String trimmedDob = dob.trim();
if (!DateOfBirth.isValidDate(dob)) {
throw new ParseException(DateOfBirth.MESSAGE_CONSTRAINTS);
} else if (!DateOfBirth.isNotFutureDay(dob)) {
throw new ParseException(DateOfBirth.MESSAGE_CONSTRAINTS_FUTURE_DAY);
}
return new DateOfBirth(trimmedDob);
}
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/seedu/address/model/datetime/DateOfBirth.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
public class DateOfBirth extends DateBase {
public static final String MESSAGE_CONSTRAINTS =
"Date of birth is compulsory, denoted by " + PREFIX_YEAR + " and should be in dd-MM-yyyy format.";
public static final String MESSAGE_CONSTRAINTS_FUTURE_DAY =
"Date of birth cannot be after today!";

/**
* Default constructor that takes in a birth day.
Expand All @@ -29,7 +31,18 @@ public DateOfBirth(String dob) {
*/
static boolean isDateBeforeToday(String test) {
String currentDateString = LocalDate.now().format(DateTimeFormatter.ofPattern(DATE_FORMAT));
return dateCompare(test, currentDateString);
return isToday(test) || dateCompare(test, currentDateString);
}

/**
* Test if both date represents the same day.
* @param test the date to be tested.
* @return true if same day, false otherwise.
*/
static boolean isToday(String test) {
DateBase today = DateOfBirth.getToday();
DateBase toTest = new DateBase(test);
return today.equals(toTest);
}

/**
Expand All @@ -38,7 +51,16 @@ static boolean isDateBeforeToday(String test) {
* @param test the string to be tested.
*/
public static boolean isValidDate(String test) {
return DateBase.isValidDate(test) && DateOfBirth.isDateBeforeToday(test);
return DateBase.isValidDate(test);
}

/**
* Returns true if the given dob is not after today.
* @param test the date to be tested.
* @return true if the date is before today, false otherwise.
*/
public static boolean isNotFutureDay(String test) {
return DateOfBirth.isDateBeforeToday(test);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public Person toModelType() throws IllegalValueException {
}
if (!DateOfBirth.isValidDate(dateOfBirth)) {
throw new IllegalValueException(DateOfBirth.MESSAGE_CONSTRAINTS);
} else if (!DateOfBirth.isNotFutureDay(dateOfBirth)) {
throw new IllegalValueException(DateOfBirth.MESSAGE_CONSTRAINTS_FUTURE_DAY);
}
final DateOfBirth modelDob = new DateOfBirth(dateOfBirth);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;

import seedu.address.MainApp;
import seedu.address.logic.commands.Command;
import seedu.address.logic.parser.exceptions.ParseException;

Expand All @@ -19,6 +20,8 @@ public static void assertParseSuccess(Parser parser, String userInput, Command e
Command command = parser.parse(userInput);
assertEquals(expectedCommand, command);
} catch (ParseException pe) {
pe.printStackTrace();
MainApp.getLogger().info("Entered command is: " + userInput);
throw new IllegalArgumentException("Invalid userInput.", pe);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@
import static seedu.address.logic.commands.CommandTestUtil.VALID_NRIC_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_SEX_BOB;
import static seedu.address.logic.parser.CliSyntax.PREFIX_YEAR;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalPersons.BOB;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

import seedu.address.logic.commands.PatientAddCommand;
import seedu.address.model.datetime.DateBase;
import seedu.address.model.datetime.DateOfBirth;
import seedu.address.model.patient.Nric;
import seedu.address.model.patient.Sex;
Expand Down Expand Up @@ -104,6 +111,86 @@ public void parse_compulsoryFieldMissing_failure() {
+ VALID_EMAIL_BOB + VALID_ADDRESS_BOB + VALID_SEX_BOB, expectedMessage);
}

@Test
public void parse_differentDates() {
String tempDob;

DateBase lastYear = DateOfBirth.getToday();
lastYear.setTo(lastYear.getDay(), lastYear.getMonth(), lastYear.getYear() - 1);

tempDob = " " + PREFIX_YEAR + lastYear.getRawFormat() + " ";
Person expectedPerson = new PersonBuilder(BOB).withDob(lastYear.getRawFormat()).build();

// last year -> valid dob
assertParseSuccess(parser, NAME_DESC_BOB + NRIC_DESC_BOB + tempDob
+ PHONE_DESC_BOB + EMAIL_DESC_AMY + EMAIL_DESC_BOB
+ ADDRESS_DESC_BOB + SEX_DESC_BOB, new PatientAddCommand(expectedPerson));

String yesterday = getYesterdayDateString();
tempDob = " " + PREFIX_YEAR + yesterday + " ";
expectedPerson = new PersonBuilder(BOB).withDob(yesterday).build();

// yesterday -> valid dob
assertParseSuccess(parser, NAME_DESC_BOB + NRIC_DESC_BOB + tempDob
+ PHONE_DESC_BOB + EMAIL_DESC_AMY + EMAIL_DESC_BOB
+ ADDRESS_DESC_BOB + SEX_DESC_BOB, new PatientAddCommand(expectedPerson));

DateBase today = DateOfBirth.getToday();

tempDob = " " + PREFIX_YEAR + today.getRawFormat() + " ";
expectedPerson = new PersonBuilder(BOB).withDob(today.getRawFormat()).build();

// today -> valid dob
assertParseSuccess(parser, NAME_DESC_BOB + NRIC_DESC_BOB + tempDob
+ PHONE_DESC_BOB + EMAIL_DESC_AMY + EMAIL_DESC_BOB
+ ADDRESS_DESC_BOB + SEX_DESC_BOB, new PatientAddCommand(expectedPerson));

// for the test failures below
String expectedMessage = DateOfBirth.MESSAGE_CONSTRAINTS_FUTURE_DAY;

String tomorrow = getTomorrowDateString();
tempDob = " " + PREFIX_YEAR + tomorrow + " ";

// tomorrow -> invalid dob
assertParseFailure(parser, NAME_DESC_BOB + NRIC_DESC_BOB + tempDob + SEX_DESC_BOB,
expectedMessage);

DateBase nextYear = DateOfBirth.getToday();
nextYear.setTo(nextYear.getDay(), nextYear.getMonth(), nextYear.getYear() + 1);

tempDob = " " + PREFIX_YEAR + nextYear.getRawFormat() + " ";

// next year compulsory fields only -> invalid dob
assertParseFailure(parser, NAME_DESC_BOB + NRIC_DESC_BOB + tempDob + SEX_DESC_BOB,
expectedMessage);

// next year all fields present -> invalid dob
assertParseFailure(parser, NAME_DESC_BOB + NRIC_DESC_BOB + tempDob + SEX_DESC_BOB
+ PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB, expectedMessage);
}

private Date yesterday() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return cal.getTime();
}

private Date tomorrow() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
return cal.getTime();
}

private String getYesterdayDateString() {
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
return dateFormat.format(yesterday());
}

private String getTomorrowDateString() {
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
return dateFormat.format(tomorrow());
}

@Test
public void parse_invalidValue_failure() {
// invalid name
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.Test;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.datetime.DateBase;
import seedu.address.model.datetime.DateOfBirth;
import seedu.address.model.description.Description;
import seedu.address.model.nextofkin.NextOfKinRelation;
Expand Down Expand Up @@ -171,6 +172,18 @@ public void toModelType_invalidDateOfBirth_throwsIllegalValueException() {
Assert.assertThrows(IllegalValueException.class, expectedMessage, person::toModelType);
}

@Test
public void toModelType_tomorrowDateOfBirth_throwsIllegalValueException() {
DateBase temp = DateOfBirth.getToday();
temp.setTo(temp.getDay(), temp.getMonth(), temp.getYear() + 1);
JsonAdaptedPerson person =
new JsonAdaptedPerson(VALID_NAME, VALID_NRIC, temp.toString(), VALID_SEX, VALID_PHONE, VALID_EMAIL,
VALID_ADDRESS, VALID_DRUG_ALLERGY, VALID_TEETH,
VALID_TAGS, VALID_RECORDS, VALID_KIN, VALID_DESC);
String expectedMessage = DateOfBirth.MESSAGE_CONSTRAINTS_FUTURE_DAY;
Assert.assertThrows(IllegalValueException.class, expectedMessage, person::toModelType);
}

@Test
public void toModelType_nullDateOfBirth_throwsIllegalValueException() {
JsonAdaptedPerson person =
Expand Down

0 comments on commit 2b9d55f

Please sign in to comment.