This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNameTimeProjections_test.go
executable file
·95 lines (77 loc) · 2.52 KB
/
NameTimeProjections_test.go
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
package main
import (
"testing"
"time"
)
var TestNameTimeProjection *NameTimeProjection
// Create the testing projection we use
//
// This allows tests to run faster as anything using this projection
// can use the same copy
func setupNameTimeProjectionTest(t *testing.T) NameTimeProjection {
// Make sure the underlying db was created first
db := setupPriceTest(t)
if TestNameTimeProjection == nil {
proj := NameTimeProjectionFromPriceDB(db)
TestNameTimeProjection = &proj
}
return *TestNameTimeProjection
}
// Create a projection out of the fully formed db
//
// Postgres equivalent
// n/a
func TestNameTimeProjectionFromPriceDB(t *testing.T) {
// Ensure test DB and projection are setup
//
// Not strictly necessary but we use TestDB anyway
proj := setupNameTimeProjectionTest(t)
db := setupPriceTest(t)
// Ensure price length is equal
priceLengthDB := db.Prices.Length()
priceLengthProj := proj.Prices.Length()
if priceLengthDB != priceLengthProj {
t.Fatalf("mismatching projection and db lengths %v != %v", priceLengthDB, priceLengthProj)
}
// Ensure sort was successful in making time ascending
badTransitions := 0
var prevName string
var prevTime time.Time
for i := 0; i < priceLengthProj; i++ {
name := proj.Names.Access(i)
time := proj.Times.Access(i)
// Unequal names should have previous time AFTER current time
if name != prevName && prevTime.Before(time) {
// Dataset can be a little dirty due to unhinged and such
// so we keep track of how many times this happeened
// and threshold the test failure
badTransitions++
if badTransitions > 5 {
t.Fatalf("not asc sort order for index %v: {%v, %v} {%v, %v}",
i, prevName, prevTime, name, time)
}
}
// Equal names should have previous time BEFORE current time
if name == prevName && prevTime.After(time) {
t.Fatalf("not asc sort order for index %v: {%v, %v} {%v, %v}",
i, prevName, prevTime, name, time)
}
prevName = name
prevTime = time
}
}
// Find the number of tuples for a certain card's latest value
//
// Postgres equivalent(result # only, not performance equiv)
// SELECT * FROM prices.mtgprice
// WHERE name='Windswept Heath' AND
// time = timestamp '2016-04-09 03:51:45' ORDER BY time DESC, price DESC;
func TestNameTimeProjectionLatest(t *testing.T) {
proj := setupNameTimeProjectionTest(t)
referenceCount := 6
query := proj.Latest("Windswept Heath")
count := len(query.TruthyIndices())
if referenceCount != referenceCount {
t.Fatalf("mismatching coubt %v != %v", referenceCount, count)
}
}