forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_test.go
96 lines (80 loc) · 2.51 KB
/
sql_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
96
package checkup
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func TestSQL(t *testing.T) {
results := []Result{{Title: "Testing"}}
// Create temporary directory for the tests
dir, err := ioutil.TempDir("", "checkup")
if err != nil {
t.Fatalf("Cannot create temporary directory: %v", err)
}
defer os.RemoveAll(dir)
dbFile := filepath.Join(dir, "checkuptest.db")
specimen := SQL{
SqliteDBFile: dbFile,
}
if err := specimen.initialize(); err != nil {
t.Fatalf("Could not initialize test database, got: %v", err)
}
if err := specimen.Store(results); err != nil {
t.Fatalf("Expected no error from Store(), got: %v", err)
}
// Test GetIndex (StorageReader interface)
index, err := specimen.GetIndex()
if err != nil {
t.Fatalf("StoreReader: cannot read index: %v", err)
}
if len(index) != 1 {
t.Fatalf("Expected length of index to be 1, but got %v", len(index))
}
var (
name string
nsec int64
)
for name, nsec = range index {
}
// Make sure index has timestamp of check
ts := time.Unix(0, nsec)
if time.Since(ts) > 1*time.Second {
t.Errorf("Timestamp of check is %s but expected something very recent", ts)
}
// Make sure stored data are correct
testResults, err := specimen.Fetch(name)
if err != nil {
t.Fatalf("Could not fetch data, got: %v", err)
}
if len(testResults) != 1 {
t.Fatalf("StoreReader: expected length of []Result to be 1, but got %v", len(testResults))
}
if testResults[0].Title != results[0].Title {
t.Fatalf("Expected test result title to be '%s', but got '%s'", results[0].Title, testResults[0].Title)
}
// Make sure the check is not deleted after maintain with CheckExpiry == 0
if err := specimen.Maintain(); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if _, err := specimen.Fetch(name); err != nil {
t.Fatalf("Expected the check to be present in the DB, got: %v", err)
}
// Make sure the check is not deleted after maintain with CheckExpiry == 1 day
specimen.CheckExpiry = 24 * time.Hour
if err := specimen.Maintain(); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if _, err := specimen.Fetch(name); err != nil {
t.Fatalf("Expected the check to be present in the DB, got: %v", err)
}
// Make sure the check is deleted after maintain with CheckExpiry > 0
specimen.CheckExpiry = 1 * time.Nanosecond
if err := specimen.Maintain(); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if _, err := specimen.Fetch(name); err == nil {
t.Fatalf("Expected not to be able to fetch the result from the DB")
}
}