Skip to content

Commit

Permalink
Add StatisticsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
zht96830 committed Apr 14, 2019
1 parent d796da0 commit 0615cdd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/main/java/seedu/address/model/statistics/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import java.util.ArrayList;

import javafx.collections.transformation.FilteredList;
import seedu.address.model.attributes.Category;
import seedu.address.model.attributes.Date;
import seedu.address.model.attributes.Frequency;
import seedu.address.model.expense.Expense;

import static java.util.Objects.requireNonNull;

/**
* Statistics
*/
Expand All @@ -26,8 +28,6 @@ public class Statistics {
public static final int OTHERS = 8;
public static final int ALL = 9;


protected Category category;
protected String html;
protected FilteredList<Expense> statsExpenses;

Expand All @@ -36,6 +36,7 @@ public class Statistics {
* statsExpense must be present and not null.
*/
public Statistics(FilteredList<Expense> statsExpenses) {
requireNonNull(statsExpenses);
this.statsExpenses = statsExpenses;
}

Expand Down Expand Up @@ -71,6 +72,8 @@ public void calculateStats(String command, Date d1, Date d2, Frequency frequency
* All parameters must be present and not null.
*/
private void basicStats(Date startDate, Date endDate) {
requireNonNull(startDate);
requireNonNull(endDate);

ArrayList<ArrayList<Expense>> data = extractRelevantExpenses(startDate, endDate);
ArrayList<ArrayList<Object>> tableData = generateSummary(data);
Expand All @@ -97,6 +100,10 @@ private void basicStats(Date startDate, Date endDate) {
* All parameters must be present and not null.
*/
private void compareStats(Date date1, Date date2, Frequency frequency) {
requireNonNull(date1);
requireNonNull(date2);
requireNonNull(frequency);

Date startDate1 = date1;
Date startDate2 = date2;

Expand Down Expand Up @@ -161,6 +168,9 @@ private void compareStats(Date date1, Date date2, Frequency frequency) {
* All parameters must be present and not null.
*/
private void trendStats(Date startDate, Date endDate, Frequency frequency) {
requireNonNull(startDate);
requireNonNull(endDate);
requireNonNull(frequency);

ArrayList<ArrayList<ArrayList<Expense>>> data = new ArrayList<>();
Date dateTracker = startDate;
Expand Down Expand Up @@ -352,6 +362,8 @@ private ArrayList<ArrayList<Expense>> extractRelevantExpenses(Date startDate, Da
* Converts String Category into Int Category
*/
protected int convertCategoryToInteger(String categoryInString) {
requireNonNull(categoryInString);

switch (categoryInString) {
case "FOOD":
return FOOD;
Expand Down Expand Up @@ -382,6 +394,7 @@ protected int convertCategoryToInteger(String categoryInString) {
* Converts Int Category into String Category
*/
protected String convertIntegerCategoryToString(int categoryInInteger) {
requireNonNull(categoryInInteger);
switch (categoryInInteger) {
case FOOD:
return "FOOD";
Expand Down Expand Up @@ -412,6 +425,9 @@ protected String convertIntegerCategoryToString(int categoryInInteger) {
* Builds table for display
*/
private String htmlTableBuilder(ArrayList<String> header, ArrayList<ArrayList<String>> data) {
requireNonNull(header);
requireNonNull(data);

String table;
table = "<table style=\"width:100%\">\n";

Expand Down
78 changes: 78 additions & 0 deletions src/test/java/seedu/address/model/statistics/StatisticsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package seedu.address.model.statistics;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static seedu.address.testutil.TypicalExpenses.DUCK_RICE;

import javafx.collections.transformation.FilteredList;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import seedu.address.model.attributes.Date;
import seedu.address.model.attributes.Frequency;
import seedu.address.model.expense.Expense;
import seedu.address.model.expense.ExpenseList;
import seedu.address.testutil.Assert;

public class StatisticsTest {

private ExpenseList expenseList = new ExpenseList();
FilteredList<Expense> statsExpenses = new FilteredList<Expense>(
expenseList.asUnmodifiableObservableList());

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void constructor_null_throwsNullPointerException() {
Assert.assertThrows(NullPointerException.class, () ->
new Statistics(null));
}

@Test
public void basicStats_parameters_null_throwsNullPointerException() {
Statistics stats = new Statistics(statsExpenses);
Date date = new Date("01-01-2019");
//startDate cannot be null
Assert.assertThrows(NullPointerException.class, () ->
stats.calculateStats("stats", null, date, null));
//endDate cannot be null
Assert.assertThrows(NullPointerException.class, () ->
stats.calculateStats("stats", date, null, null));
}

@Test
public void compareStats_parameters_null_throwsNullPointerException() {
Statistics stats = new Statistics(statsExpenses);
Date date = new Date("01-01-2019");
Frequency frequency = new Frequency("M");
//date1 cannot be null
Assert.assertThrows(NullPointerException.class, () ->
stats.calculateStats("compare", null, date , frequency));
//date2 cannot be null
Assert.assertThrows(NullPointerException.class, () ->
stats.calculateStats("compare", date, null, frequency));
//date2 cannot be null
Assert.assertThrows(NullPointerException.class, () ->
stats.calculateStats("compare", date, date, null));
}

@Test
public void trendStats_parameters_null_throwsNullPointerException() {
Statistics stats = new Statistics(statsExpenses);
Date date = new Date("01-01-2019");
Frequency frequency = new Frequency("M");
//date1 cannot be null
Assert.assertThrows(NullPointerException.class, () ->
stats.calculateStats("trend", null, date , frequency));
//date2 cannot be null
Assert.assertThrows(NullPointerException.class, () ->
stats.calculateStats("trend", date, null, frequency));
//date2 cannot be null
Assert.assertThrows(NullPointerException.class, () ->
stats.calculateStats("trend", date, date, null));
}


}

0 comments on commit 0615cdd

Please sign in to comment.