-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathsplit_test.go
More file actions
149 lines (133 loc) · 3.01 KB
/
split_test.go
File metadata and controls
149 lines (133 loc) · 3.01 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
package sqlfile
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
func TestSplit(t *testing.T) {
testdataDir := "testdata"
entries, err := os.ReadDir(testdataDir)
if err != nil {
t.Fatalf("Failed to read testdata directory: %v", err)
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
testName := entry.Name()
t.Run(testName, func(t *testing.T) {
testDir := filepath.Join(testdataDir, testName)
// Read input file
inputPath := filepath.Join(testDir, "input.sql")
inputData, err := os.ReadFile(inputPath)
if err != nil {
t.Fatalf("Failed to read input file: %v", err)
}
// Read expected output files
var expected []string
for i := 1; ; i++ {
outputPath := filepath.Join(testDir, fmt.Sprintf("output_%d.sql", i))
data, err := os.ReadFile(outputPath)
if err != nil {
if os.IsNotExist(err) {
break
}
t.Fatalf("Failed to read output file %s: %v", outputPath, err)
}
expected = append(expected, string(data))
}
// Run Split
ctx := context.Background()
reader := strings.NewReader(string(inputData))
got, err := Split(ctx, reader)
if err != nil {
t.Fatalf("Split() error = %v", err)
}
// Compare results
if len(got) != len(expected) {
t.Errorf("Split() got %d queries, expected %d", len(got), len(expected))
t.Logf("Got: %v", got)
t.Logf("Expected: %v", expected)
return
}
for i := range got {
if got[i] != expected[i] {
t.Errorf("Query %d:\ngot: %q\nexpected: %q", i, got[i], expected[i])
}
}
})
}
}
func TestSplitContextCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
reader := strings.NewReader("SELECT * FROM users;")
_, err := Split(ctx, reader)
if err != context.Canceled {
t.Errorf("Expected context.Canceled error, got %v", err)
}
}
func TestExtractDollarTag(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "empty dollar quote",
input: "$$",
expected: "$$",
},
{
name: "simple tag",
input: "$tag$",
expected: "$tag$",
},
{
name: "tag with numbers",
input: "$tag123$",
expected: "$tag123$",
},
{
name: "tag with underscore",
input: "$my_tag$",
expected: "$my_tag$",
},
{
name: "not a dollar quote (no closing)",
input: "$tag",
expected: "",
},
{
name: "not a dollar quote (invalid char)",
input: "$tag-name$",
expected: "",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "no dollar sign",
input: "tag",
expected: "",
},
{
name: "tag with extra content",
input: "$tag$rest of string",
expected: "$tag$",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractDollarTag(tt.input)
if got != tt.expected {
t.Errorf("extractDollarTag(%q) = %q, expected %q", tt.input, got, tt.expected)
}
})
}
}