Skip to content

Commit

Permalink
Fix finding profiles by condition, add tests for condition predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-setyawan committed Apr 2, 2024
1 parent b826ac7 commit d2eda09
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public CommandResult execute(Model model) throws CommandException {
new NricContainsKeywordsPredicate(nric.toString())).findFirst().get();
Person updatedPerson = createUpdatedPerson(personToUpdate, updatePersonDescriptor);

if (personToUpdate.equals(updatedPerson) /* && model.hasPerson(updatedPerson) */) {
if (!personToUpdate.isSamePerson(updatedPerson) && model.hasPerson(updatedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public ConditionContainsKeywordsPredicate(List<String> keywords) {

@Override
public boolean test(Person person) {
if (person.getCondition() == null) {
return false;
}
return keywords.stream().anyMatch(
keyword -> StringUtil.containsPhraseIgnoreCase(person.getCondition().toString(), keyword));
}
Expand Down
25 changes: 18 additions & 7 deletions src/test/java/seedu/address/logic/commands/UpdateCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() {
@Test
public void execute_noFieldSpecifiedUnfilteredList_failure() {
UpdateCommand updateCommand = new UpdateCommand(ALICE.getNric(), new UpdatePersonDescriptor());
/* Person editedPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());

String expectedMessage = String.format(
UpdateCommand.MESSAGE_UPDATE_PERSON_SUCCESS, Messages.format(editedPerson));

Model expectedModel = new ModelManager(new ImmuniMate(model.getImmuniMate()), new UserPrefs());

assertCommandSuccess(updateCommand, model, expectedMessage, expectedModel); */
assertCommandFailure(updateCommand, model, UpdateCommand.MESSAGE_DUPLICATE_PERSON);
assertCommandSuccess(updateCommand, model, expectedMessage, expectedModel);
}

@Test
Expand All @@ -112,24 +111,36 @@ public void execute_filteredList_success() {
}

@Test
public void execute_duplicatePersonUnfilteredList_failure() {
public void execute_duplicatePersonUnfilteredList_success() {
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
UpdatePersonDescriptor descriptor = new UpdatePersonDescriptorBuilder(firstPerson).build();
UpdateCommand updateCommand = new UpdateCommand(ALICE.getNric(), descriptor);

assertCommandFailure(updateCommand, model, UpdateCommand.MESSAGE_DUPLICATE_PERSON);
String expectedMessage = String.format(
UpdateCommand.MESSAGE_UPDATE_PERSON_SUCCESS, Messages.format(firstPerson));

Model expectedModel = new ModelManager(new ImmuniMate(model.getImmuniMate()), new UserPrefs());
expectedModel.setPerson(model.getFilteredPersonList().get(0), firstPerson);

assertCommandSuccess(updateCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_duplicatePersonFilteredList_failure() {
public void execute_duplicatePersonFilteredList_success() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);

// edit person in filtered list into a duplicate in address book
Person personInList = model.getImmuniMate().getPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
UpdatePersonDescriptor descriptor = new UpdatePersonDescriptorBuilder(personInList).build();
UpdateCommand updateCommand = new UpdateCommand(ALICE.getNric(), descriptor);

assertCommandFailure(updateCommand, model, UpdateCommand.MESSAGE_DUPLICATE_PERSON);
String expectedMessage = String.format(
UpdateCommand.MESSAGE_UPDATE_PERSON_SUCCESS, Messages.format(personInList));

Model expectedModel = new ModelManager(new ImmuniMate(model.getImmuniMate()), new UserPrefs());
expectedModel.setPerson(model.getFilteredPersonList().get(0), personInList);

assertCommandSuccess(updateCommand, model, expectedMessage, expectedModel);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package seedu.address.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 java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;

import seedu.address.testutil.PersonBuilder;

public class ConditionContainsKeywordsPredicateTest {

@Test
public void equals() {
List<String> firstPredicateKeywordList = Collections.singletonList("first");
List<String> secondPredicateKeywordList = Arrays.asList("first", "second");

ConditionContainsKeywordsPredicate firstPredicate =
new ConditionContainsKeywordsPredicate(firstPredicateKeywordList);
ConditionContainsKeywordsPredicate secondPredicate =
new ConditionContainsKeywordsPredicate(secondPredicateKeywordList);

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

// same values -> returns true
ConditionContainsKeywordsPredicate firstPredicateCopy =
new ConditionContainsKeywordsPredicate(firstPredicateKeywordList);
assertTrue(firstPredicate.equals(firstPredicateCopy));

// different types -> returns false
assertFalse(firstPredicate.equals(1));

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

// different person -> returns false
assertFalse(firstPredicate.equals(secondPredicate));
}

@Test
public void test_nameContainsKeywords_returnsTrue() {
// One keyword
ConditionContainsKeywordsPredicate predicate =
new ConditionContainsKeywordsPredicate(Collections.singletonList("runny nose"));
assertTrue(predicate.test(new PersonBuilder().withCondition("very runny nose").build()));

// Multiple keywords
predicate = new ConditionContainsKeywordsPredicate(Arrays.asList("myopia", "diabetes"));
assertTrue(predicate.test(new PersonBuilder().withCondition("myopia, diabetes").build()));

// Only one matching keyword
predicate = new ConditionContainsKeywordsPredicate(Arrays.asList("myopia", "diabetes"));
assertTrue(predicate.test(new PersonBuilder().withCondition("severe myopia").build()));

// Mixed-case keywords
predicate = new ConditionContainsKeywordsPredicate(Arrays.asList("mYoPiA", "dIaBeTeS"));
assertTrue(predicate.test(new PersonBuilder().withCondition("myopia, diabetes").build()));
}

@Test
public void test_nameDoesNotContainKeywords_returnsFalse() {
// Zero keywords
ConditionContainsKeywordsPredicate predicate = new ConditionContainsKeywordsPredicate(Collections.emptyList());
assertFalse(predicate.test(new PersonBuilder().withCondition("myopia").build()));

// Non-matching keyword
predicate = new ConditionContainsKeywordsPredicate(Arrays.asList("high blood pressure"));
assertFalse(predicate.test(new PersonBuilder().withCondition("myopia, diabetes").build()));

// Keywords match phone and address, but does not match condition
predicate = new ConditionContainsKeywordsPredicate(Arrays.asList("91234567", "alice@email.com", "myopia"));
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("91234567")
.withAddress("Main Street").withCondition("diabetes").build()));
}

@Test
public void toStringMethod() {
List<String> keywords = List.of("keyword1", "keyword2");
ConditionContainsKeywordsPredicate predicate = new ConditionContainsKeywordsPredicate(keywords);

String expected = ConditionContainsKeywordsPredicate.class.getCanonicalName() + "{keywords=" + keywords + "}";
assertEquals(expected, predicate.toString());
}
}

0 comments on commit d2eda09

Please sign in to comment.