Skip to content

Commit

Permalink
Add and update test cases for testing critical functions in Transacti…
Browse files Browse the repository at this point in the history
…on and TransactionList
  • Loading branch information
chydarren committed Oct 31, 2022
1 parent ec2ab77 commit c7bb73a
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 19 deletions.
121 changes: 121 additions & 0 deletions src/test/java/seedu/duke/data/TransactionListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
}
}
55 changes: 36 additions & 19 deletions src/test/java/seedu/duke/data/transaction/TransactionTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit c7bb73a

Please sign in to comment.