From fc655bcf77e064bfbea2c845e42b77343c67c295 Mon Sep 17 00:00:00 2001 From: zoebelle-pang Date: Thu, 4 Apr 2024 01:32:27 +0800 Subject: [PATCH] Add test for payment command --- .../model/person/PaymentFilterPredicate.java | 2 +- .../logic/commands/PaymentCommandTest.java | 97 +++++++++++++++++++ .../parser/PaymentCommandParserTest.java | 35 +++++++ .../model/person/PaymentPredicateTest.java | 58 +++++++++++ 4 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 src/test/java/seedu/address/logic/commands/PaymentCommandTest.java create mode 100644 src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java create mode 100644 src/test/java/seedu/address/model/person/PaymentPredicateTest.java diff --git a/src/main/java/seedu/address/model/person/PaymentFilterPredicate.java b/src/main/java/seedu/address/model/person/PaymentFilterPredicate.java index 2456385383a..7019233725c 100644 --- a/src/main/java/seedu/address/model/person/PaymentFilterPredicate.java +++ b/src/main/java/seedu/address/model/person/PaymentFilterPredicate.java @@ -59,7 +59,7 @@ public boolean equals(Object other) { @Override public String toString() { return new ToStringBuilder(this.getClass().getSimpleName()) - .add("payment", filteredPayment) + .add("Payment", filteredPayment) .toString(); } } diff --git a/src/test/java/seedu/address/logic/commands/PaymentCommandTest.java b/src/test/java/seedu/address/logic/commands/PaymentCommandTest.java new file mode 100644 index 00000000000..caf19560ff4 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/PaymentCommandTest.java @@ -0,0 +1,97 @@ +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.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.FilterCommand.MESSAGE_FILTER_ADDRESS_BOOK_SUCCESS; +import static seedu.address.testutil.TypicalPersons.ALICE; +import static seedu.address.testutil.TypicalPersons.BENSON; +import static seedu.address.testutil.TypicalPersons.CARL; +import static seedu.address.testutil.TypicalPersons.DANIEL; +import static seedu.address.testutil.TypicalPersons.ELLE; +import static seedu.address.testutil.TypicalPersons.FIONA; +import static seedu.address.testutil.TypicalPersons.GEORGE; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.jupiter.api.Test; + +import seedu.address.logic.Messages; +import seedu.address.model.CommandHistory; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.person.*; + + +/** + * Contains integration tests (interaction with the Model) for {@code PaymentCommand}. + */ +public class PaymentCommandTest { + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), new CommandHistory()); + private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), new CommandHistory()); + + @Test + public void equals() { + PaymentFilterPredicate firstPredicate = + new PaymentFilterPredicate(new Payment()); + PaymentFilterPredicate secondPredicate = + new PaymentFilterPredicate(new Payment("Paid")); + + PaymentCommand PaymentFirstCommand = new PaymentCommand(firstPredicate); + PaymentCommand PaymentSecondCommand = new PaymentCommand(secondPredicate); + + // same object -> returns true + assertTrue(PaymentFirstCommand.equals(PaymentFirstCommand)); + + // same values -> returns true + PaymentCommand findFirstCommandCopy = new PaymentCommand(firstPredicate); + assertTrue(PaymentFirstCommand.equals(findFirstCommandCopy)); + + // different types -> returns false + assertFalse(PaymentFirstCommand.equals(1)); + + // null -> returns false + assertFalse(PaymentFirstCommand.equals(null)); + + // different person -> returns false + assertFalse(PaymentFirstCommand.equals(PaymentSecondCommand)); + } + + @Test + public void execute_filterPayment_noStudentFound() { + PaymentFilterPredicate predicate = + new PaymentFilterPredicate(new Payment("Not Paid")); + String expectedMessage = + String.format(MESSAGE_FILTER_ADDRESS_BOOK_SUCCESS + Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, + 0, predicate.filterResult()); + PaymentCommand command = new PaymentCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Collections.emptyList(), model.getFilteredPersonList()); + } + + @Test + public void execute_filterPayment_multipleStudentsFound() { + PaymentFilterPredicate predicate = + new PaymentFilterPredicate(new Payment("Paid")); + String expectedMessage = + String.format(MESSAGE_FILTER_ADDRESS_BOOK_SUCCESS + Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, + 7, predicate.filterResult()); + PaymentCommand command = new PaymentCommand(predicate); + expectedModel.updateFilteredPersonList(predicate); + assertCommandSuccess(command, model, expectedMessage, expectedModel); + assertEquals(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE), model.getFilteredPersonList()); + } + + @Test + public void toStringMethod() { + PaymentFilterPredicate predicate = new PaymentFilterPredicate(new Payment()); + PaymentCommand filterCommand = new PaymentCommand(predicate); + String expected = PaymentCommand.class.getSimpleName() + "{predicate=" + predicate + "}"; + assertEquals(expected, filterCommand.toString()); + } +} diff --git a/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java b/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java new file mode 100644 index 00000000000..a70b9ba5dbc --- /dev/null +++ b/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java @@ -0,0 +1,35 @@ +package seedu.address.logic.parser; + +import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; +import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; + +import org.junit.jupiter.api.Test; + +import seedu.address.logic.commands.PaymentCommand; +import seedu.address.model.person.Payment; +import seedu.address.model.person.PaymentFilterPredicate; + + +public class PaymentCommandParserTest { + + private PaymentCommandParser parser = new PaymentCommandParser(); + + @Test + public void parse_emptyArg_throwsParseException() { + assertParseFailure(parser, " ", + String.format(MESSAGE_INVALID_COMMAND_FORMAT, PaymentCommand.MESSAGE_USAGE)); + } + + @Test + public void parse_validArgs_returnsFilterCommand() { + // no trailing whitespaces + PaymentCommand expectedPaymentCommand = + new PaymentCommand(new PaymentFilterPredicate(new Payment("Paid"))); + assertParseSuccess(parser, " pa/paid", expectedPaymentCommand); + + // multiple whitespaces between keywords + assertParseSuccess(parser, " \n pa/ \n \t paid \t", expectedPaymentCommand); + } + +} diff --git a/src/test/java/seedu/address/model/person/PaymentPredicateTest.java b/src/test/java/seedu/address/model/person/PaymentPredicateTest.java new file mode 100644 index 00000000000..7f6ced54792 --- /dev/null +++ b/src/test/java/seedu/address/model/person/PaymentPredicateTest.java @@ -0,0 +1,58 @@ +package seedu.address.model.person; + +import org.junit.jupiter.api.Test; +import seedu.address.testutil.PersonBuilder; + +import static org.junit.jupiter.api.Assertions.*; + +public class PaymentPredicateTest { + + @Test + public void equals() { + PaymentFilterPredicate firstPredicate = new PaymentFilterPredicate( + new Payment()); + + PaymentFilterPredicate secondPredicate = new PaymentFilterPredicate( + new Payment("Paid")); + + // same object -> returns true + assertTrue(firstPredicate.equals(firstPredicate)); + + // same values -> returns true + PaymentFilterPredicate firstPredicateCopy = new PaymentFilterPredicate( + new Payment()); + assertTrue(firstPredicate.equals(firstPredicateCopy)); + + // different types -> returns false + assertFalse(firstPredicate.equals(1)); + + // null -> returns false + assertFalse(firstPredicate.equals(null)); + + // different filter -> returns false + assertFalse(firstPredicate.equals(secondPredicate)); + } + + @Test + public void test_filterContainsPayment_returnsTrue() { + PaymentFilterPredicate predicate = new PaymentFilterPredicate(new Payment("Paid")); + assertTrue(predicate.test(new PersonBuilder().withName("Alice Bob").build())); + } + + @Test + public void test_filterContainsGradeSubject_returnsFalse() { + PaymentFilterPredicate predicate = new PaymentFilterPredicate( + new Payment("Not Paid")); + assertFalse(predicate.test(new PersonBuilder().withName("Alice").build())); + } + + @Test + public void toStringMethod() { + String payment = "Paid"; + PaymentFilterPredicate predicate = new PaymentFilterPredicate( + new Payment(payment)); + + String expected = PaymentFilterPredicate.class.getSimpleName() + "{Payment=" + payment + "}"; + assertEquals(expected, predicate.toString()); + } +}