-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgora_test.go
215 lines (177 loc) · 5.16 KB
/
gora_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package gora_test
import (
"fmt"
"strings"
"testing"
"time"
"github.com/eaciit/toolkit"
cv "github.com/smartystreets/goconvey/convey"
"git.eaciitapp.com/sebar/dbflex"
)
type DataModel struct {
ID string
Title string
DataInt int
DataDec float64
Created time.Time
}
var (
connectionString = "oracle://scbdc:Password@localhost:1521/orclpdb1"
tableTest = "TestTable"
tableModel = "TestModel"
)
func connect() (dbflex.IConnection, error) {
conn, err := dbflex.NewConnectionFromURI(connectionString, nil)
if err != nil {
return nil, fmt.Errorf("connection init error. %s", err.Error())
}
err = conn.Connect()
if err != nil {
return nil, fmt.Errorf("unable to connect %s. %s", connectionString, err.Error())
}
return conn, nil
}
func TestConnect(t *testing.T) {
cv.Convey("connect", t, func() {
conn, err := connect()
cv.So(err, cv.ShouldBeNil)
defer conn.Close()
})
}
func TestClearTable(t *testing.T) {
cv.Convey("delete data", t, func() {
c, _ := connect()
defer c.Close()
cmd := dbflex.From(tableModel).Delete()
_, err := c.Execute(cmd, nil)
cv.So(err, cv.ShouldBeNil)
})
}
var ndata = 5
func TestInsertData(t *testing.T) {
cv.Convey("save data", t, func() {
c, _ := connect()
defer c.Close()
cmd := dbflex.From(tableModel).Insert()
q, err := c.Prepare(cmd)
cv.So(err, cv.ShouldBeNil)
cv.Convey("process", func() {
es := []string{}
for i := 0; i < ndata; i++ {
model := new(DataModel)
model.ID = fmt.Sprintf("data-%d", i)
model.Title = fmt.Sprintf("Data title %d", i)
model.DataInt = toolkit.RandInt(50)
model.DataDec = toolkit.RandFloat(1000, 2)
model.Created = time.Now()
_, err := q.Execute(toolkit.M{}.Set("data", model))
if err != nil {
es = append(es, fmt.Sprintf("saving error %d. %s", i+1, err.Error()))
}
}
esTxt := strings.Join(es, "\n")
cv.So(esTxt, cv.ShouldEqual, "")
})
})
}
func TestQueryUsingModel(t *testing.T) {
cv.Convey("querying data", t, func() {
c, _ := connect()
defer c.Close()
cmd := dbflex.From(tableModel).Select()
cursor := c.Cursor(cmd, nil)
cv.So(cursor.Error(), cv.ShouldBeNil)
defer cursor.Close()
cv.Convey("validate result", func() {
results := []DataModel{}
err := cursor.Fetchs(&results, 0)
cv.So(err, cv.ShouldBeNil)
cv.So(len(results), cv.ShouldEqual, ndata)
fmt.Println("Results:\n", toolkit.JsonString(results))
})
})
}
func TestQueryUsingM(t *testing.T) {
cv.Convey("querying data", t, func() {
c, _ := connect()
defer c.Close()
cmd := dbflex.From(tableModel).Select()
cursor := c.Cursor(cmd, nil)
cv.So(cursor.Error(), cv.ShouldBeNil)
defer cursor.Close()
cv.Convey("validate result", func() {
results := []toolkit.M{}
err := cursor.Fetchs(&results, 0)
cv.So(err, cv.ShouldBeNil)
cv.So(len(results), cv.ShouldEqual, ndata)
fmt.Println("Results:\n", toolkit.JsonString(results))
})
})
}
func TestQueryFilter(t *testing.T) {
cv.Convey("querying", t, func() {
conn, _ := connect()
defer conn.Close()
cmd := dbflex.From(tableModel).Select().Where(dbflex.And(dbflex.Gte("ID", "data-2"), dbflex.Lte("ID", "data-4")))
cur := conn.Cursor(cmd, nil)
cv.So(cur.Error(), cv.ShouldBeNil)
cv.Convey("validate", func() {
ms := []toolkit.M{}
err := cur.Fetchs(&ms, 0)
defer cur.Close()
cv.So(err, cv.ShouldBeNil)
cv.So(len(ms), cv.ShouldEqual, 3)
})
})
}
func TestQuerySortTake(t *testing.T) {
cv.Convey("querying", t, func() {
conn, _ := connect()
defer conn.Close()
cmd := dbflex.From(tableModel).Select().OrderBy("-ID").Take(3)
cur := conn.Cursor(cmd, nil)
cv.So(cur.Error(), cv.ShouldBeNil)
cv.Convey("validate", func() {
ms := []toolkit.M{}
err := cur.Fetchs(&ms, 0)
defer cur.Close()
cv.So(err, cv.ShouldBeNil)
cv.So(len(ms), cv.ShouldEqual, 3)
cv.So(ms[2].GetString("ID"), cv.ShouldEqual, "data-2")
})
})
}
func TestQueryUpdate(t *testing.T) {
cv.Convey("querying data", t, func() {
c, _ := connect()
defer c.Close()
cmdSelect := dbflex.From(tableModel).Select().Where(dbflex.Eq("ID", "data-3"))
cursor := c.Cursor(cmdSelect, nil)
cv.So(cursor.Error(), cv.ShouldBeNil)
defer cursor.Close()
cv.Convey("update result", func() {
results := []toolkit.M{}
err := cursor.Fetchs(&results, 0)
cv.So(err, cv.ShouldBeNil)
cv.So(len(results), cv.ShouldEqual, 1)
dataInt := toolkit.RandInt(100) + 500
results[0]["DATAINT"] = dataInt
cmdUpdate := dbflex.From(tableModel).Update().Where(dbflex.Eq("ID", "data-3"))
_, err = c.Execute(cmdUpdate, toolkit.M{}.Set("data", results[0]))
cv.So(err, cv.ShouldBeNil)
cv.Convey("validate", func() {
cur2 := c.Cursor(cmdSelect, nil)
defer cur2.Close()
results2 := []toolkit.M{}
err = cur2.Fetchs(&results2, 0)
cv.So(err, cv.ShouldBeNil)
cv.So(len(results2), cv.ShouldEqual, 1)
cv.So(results2[0].GetInt("DATAINT"), cv.ShouldEqual, dataInt)
})
})
})
}
/*
INSERT INTO TestModel (ID,Title,DataInt,DataDec,Created) VALUES ('data-0','Data title 0',31,27.150000,to_date('2019-02-23 12:54:18','yyyy-mm-dd hh24:mi:ss'))
SELECT a.* FROM (SELECT tmp.* FROM (SELECT * FROM TestModel ORDER BY ID desc ) tmp WHERE ROWNUM < 3) a WHERE ROWNUM <= 1;
*/