forked from brandonvfx/sg-restful
/
format1_test.go
190 lines (141 loc) · 5.38 KB
/
format1_test.go
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
package main
import (
"net/http"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/stretchr/testify/suite"
)
// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type Format1TestSuite struct {
suite.Suite
}
// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *Format1TestSuite) SetupSuite() {
manager := GetQPManager()
log.Info(" -- Format1 Test Suite --\n")
log.Debug("Format1TestSuite.SetupSuite() - setting active parsers to format1")
manager.SetActiveParsers("format1")
}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestFormat1TestSuite(t *testing.T) {
//ts := new(Format1TestSuite)
log.Info("TestFormat1TestSuite - Running test suite")
suite.Run(t, new(Format1TestSuite))
log.Info("TestFormat1TestSuite - Finished test suite")
}
func (suite *Format1TestSuite) TestActiveParsers() {
log.Info("Format1TestSuite.TestActiveParsers()")
manager := GetQPManager()
keys, active := manager.GetActiveParsers()
log.Infof("active parsers:%s %s", keys, active)
suite.Equal(len(active), 1, "Should be a single active parser")
}
func (suite *Format1TestSuite) TestParseQueryAndOrStatementRegExpError() {
testString := "()"
f := &Format1{}
tf := f.CanParseString(testString)
suite.Equal(false, tf, "Should not be able to parse string")
_, err := f.ParseString(testString)
expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "Invalid query format",
}
suite.Equal(expectedError, err, "Should be formating error")
}
func (suite *Format1TestSuite) TestParseQueryAndOrStatementFilterFormatError() {
testString := "and(foo, bar)"
f := &Format1{}
tf := f.CanParseString(testString)
suite.Equal(false, tf, "Should not be able to parse string")
_, err := f.ParseString(testString)
expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "Invalid query filter format",
}
suite.Equal(expectedError, err, "Should be formating error")
}
func (suite *Format1TestSuite) TestParseQueryAndOrStatementJsonError() {
testString := "and([foo, bar,])"
f := &Format1{}
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should not be able to parse string")
_, err := f.ParseString(testString)
expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "invalid character 'o' in literal false (expecting 'a')",
}
suite.Equal(expectedError, err, "Should be formating error")
}
func (suite *Format1TestSuite) TestParseQueryAndOrStatementNotAQuery() {
testString := "andwell_this_is_bad"
f := &Format1{}
tf := f.CanParseString(testString)
suite.Equal(false, tf, "Should not be able to parse string")
_, err := f.ParseString(testString)
expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "Invalid query format",
}
suite.Equal(expectedError, err, "Should be formating error")
}
func (suite *Format1TestSuite) TestParseQueryAndOrStatementSliceOfFilters() {
testString := `and([["name", "is", "blorg"]])`
f := &Format1{}
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should not be able to parse string")
rf, err := f.ParseString(testString)
rfExpected := newReadFilters()
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
suite.Equal(nil, err, "Error should be nil")
suite.Equal(rfExpected, rf, "Should be the same")
}
func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicAnd() {
testString := `and(["name", "is", "blorg"])`
f := &Format1{}
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should not be able to parse string")
rf, err := f.ParseString(testString)
rfExpected := newReadFilters()
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
suite.Equal(nil, err, "Error should be nil")
suite.Equal(rfExpected, rf, "Should be the same")
}
func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicAndUpper() {
testString := `AND(["name", "is", "blorg"])`
f := &Format1{}
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")
rf, err := f.ParseString(testString)
rfExpected := newReadFilters()
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
suite.Equal(nil, err, "Error should be nil")
suite.Equal(rfExpected, rf, "Should be the same")
}
func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicOr() {
testString := `or(["name", "is", "blorg"])`
f := &Format1{}
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")
rf, err := f.ParseString(testString)
rfExpected := newReadFilters()
rfExpected.LogicalOperator = "or"
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
suite.Equal(nil, err, "Error should be nil")
suite.Equal(rfExpected, rf, "Should be the same")
}
func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicOrUpper() {
testString := `OR(["name", "is", "blorg"])`
f := &Format1{}
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")
rf, err := f.ParseString(testString)
rfExpected := newReadFilters()
rfExpected.LogicalOperator = "or"
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
suite.Equal(nil, err, "Error should be nil")
suite.Equal(rfExpected, rf, "Should be the same")
}