-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparser_test.go
More file actions
195 lines (184 loc) · 4.53 KB
/
parser_test.go
File metadata and controls
195 lines (184 loc) · 4.53 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package rql
import (
"strings"
"testing"
"time"
)
func TestValidateQuery(t *testing.T) {
type TestStruct struct {
ID int32 `rql:"name=id,type=number"`
Name string `rql:"name=name,type=string"`
IsActive bool `rql:"name=is_active,type=bool"`
CreatedAt time.Time `rql:"name=created_at,type=datetime"`
}
tests := []struct {
name string
query Query
checkStruct TestStruct
expectErr bool
}{
{
name: "Valid filters and sort",
query: Query{
Filters: []Filter{
{Name: "ID", Operator: "eq", Value: 123},
{Name: "Name", Operator: "like", Value: "test"},
{Name: "is_active", Operator: "eq", Value: true},
{Name: "created_at", Operator: "eq", Value: "2021-09-15T15:53:00Z"},
},
Sort: []Sort{
{Name: "ID", Order: "asc"},
},
},
checkStruct: TestStruct{},
expectErr: false,
},
{
name: "Invalid filter key",
query: Query{
Filters: []Filter{
{Name: "NonExistentKey", Operator: "eq", Value: "test"},
},
},
checkStruct: TestStruct{},
expectErr: true,
},
{
name: "Invalid filter operator",
query: Query{
Filters: []Filter{
{Name: "ID", Operator: "invalid", Value: 123},
},
},
checkStruct: TestStruct{},
expectErr: true,
},
{
name: "Invalid filter value type",
query: Query{
Filters: []Filter{
{Name: "ID", Operator: "eq", Value: "invalid"},
},
},
checkStruct: TestStruct{},
expectErr: true,
},
{
name: "Invalid sort key",
query: Query{
Sort: []Sort{
{Name: "NonExistentKey", Order: "asc"},
},
},
checkStruct: TestStruct{},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateQuery(&tt.query, tt.checkStruct)
if (err != nil) != tt.expectErr {
t.Errorf("ValidateQuery() error = %v, expectErr %v", err, tt.expectErr)
}
})
}
}
func TestGetDataTypeOfField(t *testing.T) {
type TestStruct struct {
StringField string `rql:"name=string_field,type=string"`
NumberField int `rql:"name=number_field,type=number"`
BoolField bool `rql:"name=bool_field,type=bool"`
DateTimeField time.Time `rql:"name=datetime_field,type=datetime"`
InvalidField string `rql:"name=invalid_field,type=invalid"`
NoTypeField string `rql:"name=no_type_field"` // No type specified
NoTagField string // No tag at all
}
tests := []struct {
name string
fieldName string
expectedType string
expectedError bool
errorContains string
}{
{
name: "String field by struct name",
fieldName: "StringField",
expectedType: "string",
expectedError: false,
},
{
name: "String field by tag name",
fieldName: "string_field",
expectedType: "string",
expectedError: false,
},
{
name: "Number field by struct name",
fieldName: "NumberField",
expectedType: "number",
expectedError: false,
},
{
name: "Number field by tag name",
fieldName: "number_field",
expectedType: "number",
expectedError: false,
},
{
name: "Bool field by struct name",
fieldName: "BoolField",
expectedType: "bool",
expectedError: false,
},
{
name: "DateTime field by struct name",
fieldName: "DateTimeField",
expectedType: "datetime",
expectedError: false,
},
{
name: "Invalid field name",
fieldName: "NonExistentField",
expectedType: "",
expectedError: true,
errorContains: "is not a valid field",
},
{
name: "No type specified in tag",
fieldName: "NoTypeField",
expectedType: "string", // Should default to string
expectedError: false,
},
{
name: "No tag field",
fieldName: "NoTagField",
expectedType: "string", // Should default to string
expectedError: false,
},
}
testStruct := TestStruct{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dataType, err := GetDataTypeOfField(tt.fieldName, testStruct)
// Check error cases
if tt.expectedError {
if err == nil {
t.Errorf("Expected error but got none")
return
}
if !strings.Contains(err.Error(), tt.errorContains) {
t.Errorf("Expected error containing '%s', got '%s'", tt.errorContains, err.Error())
}
return
}
// Check success cases
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if dataType != tt.expectedType {
t.Errorf("Expected type '%s', got '%s'", tt.expectedType, dataType)
}
})
}
}