-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_test.go
87 lines (72 loc) · 1.64 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
package harper
import (
"fmt"
"testing"
)
func TestSQL(t *testing.T) {
schema := randomID()
table := randomID()
if err := c.CreateSchema(schema); err != nil {
t.Fatal(err)
}
wait()
if err := c.CreateTable(schema, table, "id"); err != nil {
t.Log(err)
t.FailNow()
}
wait()
record := aRecord{
ID: randomID(),
Name: "Harper, the dog",
}
if _, err := c.Insert(schema, table, []interface{}{
record,
}); err != nil {
t.Fatal(err)
}
var rows []aRecord
if err := c.SQLSelect(&rows, "select * from `%s`.`%s`", schema, table); err != nil {
t.Fatal(err)
}
if found := len(rows); !(found > 0 && found <= 1) {
t.Fatal(fmt.Errorf("expected one row from select"))
}
if rows[0].Name != "Harper, the dog" {
t.Fatal(fmt.Errorf("returned row has different data"))
}
newName := "Harper, the wolf"
resp, err := c.SQLExec("update `%s`.`%s` set name = '%s' where id = '%s'", schema, table, newName, rows[0].ID)
if err != nil {
t.Fatal(err)
}
if len(resp.UpdatedHashes) != 1 {
t.Log(fmt.Errorf("expected one updated row"))
t.Fail()
}
// TODO Did it really update the name?
resp, err = c.SQLExec("DELETE FROM `%s`.`%s` where id = '%s'", schema, table, rows[0].ID)
if err != nil {
t.Fatal(err)
}
if len(resp.DeletedHashes) != 1 {
t.Fatal(fmt.Errorf("expected 1 deleted hash"))
}
// TODO Fetch record and check if delete was done
if err := c.DropSchema(schema); err != nil {
t.Fatal(err)
}
}
func TestSQLGet(t *testing.T) {
if ts, err := c.SQLGet("SELECT CURRENT_TIMESTAMP"); err == nil {
t.Log(ts)
if val, ok := ts.(float64); ok {
t.Log(val)
if val > 0 {
return
}
}
} else {
t.Log(err)
}
t.Fail()
}