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
202 lines (176 loc) · 6.05 KB
/
Copy pathformat_test.go
File metadata and controls
202 lines (176 loc) · 6.05 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
// 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")
assert.Equal(t, "\x1b[38;2;0;128;0mname\x1b[0m"+SqlcmdEol+SqlcmdEol+"\x1b[3m(1 row affected)"+SqlcmdEol+"\x1b[0m", buf.buf.String())
}
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) {
// Test that raw errors mode only prints the error message without the Msg/Level/State header
vars := InitializeVariables(false)
errBuf := new(strings.Builder)
// Create formatter with rawErrors = false (default)
f := NewSQLCmdDefaultFormatter(false, ControlIgnore, false).(*sqlCmdFormatterType)
f.BeginBatch("", vars, new(strings.Builder), errBuf)
// Create a mssql.Error to test with
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'.")
// Create formatter with rawErrors = true
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'.")
}