Skip to content

Commit

Permalink
Merge pull request #201 from bernarduskrishna/testing-model
Browse files Browse the repository at this point in the history
Add tests for model.
  • Loading branch information
bryanwee023 authored Nov 6, 2021
2 parents 903b703 + 400b460 commit 8212590
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,12 @@ public void addRevenueBookKeeping(Double revenue) {
public void initialiseBookKeeping() {
bookKeeping.initialise();
}

/**
* set bookKeeping.
* @param bookKeeping the bookKeeping it will be set to.
*/
public void setBookKeeping(BookKeeping bookKeeping) {
this.bookKeeping = bookKeeping;
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/TransactionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public boolean equalTestsTransactionLists (Object obj) {
if (this.transactionRecordList.size() != other.transactionRecordList.size()) {
return false;
}
for (int i = 0; i < this.transactionRecordList.size(); i += 1 ) {
for (int i = 0; i < this.transactionRecordList.size(); i += 1) {
if (!this.transactionRecordList.get(i).isSameTransactionInfo(
other.transactionRecordList.get(i).getOrderItems())) {
return false;
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/seedu/address/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class AddCommandTest {

private ModelStubAcceptingItemAdded modelStub = new ModelStubAcceptingItemAdded();
private ModelManager model = new ModelManager(getTypicalInventory(), new UserPrefs(),
TypicalTransactions.getTypicalTransaction(), TypicalBookkeeping.getTypicalBookkeeping());
TypicalTransactions.getTypicalTransactionList(), TypicalBookkeeping.getTypicalBookkeeping());

@Test
public void constructor_nullItem_throwsNullPointerException() {
Expand Down Expand Up @@ -139,7 +139,7 @@ public void execute_existingItemNameDescription_restockSuccessful() {
BookKeeping bookKeeping = TypicalBookkeeping.getTypicalBookkeeping();
bookKeeping.addCost(BAGEL.getCostPrice() * 5);
Model expectedModel = new ModelManager(getTypicalInventory(), new UserPrefs(),
TypicalTransactions.getTypicalTransaction(), bookKeeping);
TypicalTransactions.getTypicalTransactionList(), bookKeeping);
expectedModel.addItem(BAGEL);
expectedModel.restockItem(BAGEL, 5);

Expand All @@ -158,7 +158,7 @@ public void execute_existingItemIdDescription_restockSuccessful() {
BookKeeping bookKeeping = TypicalBookkeeping.getTypicalBookkeeping();
bookKeeping.addCost(BAGEL.getCostPrice() * 5);
Model expectedModel = new ModelManager(getTypicalInventory(), new UserPrefs(),
TypicalTransactions.getTypicalTransaction(), bookKeeping);
TypicalTransactions.getTypicalTransactionList(), bookKeeping);
expectedModel.addItem(BAGEL);
expectedModel.restockItem(BAGEL, 5);

Expand Down Expand Up @@ -207,7 +207,7 @@ public void execute_extraPriceFlags_restockSuccessful() {
BookKeeping bookKeeping = TypicalBookkeeping.getTypicalBookkeeping();
bookKeeping.addCost(BAGEL.getCostPrice() * 5);
Model expectedModel = new ModelManager(getTypicalInventory(), new UserPrefs(),
TypicalTransactions.getTypicalTransaction(), bookKeeping);
TypicalTransactions.getTypicalTransactionList(), bookKeeping);
expectedModel.addItem(BAGEL);
expectedModel.restockItem(BAGEL, 5);

Expand All @@ -227,7 +227,7 @@ public void execute_extraTagFlags_restockSuccessful() {
BookKeeping bookKeeping = TypicalBookkeeping.getTypicalBookkeeping();
bookKeeping.addCost(BAGEL.getCostPrice() * 5);
Model expectedModel = new ModelManager(getTypicalInventory(), new UserPrefs(),
TypicalTransactions.getTypicalTransaction(), bookKeeping);
TypicalTransactions.getTypicalTransactionList(), bookKeeping);
expectedModel.addItem(BAGEL);
expectedModel.restockItem(BAGEL, 5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ public class EndAndTransactOrderCommandTest {
public Path temporaryFolder;

private Model modelWithoutOrder = new ModelManager(getTypicalInventory(), new UserPrefs(),
TypicalTransactions.getTypicalTransaction(), TypicalBookkeeping.getTypicalBookkeeping());
TypicalTransactions.getTypicalTransactionList(), TypicalBookkeeping.getTypicalBookkeeping());

/**
* Returns a model with 5 donuts in its unclosed order
*/
private Model getModelWithOrderedDonut(Path path) {
UserPrefs userPrefs = new UserPrefs(path);
Model model = new ModelManager(getTypicalInventory(), userPrefs, TypicalTransactions.getTypicalTransaction(),
Model model = new ModelManager(getTypicalInventory(), userPrefs,
TypicalTransactions.getTypicalTransactionList(),
TypicalBookkeeping.getTypicalBookkeeping());
model.addItem(DONUT.updateCount(5));
model.setOrder(new Order());
Expand All @@ -58,12 +59,12 @@ public void execute_emptyOrder_success() {
EndAndTransactOrderCommand command = new EndAndTransactOrderCommand();

Model modelWithEmptyOrder = new ModelManager(getTypicalInventory(),
new UserPrefs(), TypicalTransactions.getTypicalTransaction(),
new UserPrefs(), TypicalTransactions.getTypicalTransactionList(),
TypicalBookkeeping.getTypicalBookkeeping());
modelWithEmptyOrder.setOrder(new Order());

Model modelWithoutOrder = new ModelManager(getTypicalInventory(),
new UserPrefs(), TypicalTransactions.getTypicalTransaction(),
new UserPrefs(), TypicalTransactions.getTypicalTransactionList(),
TypicalBookkeeping.getTypicalBookkeeping());
CommandResult expectedResult = new CommandResult(EndAndTransactOrderCommand.MESSAGE_EMPTY_ORDER);
assertCommandSuccess(command, modelWithEmptyOrder, expectedResult, modelWithoutOrder);
Expand All @@ -74,7 +75,8 @@ public void execute_normalTransaction_itemRemoved() {
String expectedMessage = EndAndTransactOrderCommand.MESSAGE_SUCCESS;

Model expectedModel = new ModelManager(getTypicalInventory(),
new UserPrefs(temporaryFolder.resolve("transaction.json")), TypicalTransactions.getTypicalTransaction(),
new UserPrefs(temporaryFolder.resolve("transaction.json")),
TypicalTransactions.getTypicalTransactionList(),
TypicalBookkeeping.getTypicalBookkeeping());
expectedModel.addItem(DONUT.updateCount(5));
expectedModel.setOrder(new Order());
Expand All @@ -95,7 +97,7 @@ public void execute_displayingOrder_itemRemovedAndDisplayInventory() {
EndAndTransactOrderCommand command = new EndAndTransactOrderCommand();
String expectedMessage = EndAndTransactOrderCommand.MESSAGE_SUCCESS;

TransactionList typicalTransactions = TypicalTransactions.getTypicalTransaction();
TransactionList typicalTransactions = TypicalTransactions.getTypicalTransactionList();
Model expectedModel = new ModelManager(getTypicalInventory(),
new UserPrefs(temporaryFolder.resolve("transaction.json")), typicalTransactions,
TypicalBookkeeping.getTypicalBookkeeping());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class RemoveFromOrderCommandTest {

private Model modelWithoutOrder = new ModelManager(getTypicalInventory(), new UserPrefs(),
TypicalTransactions.getTypicalTransaction(),
TypicalTransactions.getTypicalTransactionList(),
TypicalBookkeeping.getTypicalBookkeeping());
private Model modelWithOrder = getModelWithOrderedDonut();

Expand All @@ -33,7 +33,7 @@ public class RemoveFromOrderCommandTest {
*/
private Model getModelWithOrderedDonut() {
Model model = new ModelManager(getTypicalInventory(), new UserPrefs(),
TypicalTransactions.getTypicalTransaction(),
TypicalTransactions.getTypicalTransactionList(),
TypicalBookkeeping.getTypicalBookkeeping());
model.addItem(DONUT.updateCount(5));
model.setOrder(new Order());
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/seedu/address/model/DisplayListTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package seedu.address.model;

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.testutil.TypicalItems.APPLE_PIE;
import static seedu.address.testutil.TypicalItems.BAGEL;
import static seedu.address.testutil.TypicalItems.getTypicalInventory;
import static seedu.address.testutil.TypicalItems.getTypicalItems;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -56,6 +60,20 @@ public void setItems_sameSource_success() {
assertSourceListening(filteredList, itemSource);
}

@Test
public void altSetItems() {
ObservableList<Item> itemSource = FXCollections.observableArrayList(getTypicalItems());
List<Item> ls = new ArrayList<>(itemSource);
displayList.setItems(ls);

displayList.setPredicate(x -> x.equals(APPLE_PIE));

FilteredList<Item> expectedList = new FilteredList<>(getTypicalInventory().getItemList());
expectedList.setPredicate(x -> x.equals(APPLE_PIE));

assertEquals(filteredList, expectedList);
}

@Test
public void setPredicate_success() {
displayList.setPredicate(x -> x.equals(APPLE_PIE));
Expand All @@ -66,6 +84,12 @@ public void setPredicate_success() {
assertEquals(filteredList, expectedList);
}

@Test
public void equalsTest() {
assertTrue(displayList.equals(displayList));
assertFalse(displayList.equals(2));
}

/**
* Asserts that source and filtered lists match
* and any changes in the given source is propagated to the filteredList
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/seedu/address/model/InventoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static seedu.address.testutil.TypicalItems.getTypicalInventory;
import static seedu.address.testutil.TypicalOrders.getTypicalTransaction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -146,6 +147,28 @@ public void restockItem_success() {
assertEquals(inventory, expectedInventory);
}

@Test
public void addItem_moreThan1() {
inventory.addItems(new ArrayList<Item>(Arrays.asList(DONUT, BAGEL)));

Inventory expectedInventory = new Inventory();
expectedInventory.addItem(DONUT);
expectedInventory.addItem(BAGEL);

assertEquals(inventory, expectedInventory);
}

@Test
public void hashCodeTest() {
inventory.addItems(new ArrayList<Item>(Arrays.asList(DONUT, BAGEL)));

Inventory expectedInventory = new Inventory();
expectedInventory.addItem(DONUT);
expectedInventory.addItem(BAGEL);

assertEquals(inventory.hashCode(), expectedInventory.hashCode());
}

@Test
public void restockItem_itemNotInInventory_throwsException() {
inventory.addItem(BAGEL);
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/seedu/address/model/ModelManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static seedu.address.testutil.TypicalItems.BAGEL;
import static seedu.address.testutil.TypicalItems.BANANA_MUFFIN;
import static seedu.address.testutil.TypicalItems.DONUT;
import static seedu.address.testutil.TypicalTransactions.getTypicalTransactionList;

import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -79,6 +80,13 @@ public void setGuiSettings_validGuiSettings_setsGuiSettings() {
assertEquals(guiSettings, modelManager.getGuiSettings());
}

@Test
public void setBookKeeping_validBookKeeping_setBookKeeping() {
BookKeeping bookKeeping = new BookKeeping(3.0, 2.0, 1.0);
modelManager.setBookKeeping(bookKeeping);
assertEquals(bookKeeping, modelManager.getBookKeeping());
}

@Test
public void setInventoryFilePath_nullPath_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> modelManager.setInventoryFilePath(null));
Expand Down Expand Up @@ -244,6 +252,30 @@ public void updateFilteredItemList_displayingTransactions_throwClassCastExceptio
);
}

@Test
public void openTransactionTest() {
Inventory inventory = new InventoryBuilder().withItem(APPLE_PIE).withItem(BANANA_MUFFIN).build();
Inventory differentInventory = new Inventory();
UserPrefs userPrefs = new UserPrefs();

ModelManager modelManager = new ModelManager(inventory, userPrefs,
new TransactionList(getTypicalTransactionList()), new BookKeeping());
Double totalCost = modelManager.openTransaction("bagelid");
assertTrue(totalCost == BAGEL.getSalesPrice() * BAGEL.getCount());
}

@Test
public void openTransactionTest_empty() {
Inventory inventory = new InventoryBuilder().withItem(APPLE_PIE).withItem(BANANA_MUFFIN).build();
Inventory differentInventory = new Inventory();
UserPrefs userPrefs = new UserPrefs();

ModelManager modelManager = new ModelManager(inventory, userPrefs,
new TransactionList(), new BookKeeping());
Double totalCost = modelManager.openTransaction("bagelid");
assertTrue(totalCost == -1.0);
}

@Test
public void equals() {
Inventory inventory = new InventoryBuilder().withItem(APPLE_PIE).withItem(BANANA_MUFFIN).build();
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/seedu/address/model/TransactionListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package seedu.address.model;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.TypicalTransactions.getTypicalTransactionList;
import static seedu.address.testutil.TypicalTransactions.getTypicalTransactionList2;
import static seedu.address.testutil.TypicalTransactions.getTypicalTransactionList3;

import org.junit.jupiter.api.Test;

public class TransactionListTest {

private TransactionList transactionList1 = getTypicalTransactionList();
private TransactionList transactionList2 = getTypicalTransactionList2();
private TransactionList transactionList3 = getTypicalTransactionList3();
private TransactionList transactionList1Copy = getTypicalTransactionList();

@Test
public void equalSameObject() {
assertTrue(transactionList1.equals(transactionList1));
}

@Test
public void unequalNonTransaction() {
assertFalse(transactionList1.equals(1));
}

@Test
public void equalTestTransactionList() {
assertTrue(transactionList1.equalTestsTransactionLists(transactionList1));
}

@Test
public void unequalTestTransactionList_nonTransaction() {
assertFalse(transactionList1.equalTestsTransactionLists(1));
}

@Test
public void unequalTestTransactionList_diffSize() {
assertFalse(transactionList1.equalTestsTransactionLists(transactionList2));
}

@Test
public void unequalTestTransactionList_diffOrder() {
assertFalse(transactionList2.equalTestsTransactionLists(transactionList3));
}

@Test
public void equalTestTransactionList_diffObject() {
assertTrue(transactionList1.equalTestsTransactionLists(transactionList1Copy));
}

}
30 changes: 30 additions & 0 deletions src/test/java/seedu/address/model/UserPrefsTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package seedu.address.model;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;

import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

public class UserPrefsTest {
Expand All @@ -18,4 +22,30 @@ public void setInventoryFilePath_nullPath_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> userPrefs.setInventoryFilePath(null));
}

@Test
public void equals_sameObject() {
UserPrefs userPrefs = new UserPrefs();
assertTrue(userPrefs.equals(userPrefs));
}

@Test
public void unequal_notUserPrefs() {
UserPrefs userPrefs = new UserPrefs();
assertFalse(userPrefs.equals(1));
}

@Test
public void hashCodeTest() {
UserPrefs userPrefs = new UserPrefs();
assertTrue(userPrefs.hashCode() == userPrefs.hashCode());
}

@Test
public void getBookKeepingTest() {
UserPrefs userPrefs = new UserPrefs();
assertTrue(userPrefs.getBookKeepingFilePath().equals(Paths.get("data", "bookKeeping.json")));
}



}
Loading

0 comments on commit 8212590

Please sign in to comment.