diff options
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/edu/brown/cs/student/ProfitCalculationTest.java | 96 |
1 files changed, 89 insertions, 7 deletions
diff --git a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java index 256afed..fcb0de4 100644 --- a/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java +++ b/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java @@ -12,6 +12,9 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.sql.Date; import java.time.Instant; @@ -35,7 +38,7 @@ public class ProfitCalculationTest { @Before public void setUp() { try { - db = new DatabaseQuerier("data/testing/test_trades.sqlite3"); + db = new DatabaseQuerier("data/profit_testing.sqlite3"); } catch (Exception e) { System.out.println("DBQuerier Test, couldn't connect to db???"); } @@ -47,16 +50,95 @@ public class ProfitCalculationTest { } @Test - public void testEmptyDB() { + public void testBasicTrades() { setUp(); ProfitCalculation profitCalculation = - new ProfitCalculation(DatabaseQuerier.getConn(), "CAKEBREAD STEVEN", new Date(1518010558000l), - new Date(1718010556000l)); - List<StockHolding> trade = profitCalculation.getHoldingsList(); - double gain = trade.get(0).getUnrealizedGain(); - assertEquals(294800.0, gain, .01); + new ProfitCalculation(DatabaseQuerier.getConn(), "Don", new Date(1518010558000l), + new Date(1618698807000l)); + //price of GME at end time is 154.69 + List<StockHolding> trade = profitCalculation.getHoldingsList(-1); + //buy with no sell + assertEquals(trade.get(0).getUnrealizedGain(), 3842.25, .25); + assertEquals(trade.get(0).getRealizedGain(), 0, .01); + + //just sell + profitCalculation = + new ProfitCalculation(DatabaseQuerier.getConn(), "SELL", new Date(1518010558000l), + new Date(1618698807000l)); + trade = profitCalculation.getHoldingsList(-1); + assertTrue(trade.isEmpty()); + assertEquals(profitCalculation.calculateGainsSingle(-1), 0, 0.001); + + tearDown(); } + @Test + public void otherBuySellCases() { + setUp(); + //buy and sell at same timestamp + ProfitCalculation profitCalculation = + new ProfitCalculation(DatabaseQuerier.getConn(), "concurrentBS", new Date(1518010558000l), + new Date(1715629591000l)); + + Map<Integer, Double> map = profitCalculation.getProfitMap(); + + assertEquals(map.get(100), 1, .01); + //buys at multiple prices + profitCalculation = + new ProfitCalculation(DatabaseQuerier.getConn(), "mulitpleBuyPrices", + new Date(1518010558000l), + new Date(1715629591000l)); + + assertEquals(profitCalculation.getHoldingsList(-1).get(0).getRealizedGain(), 3750, 0.01); + assertEquals(profitCalculation.getMoneyInput(), 3750, .01); + + //left over holdings + profitCalculation = + new ProfitCalculation(DatabaseQuerier.getConn(), "dontSellAll", + new Date(1518010558000l), + new Date(1715629591000l)); + + assertEquals(profitCalculation.getHoldingsList(-1).get(0).getShares(), 25, .01); + tearDown(); + } + + @Test + public void testAPICalls() { + ProfitCalculation profitCalculation = + new ProfitCalculation(DatabaseQuerier.getConn(), "Don", new Date(1618234200000l), + new Date(1618703800000l)); + + //check sp500 calculation. 411.28 to 417.30 + assertEquals(profitCalculation.compareToSP500(), .01464, .001); + + tearDown(); + + } + + @Test + public void databaseAndConnectionIssues() { + //no database connection + ProfitCalculation profitCalculation = + new ProfitCalculation(null, "Don", new Date(1518010558000l), + new Date(1618698807000l)); + assertEquals(profitCalculation.getProfitMap(), new HashMap<>()); + + assertEquals(profitCalculation.getHoldingsList(-1), new LinkedList<>()); + + setUp(); + //invalid person + profitCalculation = + new ProfitCalculation(DatabaseQuerier.getConn(), "1234", new Date(1518010558000l), + new Date(1618698807000l)); + assertEquals(profitCalculation.getHoldingsList(-1), new LinkedList<>()); + + + //invalid stock ticker + profitCalculation = + new ProfitCalculation(DatabaseQuerier.getConn(), "invalidTicker", new Date(1518010558000l), + new Date(1618698807000l)); + assertTrue(profitCalculation.getHoldingsList(-1).isEmpty()); + } }
\ No newline at end of file |