Example #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)
		}
	}
}