-
Notifications
You must be signed in to change notification settings - Fork 1
/
filler_test.go
124 lines (100 loc) · 2.21 KB
/
filler_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package gatsby
import "github.com/c9s/gatsby/sqlutils"
import "testing"
import _ "github.com/c9s/pq"
import "time"
import "strings"
func TestFill(t *testing.T) {
var db = openDB()
stmt, _ := db.Prepare("select created_on from staffs")
rows, _ := stmt.Query()
for rows.Next() {
time1 := new(time.Time)
rows.Scan(time1)
t.Logf("Created on: %d", time1.Unix())
}
}
func TestCreateMapFromRows(t *testing.T) {
staff := Staff{}
var db = openDB()
// Create Staff
staff.Id = 1
staff.Name = "Mary"
staff.Phone = "1234567"
t1 := time.Now()
staff.CreatedOn = &t1
r := Create(db, &staff, DriverPg)
if r.Error != nil {
t.Fatal(r.Error)
}
if r.Id == -1 {
t.Fatal("Primary key failed")
}
staff.Id = r.Id
rows, _ := db.Query("select id, name from staffs")
rows.Next()
result, err := CreateMapFromRows(rows, new(int64), new(string))
if err != nil {
t.Fatal(err)
}
if _, ok := result["id"]; !ok {
t.Fatal("Can not read id")
}
if _, ok := result["name"]; !ok {
t.Fatal("Can not read name")
}
t.Log("Map", result)
results, err := CreateMapsFromRows(rows, new(int64), new(string))
if err != nil {
t.Fatal(err)
}
for _, r := range results {
if _, ok := r["id"]; !ok {
t.Fatal("Can not read id")
}
if _, ok := r["name"]; !ok {
t.Fatal("Can not read name")
}
}
Delete(db, &staff)
}
func TestFillRecord(t *testing.T) {
staff := Staff{}
var db = openDB()
// Create Staff
staff.Id = 1
staff.Name = "Mary"
staff.Phone = "1234567"
t1 := time.Now()
staff.CreatedOn = &t1
r := Create(db, &staff, DriverPg)
if r.Error != nil {
t.Fatal(r.Error)
}
if r.Id == -1 {
t.Fatal("Primary key failed")
}
staff.Id = r.Id
sql := sqlutils.BuildSelectClause(&staff) + " WHERE id = $1"
if !strings.Contains(sql, "id, name, gender, staff_type, phone, birthday, created_on") {
t.Fatal("Unexpected SQL: " + sql)
}
if !strings.Contains(sql, "FROM staffs WHERE id = $1") {
t.Fatal("Unexpected SQL: " + sql)
}
stmt, err := db.Prepare(sql)
rows, err := stmt.Query(r.Id)
if rows.Next() {
err = FillFromRows(&staff, rows, DriverPg)
if err != nil {
t.Fatal(err)
}
} else {
t.Fatal("No record found.")
}
r = Delete(db, &staff)
t.Log(r)
if r.Error != nil {
t.Fatal(r.Error)
}
}