-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from nrfta/feat/add-sort-by
Add sort by to paging
- Loading branch information
Showing
7 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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")) | ||
}) | ||
}) | ||
}) | ||
}) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.