-
Notifications
You must be signed in to change notification settings - Fork 3
/
builq_test.go
105 lines (93 loc) · 3.34 KB
/
builq_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
package builq
import (
"errors"
"strings"
"testing"
)
func TestColumns(t *testing.T) {
cols := Columns{"foo", "bar", "baz"}
t.Run("string", func(t *testing.T) {
const want = "foo, bar, baz"
if got := cols.String(); got != want {
t.Errorf("got %q; want %q", got, want)
}
})
t.Run("prefixed", func(t *testing.T) {
const want = "x.foo, x.bar, x.baz"
if got := cols.Prefixed("x."); got != want {
t.Errorf("got %q; want %q", got, want)
}
})
}
func TestBuilder(t *testing.T) {
test := func(name string, wantErr error, format string, args ...any) {
t.Helper()
t.Run(name, func(t *testing.T) {
t.Helper()
var b Builder
b.Addf(constString(format), args...)
_, _, err := b.Build()
if !errors.Is(err, wantErr) {
t.Errorf("\nhave: %v\nwant: %v", err, wantErr)
}
})
}
test("incorrect verb", errIncorrectVerb, "SELECT * FROM %+ slice", 1)
test("incorrect verb", errIncorrectVerb, "SELECT * FROM %+ slice", 1)
test("incorrect verb 1", errIncorrectVerb, "SELECT * FROM %+", 1)
test("incorrect verb 2", errIncorrectVerb, "SELECT * FROM %#", 1)
test("lonely verb", errLonelyModifier, "SELECT * FROM % super")
test("lonely verb", errLonelyModifier, "SELECT foo FROM bar%")
test("too few arguments", errTooFewArguments, "SELECT foo FROM bar%d")
test("too few arguments", errTooFewArguments, "SELECT * FROM %s")
test("too few arguments", errTooFewArguments, "SELECT * FROM %$")
test("too few arguments", errTooFewArguments, "SELECT * FROM %+?")
test("too many arguments", errTooManyArguments, "SELECT * FROM %s", "users", "users")
test("unsupported verb", errUnsupportedVerb, "SELECT * FROM %+x")
test("unsupported verb", errUnsupportedVerb, "SELECT * FROM %v", "users")
test("mixed placeholders", errMixedPlaceholders, "WHERE foo = %$ AND bar = %?", 1, 2)
test("non-slice argument", errNonSliceArgument, "WHERE foo = %+$", 1)
test("non-slice argument (batch)", errNonSliceArgument, "WHERE foo = %#$", 1)
test("non-numeric argument", errNonNumericArg, "WHERE foo = %d", "a")
}
func FuzzBuilder(f *testing.F) {
f.Add("SELECT %s FROM %s", "*", "users")
f.Add("SELECT * FROM %s WHERE name = %$", "users", "john")
f.Add("WHERE name = %$ AND surname = %$", "john", "doe")
f.Add("%s WHERE %? == name", "TEST", "john")
f.Add("%s WHERE %@ = surname", "TEST", "doe")
f.Fuzz(func(t *testing.T, format, arg1, arg2 string) {
var valid int
for _, verb := range []string{"%s", "%d", "%$", "%?", "%@"} {
valid += strings.Count(format, verb)
}
if valid != 2 {
t.Skip("format must have 2 valid verbs")
}
var b Builder
b.Addf(constString(format), arg1, arg2)
_, args, err := b.Build()
if err != nil {
// those errors are expected, we're looking for something new.
if errors.Is(err, errTooFewArguments) ||
errors.Is(err, errTooManyArguments) ||
errors.Is(err, errUnsupportedVerb) ||
errors.Is(err, errLonelyModifier) ||
errors.Is(err, errIncorrectVerb) ||
errors.Is(err, errMixedPlaceholders) ||
errors.Is(err, errNonSliceArgument) ||
errors.Is(err, errNonNumericArg) {
return
}
t.Fatalf("unexpected error: %v", err)
}
checkArgs := func(strCnt, argsCnt int) {
if strings.Count(format, "%s") == strCnt && len(args) != argsCnt {
t.Errorf("format with %d string verbs must be bundled with %d arguments", strCnt, argsCnt)
}
}
checkArgs(0, 2)
checkArgs(1, 1)
checkArgs(2, 0)
})
}