-
Notifications
You must be signed in to change notification settings - Fork 1
/
orderby_test.go
116 lines (91 loc) · 2.5 KB
/
orderby_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
package paging
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestGoPaging(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "GoPaging Suite")
}
var _ = Describe("Order By clause", func() {
var (
pa *PageArgs
cols []string
)
BeforeEach(func() {
first := 0
after := "after"
pa = &PageArgs{
After: &after,
First: &first,
}
cols = []string{"col1", "col2"}
})
Describe("WithSortBy", func() {
It("should handle a nil PageArgs arg", func() {
pa := WithSortBy(nil, true, "col1")
Expect(pa).ToNot(BeNil())
})
})
It("should have zero values for basic PageArgs", func() {
Expect(pa.sortByCols).To(BeNil())
Expect(pa.isDesc).To(BeFalse())
})
Describe("Default", func() {
It("should use `created_at` for default orderby column", func() {
sut := NewOffsetPaginator(pa, 5)
Expect(sut.orderBy).To(Equal("created_at"))
})
})
Describe("Desc Flag & Cols", func() {
Describe("Desc = true", func() {
It("should set the PageArgs fields", func() {
pa = WithSortBy(pa, true, cols...)
Expect(pa.isDesc).To(BeTrue())
Expect(pa.sortByCols).To(ContainElements(cols))
})
It("should set the OffsetPaginator `orderBy` field", func() {
pa = WithSortBy(pa, true, cols...)
sut := NewOffsetPaginator(pa, 5)
Expect(sut.orderBy).To(Equal("col1, col2 DESC"))
})
})
Describe("Desc = false", func() {
It("should set the PageArgs fields", func() {
pa = WithSortBy(pa, false, cols...)
Expect(pa.isDesc).To(BeFalse())
Expect(pa.sortByCols).To(ContainElements(cols))
})
It("should set the OffsetPaginator `orderBy` field", func() {
pa = WithSortBy(pa, false, cols...)
sut := NewOffsetPaginator(pa, 5)
Expect(sut.orderBy).To(Equal("col1, col2"))
})
})
})
Describe("Desc Flag only", func() {
Describe("Desc = true", func() {
It("should set the PageArgs fields", func() {
pa = WithSortBy(pa, true)
Expect(pa.isDesc).To(BeTrue())
})
It("should set the OffsetPaginator `orderBy` field", func() {
pa = WithSortBy(pa, true)
sut := NewOffsetPaginator(pa, 5)
Expect(sut.orderBy).To(Equal("created_at DESC"))
})
})
Describe("Desc = false", func() {
It("should set the PageArgs fields", func() {
pa = WithSortBy(pa, false)
Expect(pa.isDesc).To(BeFalse())
})
It("should set the OffsetPaginator `orderBy` field", func() {
pa = WithSortBy(pa, false)
sut := NewOffsetPaginator(pa, 5)
Expect(sut.orderBy).To(Equal("created_at"))
})
})
})
})