-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptions_test.go
More file actions
138 lines (129 loc) · 3.39 KB
/
options_test.go
File metadata and controls
138 lines (129 loc) · 3.39 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
package q2sql
import (
"context"
"fmt"
"reflect"
"testing"
"github.com/velmie/qparser"
)
type optionsTest struct {
b *ResourceSelectBuilder
options []ResourceSelectBuilderOption
}
func mockExtension(_ context.Context, query *qparser.Query, builder *SelectBuilder) error { return nil }
var optionsTests = []optionsTest{
{
b: &ResourceSelectBuilder{
defaultFields: []string{"fieldA", "fieldB"},
},
options: []ResourceSelectBuilderOption{
WithDefaultFields([]string{"fieldA", "fieldB"}),
},
},
{
b: &ResourceSelectBuilder{
allowedSortFields: []string{"fieldA", "fieldB"},
},
options: []ResourceSelectBuilderOption{
AllowSortingByFields([]string{"fieldA", "fieldB"}),
},
},
{
b: &ResourceSelectBuilder{
allowedSelectFields: map[string]struct{}{
"x1": {},
"x2": {},
"x3": {},
"y1": {},
"y2": {},
"y3": {},
},
allowedSelectFieldsSlc: []string{"x1", "x2", "x3", "y1", "y2", "y3"},
},
options: []ResourceSelectBuilderOption{
AllowSelectFields([]string{"x1", "x2", "x3"}),
AllowSelectFields([]string{"y1", "y2", "y3"}),
},
},
{
b: &ResourceSelectBuilder{
allowedSelectFields: map[string]struct{}{
"x1": {},
"x2": {},
"x3": {},
},
allowedSelectFieldsSlc: []string{"x1", "x2", "x3"},
alwaysSelectFields: []string{"x1"},
},
options: []ResourceSelectBuilderOption{
AllowSelectFields([]string{"x1", "x2", "x3"}),
AlwaysSelectFields([]string{"x1"}),
},
},
{
b: &ResourceSelectBuilder{
allowedSelectFields: map[string]struct{}{
"x1": {},
"x2": {},
"x3": {},
},
allowedSelectFieldsSlc: []string{"x1", "x2", "x3"},
alwaysSelectAllFields: true,
},
options: []ResourceSelectBuilderOption{
AllowSelectFields([]string{"x1", "x2", "x3"}),
AlwaysSelectAllFields(true),
},
},
}
func TestOptions(t *testing.T) {
for i, tt := range optionsTests {
meta := fmt.Sprintf("test %d", i)
b := &ResourceSelectBuilder{}
for _, option := range tt.options {
option(b)
}
if !reflect.DeepEqual(b, tt.b) {
t.Errorf("%s: \n\twant %+v \n\tgot %+v", meta, tt.b, b)
continue
}
}
}
func TestExtend(t *testing.T) {
b := new(ResourceSelectBuilder)
Extend(mockExtension)(b)
if len(b.extensions) != 1 {
t.Errorf("unexpected extensions length: want 1, got %d", len(b.extensions))
return
}
if fmt.Sprintf("%p", mockExtension) != fmt.Sprintf("%p", b.extensions[0]) {
t.Error("unexpected extension in the list")
}
}
type mockExprParser struct {
FilterExpressionParser
}
func TestAllowFiltering(t *testing.T) {
b := new(ResourceSelectBuilder)
const conditionName = "c1"
conditions := ConditionMap{
conditionName: func(field string, args ...interface{}) (Sqlizer, error) {
return nil, nil //nolint:nilnil // function is just testing
},
}
allowedCondition := AllowedConditions{
"field": []string{conditionName},
}
exprParser := new(mockExprParser)
AllowFiltering(allowedCondition, conditions, exprParser)(b)
if fmt.Sprintf("%p", b.conditions) != fmt.Sprintf("%p", conditions) {
t.Errorf("unexpected condition factory:\n\twant %+v\n\tgot %+v", conditions, b.conditions)
}
if !reflect.DeepEqual(b.allowedConditions, allowedCondition) {
t.Errorf("unexpected allowed conditions: \n\twant %+v \n\tgot %+v", allowedCondition, b.allowedConditions)
return
}
if b.parser != exprParser {
t.Errorf("unexpected expression parser:\n\twant %+v\n\tgot %+v", exprParser, b.parser)
}
}