Skip to content

Commit

Permalink
Add test cases for finding contacts by email
Browse files Browse the repository at this point in the history
  • Loading branch information
yooplo committed Oct 10, 2024
1 parent d882023 commit cb0a3f9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package seedu.address.logic.commands;

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.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import seedu.address.model.person.EmailContainsKeywordsPredicate;
import static seedu.address.testutil.TypicalPersons.CARL;
import static seedu.address.testutil.TypicalPersons.ELLE;
import static seedu.address.testutil.TypicalPersons.FIONA;
import static seedu.address.testutil.TypicalPersons.INA;
import static seedu.address.testutil.TypicalPersons.REG;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.util.Arrays;
import java.util.Collections;

import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.ContactContainsKeywordsPredicate;
import seedu.address.model.person.NameContainsKeywordsPredicate;

/**
* Contains integration tests (interaction with the Model) for {@code FindByContactCommand}.
*/
public class FindByEmailCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void equals() {
EmailContainsKeywordsPredicate firstPredicate =
new EmailContainsKeywordsPredicate(Collections.singletonList("first"));
EmailContainsKeywordsPredicate secondPredicate =
new EmailContainsKeywordsPredicate(Collections.singletonList("second"));

FindByEmailCommand findFirstCommand = new FindByEmailCommand(firstPredicate);
FindByEmailCommand findSecondCommand = new FindByEmailCommand(secondPredicate);

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

// same values -> returns true
FindByEmailCommand findFirstCommandCopy = new FindByEmailCommand(firstPredicate);
assertTrue(findFirstCommand.equals(findFirstCommandCopy));

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

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

// different person -> returns false
assertFalse(findFirstCommand.equals(findSecondCommand));
}

@Test
public void execute_zeroKeywords_noPersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
EmailContainsKeywordsPredicate predicate = preparePredicate(" ");
FindByEmailCommand command = new FindByEmailCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void execute_multipleKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2);
EmailContainsKeywordsPredicate predicate = preparePredicate("[email protected]");
FindByEmailCommand command = new FindByEmailCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(REG, INA), model.getFilteredPersonList());
}

@Test
public void toStringMethod() {
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
FindByNameCommand findCommand = new FindByNameCommand(predicate);
String expected = FindByNameCommand.class.getCanonicalName() + "{predicate=" + predicate + "}";
assertEquals(expected, findCommand.toString());
}

/**
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
*/
private EmailContainsKeywordsPredicate preparePredicate(String userInput) {
return new EmailContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}
}
8 changes: 7 additions & 1 deletion src/test/java/seedu/address/testutil/TypicalPersons.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public class TypicalPersons {
public static final Person IDA = new PersonBuilder().withName("Ida Mueller").withPhone("8482131")
.withEmail("[email protected]").withAddress("chicago ave").build();

// same emails for FindByEmailCommandTest
public static final Person REG = new PersonBuilder().withName("Reg Loo").withPhone("8482424")
.withEmail("[email protected]").withAddress("little india").build();
public static final Person INA = new PersonBuilder().withName("Ina Crust").withPhone("8482131")
.withEmail("[email protected]").withAddress("chicago ave").build();

// Manually added - Person's details found in {@code CommandTestUtil}
public static final Person AMY = new PersonBuilder().withName(VALID_NAME_AMY).withPhone(VALID_PHONE_AMY)
.withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY).withTags(VALID_TAG_FRIEND).build();
Expand All @@ -71,6 +77,6 @@ public static AddressBook getTypicalAddressBook() {
}

public static List<Person> getTypicalPersons() {
return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE));
return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE, REG, INA));
}
}

0 comments on commit cb0a3f9

Please sign in to comment.