-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
181 lines (144 loc) · 4.04 KB
/
client_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package dbx
import (
"fmt"
"testing"
"time"
"github.com/google/uuid"
)
func Test_Client_ExecStatement(t *testing.T) {
db, err := getSqlxDb()
defer db.Close()
if err != nil {
t.Errorf("Fatal create db error: %s", err.Error())
return
}
client := NewClient(db)
var newID = uuid.New()
//var newContext = context.Background()
var sql = "INSERT INTO person (id, name, created_at, updated_at) VALUES (:id, :name, :created_at, :updated_at)"
statement := NewStatement(sql)
statement.AddParameter("id", newID)
statement.AddParameter("name", "Andi Setiawan")
statement.AddParameter("created_at", time.Now())
statement.AddParameter("updated_at", nil)
result, err := client.ExecStatement(statement)
if err != nil {
t.Error("Error ", err.Error())
return
}
if result == nil {
t.Error("Error result nil")
return
}
fmt.Println("Insert into person using client success")
}
func Test_Client_QueryStatement(t *testing.T) {
db, err := getSqlxDb()
defer db.Close()
if err != nil {
t.Errorf("Fatal create db error: %s", err.Error())
return
}
client := NewClient(db)
var newID = uuid.New()
//var newContext = context.Background()
var insertSQL = "INSERT INTO person (id, name, created_at, updated_at) VALUES (:id, :name, :created_at, :updated_at)"
insertStatement := NewStatement(insertSQL)
insertStatement.AddParameter("id", newID)
insertStatement.AddParameter("name", "Dadang")
insertStatement.AddParameter("created_at", time.Now())
insertStatement.AddParameter("updated_at", nil)
result, err := client.ExecStatement(insertStatement)
if err != nil {
t.Error("Error ", err.Error())
return
}
if result == nil {
t.Error("Error result nil")
return
}
selectSQL := "SELECT * FROM person WHERE id=:id;"
selectStatement := NewStatement(selectSQL)
selectStatement.AddParameter("id", newID)
rows, err := client.QueryStatement(selectStatement)
if err != nil {
t.Error("Query process error ", err.Error())
return
}
defer rows.Close()
for rows.Next() {
fmt.Println("Select using client succedd")
return
}
t.Error("Query result must have records.")
}
func Test_Client_BeginTransaction(t *testing.T) {
db, err := getSqlxDb()
defer db.Close()
if err != nil {
t.Errorf("Fatal create db error: %s", err.Error())
}
dbClient := NewClient(db)
tx, err := dbClient.BeginTransaction()
if err != nil {
t.Errorf("Fatal begin transaction error: %s", err.Error())
}
if tx == nil {
t.Errorf("Transaction expected to be not nil")
}
if dbClient.transaction == nil {
t.Errorf("Fatal transaction error: transaction expected to be not <nil>")
}
dbClient.CompleteTransaction()
}
func Test_Client_GetTransaction(t *testing.T) {
db, err := getSqlxDb()
defer db.Close()
if err != nil {
t.Errorf("Fatal create db error: %s", err.Error())
}
dbClient := NewClient(db)
tx, err := dbClient.BeginTransaction()
if err != nil {
t.Errorf("Fatal begin transaction error: %s", err.Error())
}
if tx == nil {
t.Errorf("Fatal transaction error: transaction expected to be not <nil>")
}
if dbClient.GetTransaction() == nil {
t.Errorf("Fatal transaction error: transaction expected to be not <nil>")
}
fmt.Println("Test GetTransaction success")
}
func Test_Client_SetTransaction(t *testing.T) {
db, err := getSqlxDb()
defer db.Close()
if err != nil {
t.Errorf("Fatal create db error: %s", err.Error())
}
dbClient := NewClient(db)
tx, err := NewTransaction(db)
if err != nil {
t.Errorf("Fatal begin transaction error: %s", err.Error())
}
if tx == nil {
t.Errorf("Fatal transaction error: transaction expected to be not <nil>")
}
dbClient.SetTransaction(tx)
if dbClient.GetTransaction() == nil {
t.Errorf("Fatal transaction error: transaction expected to be not <nil>")
}
fmt.Println("Test SetTransaction success")
}
func Test_Client_NewClient(t *testing.T) {
db, err := getSqlxDb()
defer db.Close()
if err != nil {
t.Errorf("Fatal create db error: %s", err.Error())
}
dbClient := NewClient(db)
if dbClient.DB == nil {
t.Errorf("Fatal error: dbClient.DB expected to be not <nil>")
}
fmt.Println("Test NewClient success")
}