-
Notifications
You must be signed in to change notification settings - Fork 48
/
mssql_test.go
242 lines (216 loc) · 6 KB
/
mssql_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package freetds
import (
"database/sql"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"os"
)
func open(t *testing.T) (*sql.DB, error, bool) {
connStr := os.Getenv("GOFREETDS_CONN_STR")
conn, err := NewConn(connStr)
//if()
if err != nil {
t.Error(err)
}
db, err := sql.Open("mssql", testDbConnStr(1))
return db, err, conn.sybaseMode125()
}
func TestGoSqlOpenConnection(t *testing.T) {
db, err, _ := open(t)
if err != nil || db == nil {
t.Error(err)
}
}
func TestMssqlConnOpen(t *testing.T) {
d := &MssqlDriver{}
c, err := d.Open(testDbConnStr(1))
assert.Nil(t, err)
assert.IsType(t, &MssqlConn{}, c)
c.Close()
}
func TestMssqlConnOpenSybase125(t *testing.T) {
d := &MssqlDriver{}
c, err := d.Open(testDbConnStrSybase125(1))
assert.Nil(t, err)
assert.IsType(t, &MssqlConn{}, c)
c.Close()
}
func TestGoSqlDbQueryRow(t *testing.T) {
db, err, _ := open(t)
defer db.Close()
assert.Nil(t, err)
row := db.QueryRow("SELECT au_fname, au_lname name FROM authors WHERE au_id = ?", "172-32-1176")
var firstName, lastName string
err = row.Scan(&firstName, &lastName)
assert.Nil(t, err)
assert.Equal(t, firstName, "Johnson")
assert.Equal(t, lastName, "White")
}
func TestGoSqlDbQuery(t *testing.T) {
db, err, _ := open(t)
defer db.Close()
assert.Nil(t, err)
rows, err := db.Query("SELECT au_fname, au_lname name FROM authors WHERE au_lname = ? order by au_id", "Ringer")
assert.Nil(t, err)
testRingers(t, rows)
}
func testRingers(t *testing.T, rows *sql.Rows) {
var firstName, lastName string
rows.Next()
err := rows.Scan(&firstName, &lastName)
assert.Nil(t, err)
assert.Equal(t, firstName, "Anne")
rows.Next()
err = rows.Scan(&firstName, &lastName)
assert.Nil(t, err)
assert.Equal(t, firstName, "Albert")
}
func TestGoSqlPrepareQuery(t *testing.T) {
//t.Skip()
db, err, _ := open(t)
defer db.Close()
assert.Nil(t, err)
assert.NotNil(t, db)
stmt, err := db.Prepare("SELECT au_fname, au_lname name FROM authors WHERE au_lname = ? order by au_id")
assert.Nil(t, err)
rows, err := stmt.Query("Ringer")
assert.Nil(t, err)
testRingers(t, rows)
}
func TestLastInsertIdRowsAffected(t *testing.T) {
db, _, sybase125 := open(t)
defer db.Close()
if sybase125 {
t.Skip("LastInsertId and RowsEffective not returned in Sybase 12.5")
}
createTestTable(t, db, sybase125,"test_last_insert_id", "")
r, err := db.Exec("insert into [test_last_insert_id] values(?)", "pero")
assert.Nil(t, err)
assert.NotNil(t, r)
id, err := r.LastInsertId()
assert.Nil(t, err)
assert.EqualValues(t, id, 1)
ra, err := r.RowsAffected()
assert.Nil(t, err)
assert.EqualValues(t, ra, 1)
r, err = db.Exec("insert into test_last_insert_id values(?)", "pero")
assert.Nil(t, err)
assert.NotNil(t, r)
id, err = r.LastInsertId()
assert.Nil(t, err)
assert.EqualValues(t, id, 2)
ra, err = r.RowsAffected()
assert.Nil(t, err)
assert.EqualValues(t, ra, 1)
r, err = db.Exec("update test_last_insert_id set name = ?", "jozo")
assert.Nil(t, err)
assert.NotNil(t, r)
id, err = r.LastInsertId()
assert.NotNil(t, err)
ra, err = r.RowsAffected()
assert.Nil(t, err)
assert.EqualValues(t, ra, 2)
r, err = db.Exec("delete from test_last_insert_id")
assert.Nil(t, err)
ra, err = r.RowsAffected()
assert.Nil(t, err)
assert.EqualValues(t, ra, 2)
}
func createTestTable(t *testing.T, db *sql.DB, sybase125 bool, name string, columDef string) {
if columDef == "" {
columDef = "id int not null identity, name varchar(255)"
if sybase125 {
columDef = "id int identity not null, name varchar(255)"
}
}
sql := fmt.Sprintf(`
if exists(select * from sys.tables where name = 'table_name')
drop table table_name
;
create table table_name (
%s
)
`, columDef)
if sybase125 {
sql = `
if exists(select name from sysobjects where name = "table_name")
drop table table_name
`
sql = strings.Replace(sql, "table_name", name, 2)
result, err := db.Exec(sql)
result = result
assert.Nil(t, err)
sql = fmt.Sprintf(`
create table [table_name] (
%s
)
`, columDef)
}
sql = strings.Replace(sql, "table_name", name, 3)
_, err := db.Exec(sql)
assert.Nil(t, err)
}
func TestTransCommit(t *testing.T) {
db, _, sybase125 := open(t)
defer db.Close()
createTestTable(t, db, sybase125, "test_tran", "")
tx, err := db.Begin()
assert.Nil(t, err)
tx.Exec("insert into test_tran values(?)", "pero")
tx.Exec("insert into test_tran values(?)", "jozo")
err = tx.Commit()
assert.Nil(t, err)
row := db.QueryRow("select count(*) from test_tran")
assert.Nil(t, err)
var count int
err = row.Scan(&count)
assert.Nil(t, err)
assert.Equal(t, count, 2)
}
func TestTransRollback(t *testing.T) {
db, _, sybase125 := open(t)
defer db.Close()
createTestTable(t, db, sybase125, "test_tran", "")
tx, err := db.Begin()
assert.Nil(t, err)
tx.Exec("insert into test_tran values(?)", "pero")
tx.Exec("insert into test_tran values(?)", "jozo")
err = tx.Rollback()
assert.Nil(t, err)
row := db.QueryRow("select count(*) from test_tran")
assert.Nil(t, err)
var count int
err = row.Scan(&count)
assert.Nil(t, err)
assert.Equal(t, count, 0)
}
func TestBlobs(t *testing.T) {
db, _, sybase125 := open(t)
defer db.Close()
columnDef := "id int identity, blob varbinary(16), blob2 varbinary(MAX)"
if sybase125 {
columnDef = "id int identity, blob image, blob2 image"
//t.Skip("Blobs not supported in Sybase 12.5")
}
createTestTable(t, db, sybase125, "test_blobs", columnDef)
want := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
_, err := db.Exec("insert into test_blobs values(?, ?)", want, want)
assert.Nil(t, err)
var got []byte
err = db.QueryRow("select blob from test_blobs").Scan(&got)
assert.Nil(t, err)
assert.Equal(t, 16, len(got))
strWant := fmt.Sprintf("%x", want)
strGot := fmt.Sprintf("%x", got)
assert.Equal(t, strWant, strGot)
assert.Equal(t, want, got)
err = db.QueryRow("select blob2 from test_blobs").Scan(&got)
assert.Nil(t, err)
assert.Equal(t, 16, len(got))
strWant = fmt.Sprintf("%x", want)
strGot = fmt.Sprintf("%x", got)
assert.Equal(t, strWant, strGot)
assert.Equal(t, want, got)
}