-
Notifications
You must be signed in to change notification settings - Fork 2
/
format2_test.go
175 lines (136 loc) · 5.21 KB
/
format2_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
package main
import (
"net/http"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/stretchr/testify/suite"
)
// Format2TestSuite defines the suite, and absorbs the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type Format2TestSuite struct {
suite.Suite
}
// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *Format2TestSuite) SetupSuite() {
log.Info(" -- Format2 Test Suite --\n")
manager := GetQPManager()
manager.ResetActive()
manager.AddParser("format2", &Format2{})
manager.SetActiveParsers("format2")
}
// 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 TestFormat2TestSuite(t *testing.T) {
suite.Run(t, new(Format2TestSuite))
}
func (suite *Format2TestSuite) TestParseQueryHashJsonError() {
f := &Format2{}
testString := "{foo}"
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 character 'f' looking for beginning of object key string",
}
suite.Equal(expectedError, err, "Should a json error")
}
func (suite *Format2TestSuite) TestParseQueryHashConditionsOnly() {
f := &Format2{}
teststring := `{"conditions": [["name", "is", "blorg"]]}`
tf := f.CanParseString(teststring)
suite.Equal(tf, false, "Should not be able to parse string")
_, err := f.ParseString(teststring)
expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "Missing key: 'logical_operator'",
}
suite.Equal(expectedError, err, "Should a json error")
}
func (suite *Format2TestSuite) TestParseQueryHashLogicalOpOnly() {
f := &Format2{}
testString := `{"logical_operator": "and"}`
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: "Missing key: 'conditions'",
}
suite.Equal(expectedError, err, "Should a json error")
}
func (suite *Format2TestSuite) TestParseQueryHashStatementBasicAnd() {
f := &Format2{}
testString := `{"logical_operator": "and", "conditions": [["name", "is", "blorg"]]}`
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")
rf, err := f.ParseString(testString)
rfExpected := newReadFilters()
rfExpected.LogicalOperator = "and"
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
suite.Equal(nil, err, "Error should be nil")
suite.Equal(rfExpected, rf, "Should be the same")
}
func (suite *Format2TestSuite) TestParseQueryHashStatementBasicOr() {
f := &Format2{}
testString := `{"logical_operator": "or", "conditions": [["name", "is", "blorg"]]}`
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 *Format2TestSuite) TestParseQueryHashStatementAnd() {
testString := `{"logical_operator": "and", "conditions": [["name", "is", "blorg"], ["sg_status", "in", ["Active", "Bidding"]]]}`
f := &Format2{}
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")
rf, err := f.ParseString(testString)
rfExpected := newReadFilters()
rfExpected.LogicalOperator = "and"
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
rfExpected.AddCondition(newQueryCondition("sg_status", "in", []interface{}{"Active", "Bidding"}))
suite.Equal(nil, err, "Error should be nil")
suite.Equal(rfExpected, rf, "Should be the same")
}
func (suite *Format2TestSuite) TestParseQueryHashStatementOr() {
testString := `{"logical_operator": "or", "conditions": [["name", "is", "blorg"]]}`
f := &Format2{}
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 *Format2TestSuite) TestMapToReadFiltersConditionsOnlyError() {
rf := newReadFilters()
query := map[string]interface{}{
"conditions": []interface{}{[]string{"name", "is", "blorg"}},
}
err := mapToReadFilters(query, &rf)
expectedError := queryParseError{
StatusCode: http.StatusInternalServerError,
Message: "Missing key: 'logical_operator'",
}
suite.Equal(expectedError, err, "Should a json error")
}
func (suite *Format2TestSuite) TestMapToReadFiltersLogicalOpOnlyError() {
rf := newReadFilters()
query := map[string]interface{}{
"logical_operator": "or",
}
err := mapToReadFilters(query, &rf)
expectedError := queryParseError{
StatusCode: http.StatusInternalServerError,
Message: "Missing key: 'conditions'",
}
suite.Equal(expectedError, err, "Should a json error")
}