MCPcopy
hub / github.com/sourcegraph/checkup / TestSQL

Function TestSQL

storage/sql/sql_test.go:15–100  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestSQL(t *testing.T) {
16 results := []types.Result{{Title: "Testing"}}
17
18 // Create temporary directory for the tests
19 dir, err := ioutil.TempDir("", "checkup")
20 if err != nil {
21 t.Fatalf("Cannot create temporary directory: %v", err)
22 }
23 defer os.RemoveAll(dir)
24
25 dbFile := filepath.Join(dir, "checkuptest.db")
26
27 specimen := Storage{
28 SqliteDBFile: dbFile,
29 }
30
31 if err := specimen.initialize(); err != nil {
32 t.Fatalf("Could not initialize test database, got: %v", err)
33 }
34
35 if err := specimen.Store(results); err != nil {
36 t.Fatalf("Expected no error from Store(), got: %v", err)
37 }
38
39 // Test GetIndex (StorageReader interface)
40 index, err := specimen.GetIndex()
41 if err != nil {
42 t.Fatalf("StoreReader: cannot read index: %v", err)
43 }
44
45 if len(index) != 1 {
46 t.Fatalf("Expected length of index to be 1, but got %v", len(index))
47 }
48
49 var (
50 name string
51 nsec int64
52 )
53 for name, nsec = range index {
54 }
55
56 // Make sure index has timestamp of check
57 ts := time.Unix(0, nsec)
58 if time.Since(ts) > 1*time.Second {
59 t.Errorf("Timestamp of check is %s but expected something very recent", ts)
60 }
61
62 // Make sure stored data are correct
63 testResults, err := specimen.Fetch(name)
64 if err != nil {
65 t.Fatalf("Could not fetch data, got: %v", err)
66 }
67 if len(testResults) != 1 {
68 t.Fatalf("StoreReader: expected length of []Result to be 1, but got %v", len(testResults))
69 }
70
71 if testResults[0].Title != results[0].Title {
72 t.Fatalf("Expected test result title to be '%s', but got '%s'", results[0].Title, testResults[0].Title)

Callers

nothing calls this directly

Calls 5

initializeMethod · 0.95
StoreMethod · 0.95
GetIndexMethod · 0.95
FetchMethod · 0.95
MaintainMethod · 0.95

Tested by

no test coverage detected