-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsort_parser_test.go
More file actions
155 lines (148 loc) · 3.75 KB
/
Copy pathsort_parser_test.go
File metadata and controls
155 lines (148 loc) · 3.75 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
package qfv
import (
"reflect"
"testing"
)
func TestSortParser_Parse(t *testing.T) {
allowedFields := []string{"name", "age", "city"}
parser := NewSortParser(allowedFields)
tests := []struct {
name string
input string
expected SortNode
expectedErr error
}{
{
name: "empty input",
input: "",
expected: SortNode{},
expectedErr: &QFVSortError{Message: "empty input expression"},
},
{
name: "single field ascending lowercase",
input: "name asc",
expected: SortNode{
Fields: []SortFieldNode{
{Field: "name", Direction: SortAsc},
},
},
expectedErr: nil,
},
{
name: "single field ascending Uppercase",
input: "name ASC",
expected: SortNode{
Fields: []SortFieldNode{
{Field: "name", Direction: SortAsc},
},
},
expectedErr: nil,
},
{
name: "single field missing direction",
input: "name",
expected: SortNode{},
expectedErr: &QFVSortError{Field: "name", Message: "missing sort direction after field"},
},
{
name: "single field descending uppercase",
input: "name DESC",
expected: SortNode{
Fields: []SortFieldNode{
{Field: "name", Direction: SortDesc},
},
},
expectedErr: nil,
},
{
name: "single field descending lowercase",
input: "name desc",
expected: SortNode{
Fields: []SortFieldNode{
{Field: "name", Direction: SortDesc},
},
},
expectedErr: nil,
},
{
name: "single field, wrong direction",
input: "name invalid",
expected: SortNode{},
expectedErr: &QFVSortError{Field: "name", Message: "invalid sort direction"},
},
{
name: "single field multiple direction",
input: "name asc DESC",
expected: SortNode{},
expectedErr: &QFVSortError{Field: "name asc DESC", Message: "too many sort expressions"},
},
{
name: "single field descending with extra comma",
input: "name DESC,",
expected: SortNode{},
expectedErr: &QFVSortError{Message: "empty sort expression"},
},
{
name: "multiple fields, lowercase and uppercase",
input: "name asc, age DESC, city desc",
expected: SortNode{
Fields: []SortFieldNode{
{Field: "name", Direction: SortAsc},
{Field: "age", Direction: SortDesc},
{Field: "city", Direction: SortDesc},
},
},
expectedErr: nil,
},
{
name: "invalid field",
input: "unknown",
expected: SortNode{},
expectedErr: &QFVSortError{Field: "unknown", Message: "field not allowed for sorting"},
},
{
name: "invalid direction",
input: "name invalid",
expected: SortNode{},
expectedErr: &QFVSortError{Field: "name", Message: "invalid sort direction"},
},
{
name: "empty part",
input: "name, ,age",
expected: SortNode{},
expectedErr: &QFVSortError{Field: "name", Message: "missing sort direction after field"},
},
{
name: "invalid sort expression",
input: " ",
expected: SortNode{},
expectedErr: &QFVSortError{Message: "empty sort expression"},
},
{
name: "single comma",
input: ",",
expected: SortNode{},
expectedErr: &QFVSortError{Message: "empty sort expression"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := parser.Parse(tt.input)
if tt.expectedErr != nil {
if err == nil {
t.Fatalf("expected error '%v', got nil", tt.expectedErr)
}
if err.Error() != tt.expectedErr.Error() {
t.Fatalf("expected error '%v', got '%v'", tt.expectedErr, err)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(actual, tt.expected) {
t.Errorf("expected '%v', got '%v'", tt.expected, actual)
}
})
}
}