-
Notifications
You must be signed in to change notification settings - Fork 4
/
dbora_test.go
219 lines (165 loc) · 3.85 KB
/
dbora_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
216
217
218
219
package xsql_test
import (
"database/sql"
"fmt"
"github.com/mix-go/xsql"
"github.com/sijms/go-ora/v2"
"github.com/stretchr/testify/assert"
"log"
"testing"
"time"
)
func newOracleDB() *xsql.DB {
db, err := sql.Open("oracle", "oracle://root:123456@127.0.0.1:1521/orcl")
if err != nil {
log.Fatal(err)
}
return xsql.New(
db,
xsql.WithDebugFunc(func(l *xsql.Log) {
log.Println(l)
}),
xsql.WithTimeLocation(time.UTC),
xsql.UseOracle(),
)
}
func TestOracleClear(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
_, err := DB.Exec("DELETE FROM XSQL WHERE ID > 2")
a.Empty(err)
}
func TestOracleQuery(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
rows, err := DB.Query("SELECT * FROM XSQL WHERE ROWNUM <= 2")
if err != nil {
log.Fatal(err)
}
bar := rows[0].Get("BAR").Time().Format(xsql.DefaultOptions.TimeLayout)
fmt.Println(bar)
a.Equal(bar, "2022-04-14 23:49:48")
}
type TestOracle struct {
Id int `xsql:"ID"`
Foo string `xsql:"FOO"`
Bar go_ora.TimeStamp `xsql:"BAR"`
}
func (t TestOracle) TableName() string {
return "XSQL"
}
func TestOracleInsert(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
test := TestOracle{
Id: 3,
Foo: "test",
Bar: go_ora.TimeStamp(time.Now()),
}
_, err := DB.Insert(&test)
a.Empty(err)
}
// oracle 不支持批量插入
func __TestOracleBatchInsert(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
tests := []TestOracle{
{
Id: 4,
Foo: "test1",
Bar: go_ora.TimeStamp(time.Now()),
},
{
Id: 5,
Foo: "test2",
Bar: go_ora.TimeStamp(time.Now()),
},
}
_, err := DB.BatchInsert(&tests)
a.Empty(err)
}
func TestOracleUpdate(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
test := TestOracle{
Id: 999,
Foo: "test update",
Bar: go_ora.TimeStamp(time.Now()),
}
_, err := DB.Update(&test, "id = :id", 3)
a.Empty(err)
}
func TestOracleExec(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
_, err := DB.Exec("DELETE FROM XSQL WHERE ID = :id", 999)
a.Empty(err)
}
func TestOracleFirst(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
var test TestOracle
err := DB.First(&test, "SELECT * FROM XSQL")
if err != nil {
log.Fatal(err)
}
a.Equal(fmt.Sprintf("%+v", test), "{Id:1 Foo:v Bar:{wall:0 ext:63785576988 loc:<nil>}}")
}
func TestOracleFirstPart(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
var test TestOracle
err := DB.First(&test, "SELECT foo FROM XSQL")
if err != nil {
log.Fatal(err)
}
a.Equal(fmt.Sprintf("%+v", test), "{Id:0 Foo:v Bar:{wall:0 ext:0 loc:<nil>}}")
}
func TestOracleFind(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
var tests []TestOracle
err := DB.Find(&tests, "SELECT * FROM XSQL WHERE ROWNUM <= 2")
if err != nil {
log.Fatal(err)
}
a.Equal(fmt.Sprintf("%+v", tests), `[{Id:1 Foo:v Bar:{wall:0 ext:63785576988 loc:<nil>}} {Id:2 Foo:v1 Bar:{wall:0 ext:63785577000 loc:<nil>}}]`)
}
func TestOracleFindPart(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
var tests []TestOracle
err := DB.Find(&tests, "SELECT foo FROM XSQL WHERE ROWNUM <= 2")
if err != nil {
log.Fatal(err)
}
a.Equal(fmt.Sprintf("%+v", tests), `[{Id:0 Foo:v Bar:{wall:0 ext:0 loc:<nil>}} {Id:0 Foo:v1 Bar:{wall:0 ext:0 loc:<nil>}}]`)
}
func TestOracleTxCommit(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
tx, _ := DB.Begin()
test := TestOracle{
Id: 998, // oracle not support AUTO_INCREMENT
Foo: "test",
Bar: go_ora.TimeStamp(time.Now()),
}
_, err := tx.Insert(&test)
a.Empty(err)
err = tx.Commit()
a.Empty(err)
}
func TestOracleTxRollback(t *testing.T) {
a := assert.New(t)
DB := newOracleDB()
tx, _ := DB.Begin()
test := TestOracle{
Id: 999, // oracle not support AUTO_INCREMENT
Foo: "test",
Bar: go_ora.TimeStamp(time.Now()),
}
_, err := tx.Insert(&test)
a.Empty(err)
err = tx.Rollback()
a.Empty(err)
}