aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/edu/brown/cs/student/ProfitCalculationTest.java
blob: 0d22109921c6bcb7f2be6918f2dc3cfe7e7affeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package edu.brown.cs.student;

import edu.brown.cs.student.term.DatabaseQuerier;
import edu.brown.cs.student.term.hub.Holder;
import edu.brown.cs.student.term.hub.HubSearch;
import edu.brown.cs.student.term.hub.LinkMapper;
import edu.brown.cs.student.term.profit.ProfitCalculation;
import edu.brown.cs.student.term.profit.StockHolding;
import edu.brown.cs.student.term.repl.commands.SetupCommand;
import edu.brown.cs.student.term.trade.Trade;
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;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ProfitCalculationTest {

  /**
   * these should span the entire mock dataset
   */
  //12 am on 3/11 in UTC
  private Instant start = Instant.parse("2021-03-11T05:00:00.00Z");
  //12 am on 3/28 in UTC
  private Instant end = Instant.parse("2021-03-28T05:00:00.00Z");

  private DatabaseQuerier db;

  @Before
  public void setUp() {
    try {
      db = new DatabaseQuerier("data/profit_testing.sqlite3");
    } catch (Exception e) {
      System.out.println("DBQuerier Test, couldn't connect to db???");
    }
  }

  @After
  public void tearDown() {
    db = null;
  }

  @Test
  public void testBasicTrades() {
    setUp();
    ProfitCalculation profitCalculation =
        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();
    //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();
    assertTrue(trade.isEmpty());
    assertEquals(profitCalculation.calculateGainsSingle(), 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().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().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(), new LinkedList<>());

    setUp();
    //invalid person
    profitCalculation =
        new ProfitCalculation(DatabaseQuerier.getConn(), "1234", new Date(1518010558000l),
            new Date(1618698807000l));
    assertEquals(profitCalculation.getHoldingsList(), new LinkedList<>());


    //invalid stock ticker
    profitCalculation =
        new ProfitCalculation(DatabaseQuerier.getConn(), "invalidTicker", new Date(1518010558000l),
            new Date(1618698807000l));
    assertTrue(profitCalculation.getHoldingsList().isEmpty());
  }

}