-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_test.go
More file actions
80 lines (70 loc) · 1.61 KB
/
hash_test.go
File metadata and controls
80 lines (70 loc) · 1.61 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
package sqlcmp
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func testHashString(t *testing.T, s string) string {
t.Helper()
h, err := hashString(s)
require.NoError(t, err)
return h
}
func TestSemiHash(t *testing.T) {
t.Parallel()
tt := []struct {
name string
sql string
out string
segment Segment
err error
}{
{
name: "empty",
out: testHashString(t, ""),
},
{
name: "error parse",
sql: "select * from wh as 1 ere id=1",
err: ErrParse,
},
{
name: "segment all",
sql: "select * from users where id = 100",
segment: SegmentAll,
out: testHashString(t, "*||users||(id = 100)||"),
},
{
name: "segment from",
sql: "select * from users where id = 100",
segment: SegmentFrom,
out: testHashString(t, "users||"),
},
{
name: "segment all and skip values",
sql: "select * from users where id = 100 and abc IN (99,100)",
segment: SegmentAll | SegmentSkipValues,
out: testHashString(t, "*||users||(id = ?) ANDabc IN (?) AND||"),
},
{
name: "segment all and not skip values",
sql: "select * from users where id = 100 and abc IN (99,100)",
segment: SegmentAll,
out: testHashString(t, "*||users||((id = 100) AND abc IN (99, 100))||"),
},
}
for i := range tt {
tc := tt[i]
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
out, err := SemiHash(tc.sql, tc.segment)
if tc.err == nil {
require.NoError(t, err)
require.EqualValues(t, tc.out, out)
} else {
require.Error(t, err)
require.True(t, errors.Is(err, tc.err))
}
})
}
}