forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathmssql_go110_unit_test.go
More file actions
109 lines (91 loc) · 2.65 KB
/
mssql_go110_unit_test.go
File metadata and controls
109 lines (91 loc) · 2.65 KB
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
//go:build go1.10
// +build go1.10
package mssql
import (
"context"
"testing"
"github.com/microsoft/go-mssqldb/msdsn"
"github.com/stretchr/testify/assert"
)
func TestResult_LastInsertId(t *testing.T) {
r := &Result{}
id, err := r.LastInsertId()
assert.Error(t, err, "LastInsertId() should return an error")
assert.Equal(t, int64(-1), id, "LastInsertId() should return -1")
expectedMsg := "LastInsertId is not supported"
if err != nil {
assert.Contains(t, err.Error(), expectedMsg, "error message should contain expected text")
}
}
func TestConnector_Driver(t *testing.T) {
drv := &Driver{}
c := &Connector{
driver: drv,
}
result := c.Driver()
assert.Same(t, drv, result, "Driver() should return the same driver instance")
}
// TestRowsClose_SurfacesDoneStructErrors verifies that Rows.Close() returns
// errors from doneStruct tokens instead of silently discarding them.
// This is the unit test counterpart of the integration test
// TestIssue244_XactAbortErrorSurfaced.
func TestRowsClose_SurfacesDoneStructErrors(t *testing.T) {
sess := &tdsSession{}
conn := &Conn{
sess: sess,
connectionGood: true,
connector: &Connector{params: msdsn.Config{}},
}
stmt := &Stmt{c: conn}
ctx := context.Background()
tokChan := make(chan tokenStruct, 5)
reader := &tokenProcessor{
tokChan: tokChan,
ctx: ctx,
sess: sess,
}
rows := &Rows{
stmt: stmt,
reader: reader,
cancel: func() {},
}
// Simulate the token stream: a row followed by an error-bearing doneStruct,
// then close the channel to signal end of response.
serverErr := Error{Number: 245, Message: "Conversion failed"}
tokChan <- doneStruct{
Status: doneError,
errors: []Error{serverErr},
}
close(tokChan)
err := rows.Close()
assert.Error(t, err, "Close() should return an error from doneStruct")
assert.Contains(t, err.Error(), "Conversion failed")
}
// TestRowsClose_NoErrorWhenClean verifies that Rows.Close() returns nil
// when the token stream has no errors.
func TestRowsClose_NoErrorWhenClean(t *testing.T) {
sess := &tdsSession{}
conn := &Conn{
sess: sess,
connectionGood: true,
connector: &Connector{params: msdsn.Config{}},
}
stmt := &Stmt{c: conn}
ctx := context.Background()
tokChan := make(chan tokenStruct, 5)
reader := &tokenProcessor{
tokChan: tokChan,
ctx: ctx,
sess: sess,
}
rows := &Rows{
stmt: stmt,
reader: reader,
cancel: func() {},
}
// Simulate clean token stream: a done token with no error, then close.
tokChan <- doneStruct{Status: doneCount, RowCount: 1}
close(tokChan)
err := rows.Close()
assert.NoError(t, err, "Close() should return nil when no errors in token stream")
}