Beispiel #1
0
func checkMemoryAndDatabase(st *stock.Stock, sp *stock.Span, tdb *stock.StockDB, t *testing.T) {
	selectSchema := `SELECT Time, Value FROM Measures where Symbol = $1`

	if len(*sp) == 0 {
		// sp is empty, we should expect the stock to be unpopulated

		// Confirm memory is missing this stock
		if len(st.Span) > 0 {
			t.Errorf("Expected empty stock.Span but had memoized data, stock.Span:\n%+v\n", st.Span)
		}
		// Confirm database does not include data for this stock
		r, e := tdb.Queryx(selectSchema, st.Symbol)
		if e != nil {
			t.Error(e)
		} else {
			// we'll get rows if the table exists, but won't be able to access any data
			hasNext := r.Next()
			if hasNext {
				// if we have data something is wrong
				t.Errorf("Expected nothing in database, but found data for stock.Symbol: %s\n", st.Symbol)
			}
		}
	} else {
		// sp is the expected results
		if !st.Span.Equal(*sp) {
			t.Errorf("Expected span to be memoized. Expected:\n%+v\n Got:\n%+v\n", st.Span, sp)
		}

		r, e := tdb.Queryx(selectSchema, st.Symbol)
		if e != nil {
			t.Error(e)
		}
		dbSpan := *new(stock.Span)
		for r.Next() {
			m := new(stock.Measure)
			e = r.StructScan(m)
			if e != nil {
				t.Error(e)
			}
			dbSpan = append(dbSpan, *m)
		}

		// sp is the expected results
		if !st.Span.Equal(dbSpan) {
			t.Errorf("Expected span to be in the database. Expected:\n%+v\n Got:\n%+v\n", st.Span, dbSpan)
		}
	}
}
Beispiel #2
0
func (td *TearDown) TearDown(tdb *stock.StockDB, t *testing.T) error {
	sort.Sort(td) // sort actions so that CREATE is reversed last
	for _, change := range *td {
		var err error = nil
		switch change.Action {
		case CREATE:
			// reverse of table creation is dropping the table
			exec := strings.Join([]string{"DROP TABLE ", change.Table}, "")
			_, err = tdb.Exec(exec)
		case INSERT:
			// reverse of insert is delete, symbol and time is key
			exec := strings.Join([]string{"DELETE FROM ", change.Table, " WHERE Symbol = $1 AND Time = $2"}, "")
			_, err = tdb.Exec(exec, change.Key.Symbol, stock.TimeForSQL(change.Key.Date))
		}
		if err != nil {
			return err
		}
	}
	return nil
}