diff --git a/src/test/java/seedu/duke/data/TransactionListTest.java b/src/test/java/seedu/duke/data/TransactionListTest.java index 6b48ea8f4..1779545e7 100644 --- a/src/test/java/seedu/duke/data/TransactionListTest.java +++ b/src/test/java/seedu/duke/data/TransactionListTest.java @@ -3,13 +3,19 @@ import org.junit.jupiter.api.Test; import seedu.duke.data.transaction.Expense; import seedu.duke.data.transaction.Income; +import seedu.duke.data.transaction.Transaction; +import seedu.duke.exception.InputTransactionInvalidTypeException; +import seedu.duke.exception.StatsInvalidTypeException; +import seedu.duke.parser.ParameterParser; import java.time.LocalDate; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class TransactionListTest { //@@author wcwy + @Test public void addExpense_addValidExpense_expectedSuccessfulExpenseAddition() { TransactionList transactions = new TransactionList(); @@ -78,4 +84,119 @@ public void calculateMonthlyTotalExpense_multipleDateDifferentYear_expectedCorre assertEquals(transactions.calculateMonthlyTotalExpense(LocalDate.of(2004, 1, 03)), 4); assertEquals(transactions.calculateMonthlyTotalExpense(LocalDate.of(2005, 1, 04)), 5); } + + //@@author chydarren + + @Test + public void findTransactions_matchingTransaction_expectNonEmptyTransactionList() { + TransactionList transactions = new TransactionList(); + transactions.addExpense("buy_an_apple", 1, "fruits", LocalDate.of(2001, 1, 1)); + transactions.addIncome("sell_a_pear", 2, "fruits", LocalDate.of(2002, 1, 1)); + + String transactionsList = transactions.findTransactions("sell"); + assertEquals(transactionsList, "[+][fruits] $2 on Jan 01 2002 | Description: sell_a_pear" + + System.lineSeparator()); + } + + @Test + public void findTransactions_noMatchingTransaction_expectEmptyTransactionList() { + TransactionList transactions = new TransactionList(); + transactions.addExpense("buy_an_apple", 1, "fruits", + LocalDate.of(2001, 1, 1)); + transactions.addIncome("sell_a_pear", 2, "fruits", + LocalDate.of(2002, 1, 1)); + + String transactionsList = transactions.findTransactions(""); + assertEquals(transactionsList, ""); + } + + @Test + public void getSpendingHabitComment_highSavingsRate_expectHighSavingsSpendingHabitComment() { + TransactionList transactions = new TransactionList(); + assertEquals(transactions.getSpendingHabitComment(50, 40), + "Wow, keep up the good work. You saved at least two-third of your income."); + } + + @Test + public void getSpendingHabitComment_divideByZeroIncome_expectVeryLowSavingsSpendingHabitComment() { + TransactionList transactions = new TransactionList(); + assertEquals(transactions.getSpendingHabitComment(0, -100), + "You spent way more than what you have earned for the current month. " + + "Please spend wisely based on your income."); + } + + @Test + public void isMatchListFilters_matchingTransactionDate_expectTrue() throws + InputTransactionInvalidTypeException { + TransactionList transactions = new TransactionList(); + Transaction transaction = new Income("sell_a_pear", 20, "fruits", + LocalDate.of(2002, 1, 1)); + + assertEquals(transactions.isMatchListFilters(transaction, "", "", + LocalDate.of(2002, 1, 1)), true); + + } + + @Test + public void isMatchListFilters_matchingTransactionType_expectTrue() throws + InputTransactionInvalidTypeException { + TransactionList transactions = new TransactionList(); + Transaction transaction = new Income("sell_a_pear", 20, "fruits", + LocalDate.of(2002, 1, 1)); + + assertEquals(transactions.isMatchListFilters(transaction, "seedu.duke.data.transaction.Income", + "", null), true); + } + + @Test + public void isMatchListFilters_matchingTransactionCategoryAndType_expectTrue() throws + InputTransactionInvalidTypeException { + TransactionList transactions = new TransactionList(); + Transaction transaction = new Income("sell_a_pear", 20, "fruits", + LocalDate.of(2002, 1, 1)); + + assertEquals(transactions.isMatchListFilters(transaction, "seedu.duke.data.transaction.Income", + "fruits", null), true); + } + + @Test + public void isMatchListFilters_NoMatchingTransactionDateAndCategory_expectFalse() throws + InputTransactionInvalidTypeException { + TransactionList transactions = new TransactionList(); + Transaction transaction = new Income("sell_a_pear", 20, "fruits", + LocalDate.of(2002, 1, 1)); + + assertEquals(transactions.isMatchListFilters(transaction, "", "veggies", + LocalDate.of(2022, 1, 1)), false); + } + + @Test + public void isMatchListFilters_invalidTransactionType_exceptionThrown() { + TransactionList transactions = new TransactionList(); + Transaction transaction = new Income("sell_a_pear", 20, "fruits", + LocalDate.of(2002, 1, 1)); + + assertThrows( + InputTransactionInvalidTypeException.class, + () -> transactions.isMatchListFilters(transaction, "income", "fruits", null) + ); + } + + @Test + public void isTransactionInstance_validTransactionInstanceAsExpense_expectTrue() { + TransactionList transactions = new TransactionList(); + Transaction transaction = new Expense("buy_a_bear", 80, "toys", + LocalDate.of(2022, 10, 30)); + + assertEquals(transaction, "seedu.duke.data.transaction.Expense"); + } + + @Test + public void isTransactionInstance_invalidTransactionInstanceAsExpense_expectFalse() { + TransactionList transactions = new TransactionList(); + Transaction transaction = new Income("buy_a_bear", 80, "toys", + LocalDate.of(2022, 10, 30)); + + assertEquals(transaction, "seedu.duke.data.transaction.Expense"); + } } diff --git a/src/test/java/seedu/duke/data/transaction/TransactionTest.java b/src/test/java/seedu/duke/data/transaction/TransactionTest.java index 9790743d1..f63ffbc41 100644 --- a/src/test/java/seedu/duke/data/transaction/TransactionTest.java +++ b/src/test/java/seedu/duke/data/transaction/TransactionTest.java @@ -1,54 +1,71 @@ package seedu.duke.data.transaction; import org.junit.jupiter.api.Test; -import seedu.duke.data.TransactionList; import java.time.LocalDate; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TransactionTest { //@@author chydarren - LocalDate date = LocalDate.of(2022, 1, 1); - Transaction transaction = new Income("Milked cows in the farm", 50, - "Salary", date); @Test - public void testGetDescription() { - assertEquals("Milked cows in the farm", transaction.getDescription()); + public void setDescription_setValidDescription_expectSuccessfulSetDescription() { + Transaction transaction = new Income("Milked_cows_in_the_farm", 50, + "Salary", LocalDate.of(2022, 1, 1)); + transaction.setDescription("Milked_cows_in_the_pantry"); + assertEquals("Milked_cows_in_the_pantry", transaction.getDescription()); } @Test - public void testSetDescription() { - transaction.setDescription("Helped the cows to wash their ass"); - assertEquals("Helped the cows to wash their ass", transaction.getDescription()); + public void setAmount_setValidAmount_expectSuccessfulSetAmount() { + Transaction transaction = new Income("Milked_cows_in_the_farm", 50, + "Salary", LocalDate.of(2022, 1, 1)); + transaction.setAmount(500); + assertEquals(500, transaction.getAmount()); } @Test - public void testGetAmount() { - assertEquals(50, transaction.getAmount()); + public void setCategory_setValidCategory_expectSuccessfulSetCategory() { + Transaction transaction = new Income("Milked_cows_in_the_farm", 50, + "Salary", LocalDate.of(2022, 1, 1)); + transaction.setCategory("Chore"); + assertEquals("Chore", transaction.getCategory()); } @Test - public void testSetAmount() { - transaction.setAmount(500); - assertEquals(500, transaction.getAmount()); + public void setDate_setValidDate_expectSuccessfulSetDate() { + Transaction transaction = new Income("Milked_cows_in_the_farm", 50, + "Salary", LocalDate.of(2022, 1, 1)); + transaction.setDate(LocalDate.of(2022, 10, 30)); + assertEquals(transaction.getDate(), LocalDate.of(2022, 10, 30)); } @Test - public void testGetCategory() { - assertEquals("Salary", transaction.getCategory()); + public void compareTo_inputTransactionWithEarlierDate_expectTransactionSortAsEarlier() { + Transaction transaction = new Income("Milked_cows_in_the_farm", 50, + "Salary", LocalDate.of(2022, 1, 1)); + Transaction earlierTransaction = new Expense("Bought_cow_food", 50, + "Food", LocalDate.of(2021, 12, 25)); + assertTrue(transaction.compareTo(earlierTransaction) > 0); } @Test - public void testSetCategory() { - transaction.setCategory("Love"); - assertEquals("Love", transaction.getCategory()); + public void compareTo_inputTransactionWithLaterDate_expectTransactionSortAsLater() { + Transaction transaction = new Income("Milked_cows_in_the_farm", 50, + "Salary", LocalDate.of(2022, 1, 1)); + Transaction laterTransaction = new Expense("Destroy_the_farm", 1000, + "Food", LocalDate.of(2022, 9, 20)); + assertTrue(transaction.compareTo(laterTransaction) < 0); } //@@author wcwy + @Test public void printFormattedDate_validDateFormat_expectCorrectFormatDate() { + Transaction transaction = new Income("Milked_cows_in_the_farm", 50, + "Salary", LocalDate.of(2022, 1, 1)); assertEquals(transaction.printFormattedDate(), "Jan 01 2022"); } } \ No newline at end of file