-
Notifications
You must be signed in to change notification settings - Fork 4
/
query_test.go
165 lines (129 loc) · 3.12 KB
/
query_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
package goal_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"testing"
"github.com/thomasdao/goal"
)
func (user *testuser) Query(w http.ResponseWriter, req *http.Request) (int, interface{}, error) {
return goal.HandleQuery(reflect.TypeOf(user), req)
}
func queryPath(query []byte) string {
return fmt.Sprint(server.URL, "/query/testuser/", url.QueryEscape(string(query)))
}
func createUsers() {
// Prepare data
names := []string{"Thomas", "Alan", "Jason", "Ben"}
ages := []int{28, 30, 22, 40}
for index, name := range names {
user := &testuser{}
user.Name = name
user.Age = ages[index]
db.Create(user)
}
}
func TestSuccessQueryParamsFind(t *testing.T) {
setup()
defer tearDown()
createUsers()
// Test success case
item := &goal.QueryItem{}
item.Key = "name"
item.Op = "="
item.Val = "Thomas"
params := &goal.QueryParams{}
params.Where = []*goal.QueryItem{item}
var results []testuser
var user testuser
params.Find(&user, &results)
if results == nil || len(results) != 1 {
t.Error("Error: query should return 1 result")
}
orItem := &goal.QueryItem{}
orItem.Key = "name"
orItem.Op = "="
orItem.Val = "Alan"
item.Or = []*goal.QueryItem{orItem}
params.Where = []*goal.QueryItem{item}
params.Find(&user, &results)
if results == nil || len(results) != 2 {
t.Error("Error: query should return 2 result")
}
andItem := &goal.QueryItem{}
andItem.Key = "age"
andItem.Op = ">"
andItem.Val = "29"
params.Where = []*goal.QueryItem{item, andItem}
params.Find(&user, &results)
if results == nil || len(results) != 1 {
t.Error("Error: query should return 1 result")
}
}
func TestInvalidQueryParamsFind(t *testing.T) {
setup()
defer tearDown()
createUsers()
// Test success case
item := &goal.QueryItem{}
item.Key = "name"
item.Op = "hello"
item.Val = "Thomas"
params := &goal.QueryParams{}
params.Where = []*goal.QueryItem{item}
var results []testuser
var user testuser
err := params.Find(&user, &results)
if err == nil {
t.Error("Error: Query operator should be invalid")
}
item = &goal.QueryItem{}
item.Key = "hello"
item.Op = "="
item.Val = "Thomas"
params.Where = []*goal.QueryItem{item}
err = params.Find(&user, &results)
if err == nil {
t.Error("Error: Query column should be invalid")
}
}
func TestQueryViaAPI(t *testing.T) {
setup()
defer tearDown()
createUsers()
item := &goal.QueryItem{}
item.Key = "name"
item.Op = "="
item.Val = "Thomas"
params := &goal.QueryParams{}
params.Where = []*goal.QueryItem{item}
query, _ := json.Marshal(params)
req, _ := http.NewRequest("GET", queryPath(query), nil)
req.Header.Set("Content-Type", "application/json")
// Get response
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
t.Error(err)
}
if res.StatusCode != 200 {
fmt.Println(res.StatusCode)
t.Error("Request Failed")
return
}
defer res.Body.Close()
content, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Error(err)
return
}
var results []testuser
json.Unmarshal(content, &results)
if results == nil || len(results) != 1 {
t.Error("Error: query should return 1 result")
}
}