forked from microsoft/go-sqlcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_test.go
More file actions
259 lines (225 loc) · 7.74 KB
/
Copy pathformat_test.go
File metadata and controls
259 lines (225 loc) · 7.74 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package sqlcmd
import (
"context"
"strings"
"testing"
mssql "github.com/microsoft/go-mssqldb"
"github.com/microsoft/go-sqlcmd/internal/color"
"github.com/stretchr/testify/assert"
)
func TestFitToScreen(t *testing.T) {
type fitTest struct {
width int64
raw string
fit string
}
tests := []fitTest{
{0, "this is a string", "this is a string"},
{9, "12345678", "12345678"},
{9, "123456789", "123456789"},
{9, "123456789A", "123456789" + SqlcmdEol + "A"},
{9, "123456789" + SqlcmdEol, "123456789" + SqlcmdEol},
{9, "12345678" + SqlcmdEol + "9A", "12345678" + SqlcmdEol + "9A"},
{9, "123456789\rA", "123456789" + SqlcmdEol + "\rA"},
}
for _, test := range tests {
line := new(strings.Builder)
line.WriteString(test.raw)
t.Log(test.raw)
f := fitToScreen(line, test.width).String()
assert.Equal(t, test.fit, f, "Mismatched fit for raw string: '%s'", test.raw)
}
}
func TestCalcColumnDetails(t *testing.T) {
type colTest struct {
fixed int64
variable int64
query string
details []columnDetail
max int
}
tests := []colTest{
{8, 8,
"select 100 as '123456789ABC', getdate() as '987654321', 'string' as col1",
[]columnDetail{
{leftJustify: false, displayWidth: 12},
{leftJustify: false, displayWidth: 23},
{leftJustify: true, displayWidth: 6},
},
12,
},
}
db, err := ConnectDb(t)
if assert.NoError(t, err, "ConnectDB failed") {
defer db.Close()
for x, test := range tests {
rows, err := db.QueryContext(context.Background(), test.query)
if assert.NoError(t, err, "Query failed: %s", test.query) {
defer rows.Close()
cols, err := rows.ColumnTypes()
if assert.NoError(t, err, "ColumnTypes failed:%s", test.query) {
actual, max := calcColumnDetails(cols, test.fixed, test.variable)
for i, a := range actual {
if test.details[i].displayWidth != a.displayWidth ||
test.details[i].leftJustify != a.leftJustify ||
test.details[i].zeroesAfterDecimal != a.zeroesAfterDecimal {
assert.Failf(t, "", "[%d] Incorrect test details for column [%s] in query '%s':%+v", x, cols[i].Name(), test.query, a)
}
assert.Equal(t, test.max, max, "[%d] Max column name length incorrect", x)
}
}
}
}
}
}
func TestControlCharacterBehavior(t *testing.T) {
type ccbTest struct {
raw string
replaced string
removed string
consecutivereplaced string
}
tests := []ccbTest{
{"no control", "no control", "no control", "no control"},
{string(rune(1)) + "tabs\t\treturns\r\n\r\n", " tabs returns ", "tabsreturns", " tabs returns "},
}
for _, test := range tests {
s := applyControlCharacterBehavior(test.raw, ControlReplace)
assert.Equalf(t, test.replaced, s, "Incorrect Replaced for '%s'", test.raw)
s = applyControlCharacterBehavior(test.raw, ControlRemove)
assert.Equalf(t, test.removed, s, "Incorrect Remove for '%s'", test.raw)
s = applyControlCharacterBehavior(test.raw, ControlReplaceConsecutive)
assert.Equalf(t, test.consecutivereplaced, s, "Incorrect ReplaceConsecutive for '%s'", test.raw)
}
}
func TestDecodeBinary(t *testing.T) {
type decodeTest struct {
b []byte
s string
}
tests := []decodeTest{
{[]byte("123456ABCDEF"), "313233343536414243444546"},
{[]byte{0x12, 0x34, 0x56}, "123456"},
}
for _, test := range tests {
a := decodeBinary(test.b)
assert.Equalf(t, test.s, a, "Incorrect decoded binary string for %v", test.b)
}
}
func BenchmarkDecodeBinary(b *testing.B) {
b.ReportAllocs()
bytes := make([]byte, 10000)
for i := 0; i < 10000; i++ {
bytes[i] = byte(i % 0xff)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := decodeBinary(bytes)
if len(s) != 20000 {
b.Fatalf("Incorrect length of returned string. Should be 20k, was %d", len(s))
}
}
}
func TestFormatterColorizer(t *testing.T) {
s, buf := setupSqlCmdWithMemoryOutput(t)
defer buf.Close()
s.vars.Set(SQLCMDCOLORSCHEME, "emacs")
s.Format.(*sqlCmdFormatterType).colorizer = color.New(true)
err := runSqlCmd(t, s, []string{"select 'name' as name", "GO"})
assert.NoError(t, err, "runSqlCmd returned error")
output := buf.buf.String()
// Verify the colorized output contains ANSI escape codes and expected content
assert.Contains(t, output, "\x1b[", "output should contain ANSI escape codes")
assert.Contains(t, output, "name", "output should contain column value")
assert.Contains(t, output, "(1 row affected)", "output should contain row count")
}
func TestFormatterXmlMode(t *testing.T) {
s, buf := setupSqlCmdWithMemoryOutput(t)
defer buf.Close()
s.Format.XmlMode(true)
err := runSqlCmd(t, s, []string{"select name from sys.databases where name='master' for xml auto ", "GO"})
assert.NoError(t, err, "runSqlCmd returned error")
assert.Equal(t, `<sys.databases name="master"/>`+SqlcmdEol, buf.buf.String())
}
func TestFormatterRawErrors(t *testing.T) {
vars := InitializeVariables(false)
errBuf := new(strings.Builder)
f := NewSQLCmdDefaultFormatter(false, ControlIgnore, false).(*sqlCmdFormatterType)
f.BeginBatch("", vars, new(strings.Builder), errBuf)
testErr := mssql.Error{
Number: 208,
Class: 16,
State: 1,
ServerName: "testserver",
Message: "Invalid object name 'nonexistent'.",
}
f.AddError(testErr)
normalOutput := errBuf.String()
// Normal mode should include the Msg header
assert.Contains(t, normalOutput, "Msg 208")
assert.Contains(t, normalOutput, "Level 16")
assert.Contains(t, normalOutput, "State 1")
assert.Contains(t, normalOutput, "Invalid object name 'nonexistent'.")
// Raw mode
errBuf.Reset()
f = NewSQLCmdDefaultFormatter(false, ControlIgnore, true).(*sqlCmdFormatterType)
f.BeginBatch("", vars, new(strings.Builder), errBuf)
f.AddError(testErr)
rawOutput := errBuf.String()
// Raw mode should NOT include the Msg header
assert.NotContains(t, rawOutput, "Msg 208")
assert.NotContains(t, rawOutput, "Level 16")
assert.NotContains(t, rawOutput, "State 1")
// But should still contain the actual error message
assert.Contains(t, rawOutput, "Invalid object name 'nonexistent'.")
}
func TestFormatterErrorWithProcName(t *testing.T) {
vars := InitializeVariables(false)
errBuf := new(strings.Builder)
f := NewSQLCmdDefaultFormatter(false, ControlIgnore, false).(*sqlCmdFormatterType)
f.BeginBatch("", vars, new(strings.Builder), errBuf)
testErr := mssql.Error{
Number: 50000,
Class: 16,
State: 1,
ServerName: "testserver",
ProcName: "myStoredProc",
LineNo: 10,
Message: "Error raised from stored procedure.",
}
f.AddError(testErr)
output := errBuf.String()
// Should include the Procedure in the header
assert.Contains(t, output, "Msg 50000")
assert.Contains(t, output, "Level 16")
assert.Contains(t, output, "State 1")
assert.Contains(t, output, "Server testserver")
assert.Contains(t, output, "Procedure myStoredProc")
assert.Contains(t, output, "Line 10")
assert.Contains(t, output, "Error raised from stored procedure.")
}
func TestFormatterErrorWithProcNameRawMode(t *testing.T) {
vars := InitializeVariables(false)
errBuf := new(strings.Builder)
f := NewSQLCmdDefaultFormatter(false, ControlIgnore, true).(*sqlCmdFormatterType)
f.BeginBatch("", vars, new(strings.Builder), errBuf)
testErr := mssql.Error{
Number: 50000,
Class: 16,
State: 1,
ServerName: "testserver",
ProcName: "myStoredProc",
LineNo: 10,
Message: "Error raised from stored procedure.",
}
f.AddError(testErr)
output := errBuf.String()
// Raw mode should NOT include the header
assert.NotContains(t, output, "Msg 50000")
assert.NotContains(t, output, "Level 16")
assert.NotContains(t, output, "Procedure myStoredProc")
// But should still contain the actual error message
assert.Contains(t, output, "Error raised from stored procedure.")
}