forked from minus5/gofreetds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql_test.go
180 lines (161 loc) · 4.49 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
package freetds
import (
"database/sql"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func open() (*sql.DB, error) {
return sql.Open("mssql", testDbConnStr(1))
}
func TestGoSqlOpenConnection(t *testing.T) {
db, err := open()
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 TestGoSqlDbQueryRow(t *testing.T) {
db, err := open()
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()
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()
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, _ := open()
createTestTable(t, db, "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, name string, columDef string) {
if columDef == "" {
columDef = "id int not null identity, 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)
sql = strings.Replace(sql, "table_name", name, 3)
_, err := db.Exec(sql)
assert.Nil(t, err)
}
func TestTransCommit(t *testing.T) {
db, _ := open()
createTestTable(t, db, "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, _ := open()
createTestTable(t, db, "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, _ := open()
createTestTable(t, db, "test_blobs", "id int identity, blob varbinary(16)")
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)
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)
}