-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_test.go
More file actions
161 lines (138 loc) · 5.05 KB
/
Copy pathfilter_test.go
File metadata and controls
161 lines (138 loc) · 5.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
package pistachio_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/winebarrel/pistachio"
)
func TestValidatePatterns(t *testing.T) {
t.Run("valid patterns", func(t *testing.T) {
o := &pistachio.FilterOptions{Include: []string{"user*", "post?"}, Exclude: []string{"tmp_*"}}
assert.NoError(t, o.ValidatePatterns())
})
t.Run("invalid include pattern", func(t *testing.T) {
o := &pistachio.FilterOptions{Include: []string{"[invalid"}}
err := o.ValidatePatterns()
require.Error(t, err)
assert.Contains(t, err.Error(), "--include")
})
t.Run("invalid exclude pattern", func(t *testing.T) {
o := &pistachio.FilterOptions{Exclude: []string{"[invalid"}}
err := o.ValidatePatterns()
require.Error(t, err)
assert.Contains(t, err.Error(), "--exclude")
})
t.Run("empty", func(t *testing.T) {
o := &pistachio.FilterOptions{}
assert.NoError(t, o.ValidatePatterns())
})
}
func TestFilterOptions_AfterApply_Valid(t *testing.T) {
o := &pistachio.FilterOptions{Include: []string{"user*"}}
assert.NoError(t, o.AfterApply())
}
func TestFilterOptions_AfterApply_Invalid(t *testing.T) {
o := &pistachio.FilterOptions{Include: []string{"[bad"}}
err := o.AfterApply()
require.Error(t, err)
assert.Contains(t, err.Error(), "--include")
}
func TestFilterOptions_AfterApply_TrimsWhitespace(t *testing.T) {
o := &pistachio.FilterOptions{
Include: []string{" user* ", "\tposts\n"},
Exclude: []string{" tmp_* "},
}
require.NoError(t, o.AfterApply())
assert.Equal(t, []string{"user*", "posts"}, o.Include)
assert.Equal(t, []string{"tmp_*"}, o.Exclude)
}
func TestMatchName(t *testing.T) {
t.Run("no filters", func(t *testing.T) {
o := &pistachio.FilterOptions{}
assert.True(t, o.MatchName("users"))
})
t.Run("include match", func(t *testing.T) {
o := &pistachio.FilterOptions{Include: []string{"users"}}
assert.True(t, o.MatchName("users"))
assert.False(t, o.MatchName("posts"))
})
t.Run("include wildcard", func(t *testing.T) {
o := &pistachio.FilterOptions{Include: []string{"user*"}}
assert.True(t, o.MatchName("users"))
assert.True(t, o.MatchName("user_roles"))
assert.False(t, o.MatchName("posts"))
})
t.Run("exclude match", func(t *testing.T) {
o := &pistachio.FilterOptions{Exclude: []string{"posts"}}
assert.True(t, o.MatchName("users"))
assert.False(t, o.MatchName("posts"))
})
t.Run("exclude wildcard", func(t *testing.T) {
o := &pistachio.FilterOptions{Exclude: []string{"tmp_*"}}
assert.True(t, o.MatchName("users"))
assert.False(t, o.MatchName("tmp_backup"))
})
t.Run("include and exclude", func(t *testing.T) {
o := &pistachio.FilterOptions{Include: []string{"user*"}, Exclude: []string{"user_tmp"}}
assert.True(t, o.MatchName("users"))
assert.True(t, o.MatchName("user_roles"))
assert.False(t, o.MatchName("user_tmp"))
assert.False(t, o.MatchName("posts"))
})
t.Run("multiple include patterns", func(t *testing.T) {
o := &pistachio.FilterOptions{Include: []string{"users", "posts"}}
assert.True(t, o.MatchName("users"))
assert.True(t, o.MatchName("posts"))
assert.False(t, o.MatchName("orders"))
})
t.Run("question mark wildcard", func(t *testing.T) {
o := &pistachio.FilterOptions{Include: []string{"user?"}}
assert.True(t, o.MatchName("users"))
assert.False(t, o.MatchName("user_roles"))
})
}
func TestIsTypeEnabled_Disable(t *testing.T) {
t.Run("disable table", func(t *testing.T) {
f := &pistachio.FilterOptions{Disable: []string{"table"}}
assert.False(t, f.IsTypeEnabled("table"))
assert.True(t, f.IsTypeEnabled("view"))
assert.True(t, f.IsTypeEnabled("enum"))
assert.True(t, f.IsTypeEnabled("domain"))
})
t.Run("disable multiple", func(t *testing.T) {
f := &pistachio.FilterOptions{Disable: []string{"table", "view"}}
assert.False(t, f.IsTypeEnabled("table"))
assert.False(t, f.IsTypeEnabled("view"))
assert.True(t, f.IsTypeEnabled("enum"))
assert.True(t, f.IsTypeEnabled("domain"))
})
t.Run("enable takes precedence over disable", func(t *testing.T) {
f := &pistachio.FilterOptions{Enable: []string{"enum"}, Disable: []string{"table"}}
assert.True(t, f.IsTypeEnabled("enum"))
assert.False(t, f.IsTypeEnabled("table"))
assert.False(t, f.IsTypeEnabled("view"))
})
}
func TestIsTypeEnabled_Enable(t *testing.T) {
t.Run("empty (all enabled)", func(t *testing.T) {
f := &pistachio.FilterOptions{}
assert.True(t, f.IsTypeEnabled("table"))
assert.True(t, f.IsTypeEnabled("view"))
assert.True(t, f.IsTypeEnabled("enum"))
assert.True(t, f.IsTypeEnabled("domain"))
})
t.Run("only table", func(t *testing.T) {
f := &pistachio.FilterOptions{Enable: []string{"table"}}
assert.True(t, f.IsTypeEnabled("table"))
assert.False(t, f.IsTypeEnabled("view"))
assert.False(t, f.IsTypeEnabled("enum"))
assert.False(t, f.IsTypeEnabled("domain"))
})
t.Run("multiple types", func(t *testing.T) {
f := &pistachio.FilterOptions{Enable: []string{"table", "enum"}}
assert.True(t, f.IsTypeEnabled("table"))
assert.False(t, f.IsTypeEnabled("view"))
assert.True(t, f.IsTypeEnabled("enum"))
assert.False(t, f.IsTypeEnabled("domain"))
})
}