This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_test.go
212 lines (166 loc) · 5.29 KB
/
order_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) KwanJunWen
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package pgquery_test
import (
"encoding/json"
"fmt"
"math"
"github.com/go-pg/pg/v10/orm"
"github.com/junwen-k/pgquery"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Order", func() {
type OrderTestItem struct {
Id int64
Name string
Age int
}
Context("marshalling json", func() {
It("should marshal json successfully", func() {
s := pgquery.NewOrderAsc("")
b, err := json.Marshal(s)
Expect(err).NotTo(HaveOccurred())
Expect(b).To(MatchJSON(`"ASC"`))
})
})
Context("unmarshalling json", func() {
When("using object syntax", func() {
When("using integer", func() {
It("should unmarshal json successfully", func() {
s := pgquery.NewOrderAsc("")
err := json.Unmarshal([]byte(`{"direction":1}`), s)
Expect(err).ToNot(HaveOccurred())
Expect(s).To(Equal(pgquery.NewOrderDesc("")))
})
})
When("using uppercase", func() {
It("should unmarshal json successfully", func() {
s := pgquery.NewOrderAsc("")
err := json.Unmarshal([]byte(`{"direction":"DESC"}`), s)
Expect(err).ToNot(HaveOccurred())
Expect(s).To(Equal(pgquery.NewOrderDesc("")))
})
})
When("using mixed case", func() {
It("should unmarshal json successfully", func() {
s := pgquery.NewOrderAsc("")
err := json.Unmarshal([]byte(`{"direction":"DeSc"}`), s)
Expect(err).ToNot(HaveOccurred())
Expect(s).To(Equal(pgquery.NewOrderDesc("")))
})
})
When("using lowercase", func() {
It("should unmarshal json successfully", func() {
s := pgquery.NewOrderAsc("")
err := json.Unmarshal([]byte(`{"direction":"desc"}`), s)
Expect(err).ToNot(HaveOccurred())
Expect(s).To(Equal(pgquery.NewOrderDesc("")))
})
})
})
When("using non-object syntax", func() {
When("using uppercase", func() {
It("should unmarshal json successfully", func() {
s := pgquery.NewOrderAsc("")
err := json.Unmarshal([]byte(`"DESC"`), s)
Expect(err).ToNot(HaveOccurred())
Expect(s).To(Equal(pgquery.NewOrderDesc("")))
})
})
When("using mixed case", func() {
It("should unmarshal json successfully", func() {
s := pgquery.NewOrderAsc("")
err := json.Unmarshal([]byte(`"DeSc"`), s)
Expect(err).ToNot(HaveOccurred())
Expect(s).To(Equal(pgquery.NewOrderDesc("")))
})
})
When("using lowercase", func() {
It("should unmarshal json successfully", func() {
s := pgquery.NewOrderAsc("")
err := json.Unmarshal([]byte(`"desc"`), s)
Expect(err).ToNot(HaveOccurred())
Expect(s).To(Equal(pgquery.NewOrderDesc("")))
})
})
})
})
Context("generating sql", func() {
It("should generate correct SQL string", func() {
q := orm.NewQuery(nil, &OrderTestItem{})
q.OrderExpr(pgquery.NewOrderAsc("age").Appender())
s := queryString(q)
Expect(s).To(Equal(`SELECT "order_test_item"."id", "order_test_item"."name", "order_test_item"."age" FROM "order_test_items" AS "order_test_item" ORDER BY "age" ASC`))
})
})
Context("integration testing", func() {
err := db.Model((*OrderTestItem)(nil)).CreateTable(&orm.CreateTableOptions{
Temp: true,
})
Expect(err).ToNot(HaveOccurred())
for itemCount := 1; itemCount <= 10; itemCount++ {
item := &OrderTestItem{
Name: fmt.Sprintf("name-%d", itemCount),
Age: itemCount,
}
_, err = db.Model(item).Insert()
Expect(err).ToNot(HaveOccurred())
}
It("works with asc value", func() {
var items []OrderTestItem
q := db.Model(&items)
q.OrderExpr(pgquery.NewOrderAsc("age").Appender())
err := q.Select()
Expect(err).ToNot(HaveOccurred())
if Expect(items).To(HaveLen(10)) {
for idx, item := range items {
Expect(item.Id).ToNot(BeZero())
Expect(item.Name).To(Equal(fmt.Sprintf("name-%d", idx+1)))
Expect(item.Age).To(Equal(idx + 1))
}
}
})
It("works with desc value", func() {
var items []OrderTestItem
q := db.Model(&items)
q.OrderExpr(pgquery.NewOrderDesc("age").Appender())
err := q.Select()
Expect(err).ToNot(HaveOccurred())
if Expect(items).To(HaveLen(10)) {
for idx, item := range items {
Expect(item.Id).ToNot(BeZero())
Expect(item.Name).To(Equal(fmt.Sprintf("name-%d", len(items)-idx)))
Expect(item.Age).To(Equal(len(items) - idx))
}
}
})
It("works with multiple columns asc value", func() {
var items []OrderTestItem
q := db.Model(&items)
q.OrderExpr(pgquery.NewOrderDesc("name").Appender())
q.OrderExpr(pgquery.NewOrderDesc("age").Appender())
err := q.Select()
Expect(err).ToNot(HaveOccurred())
if Expect(items).To(HaveLen(10)) {
for idx, item := range items {
switch idx {
case 8:
Expect(item.Id).ToNot(BeZero())
Expect(item.Name).To(Equal(fmt.Sprintf("name-%d", 10)))
Expect(item.Age).To(Equal(10))
case 9:
Expect(item.Id).ToNot(BeZero())
Expect(item.Name).To(Equal(fmt.Sprintf("name-%d", 1)))
Expect(item.Age).To(Equal(1))
default:
Expect(item.Id).ToNot(BeZero())
Expect(item.Name).To(Equal(fmt.Sprintf("name-%d", int(math.Abs(float64(idx-9))))))
Expect(item.Age).To(Equal(int(math.Abs(float64(idx - 9)))))
}
}
}
})
})
})