aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/edu/brown/cs/student/DBQuerierTest.java
blob: d8139695ae3bcd3d9bd1a243664e745641dfb11e (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
package edu.brown.cs.student;

import java.io.PrintStream;
import java.sql.SQLException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import edu.brown.cs.student.term.DatabaseQuerier;
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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

//TODO: Write more tests for methods besides stock by name
public class DBQuerierTest {

  /** 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/mock_trades.sqlite3");
    } catch(Exception e){
      System.out.println("DBQuerier Test, couldn't connect to db???");
    }
  }

  /*
  * try{

    } catch(Exception e) {
      System.out.println("Error in test");
    }*/

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

  @Test
  public void testNonExistentStock(){
    setUp();
    try{
      List<Trade> fakeStockList = db.getTradeByStock("NONO", 1, start, end);
      assertTrue(fakeStockList.isEmpty());

    } catch(Exception e) {
      System.out.println("Error in test");
    }
    tearDown();
  }

  @Test
  public void testFlippedDates(){
    setUp();
    try{
      List<Trade> gmeBadDatesList = db.getTradeByStock("GME", 1, end, start);
      assertTrue(gmeBadDatesList.isEmpty());

    } catch(Exception e) {
      System.out.println("Error in test");
    }
    tearDown();
  }

  @Test
  public void testTradeByStockNameBuy(){
    setUp();
    try{
      List<Trade> gmeBuyList = db.getTradeByStock("GME", 1, start, end);
      System.out.println(gmeBuyList);
      assertEquals(gmeBuyList.size(), 6);
      assertEquals(gmeBuyList.get(0).getId(), 482);
      assertEquals(gmeBuyList.get(3).getId(), 149);
      assertEquals(gmeBuyList.get(4).getId(), 275);
      assertEquals(gmeBuyList.get(5).getId(), 30);


      List<Trade> teslaBuyList = db.getTradeByStock("TSLA", 1, start, end);
      assertEquals(teslaBuyList.size(), 16);
      assertEquals(teslaBuyList.get(0).getId(), 328);
      assertEquals(teslaBuyList.get(7).getId(), 241);
      assertEquals(teslaBuyList.get(15).getId(), 774);

    } catch(Exception e) {
      System.out.println("Error in testTradeByStockName");
    }

    tearDown();
  }

  @Test
  public void testTradeByNameOrdering(){
    setUp();

    try{
      List<Trade> gmeSellList = db.getTradeByStock("GME", 0, start, end);
      for(int i = 1; i < gmeSellList.size(); i++){
        assertTrue(gmeSellList.get(i-1).getTimestamp() < gmeSellList.get(i).getTimestamp());
      }

      List<Trade> amznBuyList = db.getTradeByStock("AMZN", 1, start, end);
      for(int i = 1; i < amznBuyList.size(); i++){
        assertTrue(amznBuyList.get(i-1).getTimestamp() < amznBuyList.get(i).getTimestamp());
      }

    } catch(Exception e) {
      System.out.println("Error in test");
    }
    tearDown();
  }
}