pagination_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package pagination_test
  2. import (
  3. "testing"
  4. . "github.com/onsi/ginkgo"
  5. . "github.com/onsi/gomega"
  6. "github.com/vladimirok5959/golang-utils/utils/pagination"
  7. )
  8. var _ = Describe("pagination", func() {
  9. Context("Data", func() {
  10. Context("New", func() {
  11. It("generate correct data", func() {
  12. pd := pagination.New(0, 0, 2)
  13. Expect(pd.CurrentPage()).To(Equal(int64(1)))
  14. Expect(pd.MaxPages()).To(Equal(int64(1)))
  15. Expect(pd.ResultsCount()).To(Equal(int64(0)))
  16. Expect(pd.ResultsPerPage()).To(Equal(int64(2)))
  17. })
  18. })
  19. Context("CurrentPage", func() {
  20. It("returns correct value", func() {
  21. Expect(pagination.New(0, 0, 2).CurrentPage()).To(Equal(int64(1)))
  22. Expect(pagination.New(1, 0, 2).CurrentPage()).To(Equal(int64(1)))
  23. Expect(pagination.New(2, 0, 2).CurrentPage()).To(Equal(int64(1)))
  24. Expect(pagination.New(3, 0, 2).CurrentPage()).To(Equal(int64(1)))
  25. Expect(pagination.New(-1, 0, 2).CurrentPage()).To(Equal(int64(1)))
  26. Expect(pagination.New(0, 2, 2).CurrentPage()).To(Equal(int64(1)))
  27. Expect(pagination.New(1, 2, 2).CurrentPage()).To(Equal(int64(1)))
  28. Expect(pagination.New(2, 2, 2).CurrentPage()).To(Equal(int64(1)))
  29. Expect(pagination.New(3, 2, 2).CurrentPage()).To(Equal(int64(1)))
  30. Expect(pagination.New(-1, 2, 2).CurrentPage()).To(Equal(int64(1)))
  31. Expect(pagination.New(0, 4, 2).CurrentPage()).To(Equal(int64(1)))
  32. Expect(pagination.New(1, 4, 2).CurrentPage()).To(Equal(int64(1)))
  33. Expect(pagination.New(2, 4, 2).CurrentPage()).To(Equal(int64(2)))
  34. Expect(pagination.New(3, 4, 2).CurrentPage()).To(Equal(int64(2)))
  35. Expect(pagination.New(-1, 4, 2).CurrentPage()).To(Equal(int64(1)))
  36. })
  37. })
  38. Context("MaxPages", func() {
  39. It("returns correct value", func() {
  40. Expect(pagination.New(0, 0, 2).MaxPages()).To(Equal(int64(1)))
  41. Expect(pagination.New(0, 1, 2).MaxPages()).To(Equal(int64(1)))
  42. Expect(pagination.New(0, 2, 2).MaxPages()).To(Equal(int64(1)))
  43. Expect(pagination.New(0, 3, 2).MaxPages()).To(Equal(int64(2)))
  44. Expect(pagination.New(0, 4, 2).MaxPages()).To(Equal(int64(2)))
  45. Expect(pagination.New(0, 5, 2).MaxPages()).To(Equal(int64(3)))
  46. Expect(pagination.New(0, 6, 2).MaxPages()).To(Equal(int64(3)))
  47. Expect(pagination.New(0, 7, 2).MaxPages()).To(Equal(int64(4)))
  48. Expect(pagination.New(0, 8, 2).MaxPages()).To(Equal(int64(4)))
  49. Expect(pagination.New(0, 9, 2).MaxPages()).To(Equal(int64(5)))
  50. Expect(pagination.New(0, 10, 2).MaxPages()).To(Equal(int64(5)))
  51. })
  52. })
  53. Context("ResultsCount", func() {
  54. It("returns correct value", func() {
  55. Expect(pagination.New(0, 1, 0).ResultsCount()).To(Equal(int64(1)))
  56. Expect(pagination.New(0, 2, 0).ResultsCount()).To(Equal(int64(2)))
  57. Expect(pagination.New(0, 3, 0).ResultsCount()).To(Equal(int64(3)))
  58. })
  59. })
  60. Context("ResultsPerPage", func() {
  61. It("returns correct value", func() {
  62. Expect(pagination.New(0, 0, 1).ResultsPerPage()).To(Equal(int64(1)))
  63. Expect(pagination.New(0, 0, 2).ResultsPerPage()).To(Equal(int64(2)))
  64. Expect(pagination.New(0, 0, 3).ResultsPerPage()).To(Equal(int64(3)))
  65. })
  66. })
  67. Context("Limit", func() {
  68. It("returns correct value", func() {
  69. limit, offset := pagination.New(1, 10, 2).Limit()
  70. Expect(limit).To(Equal(int64(2)))
  71. Expect(offset).To(Equal(int64(0)))
  72. limit, offset = pagination.New(2, 10, 2).Limit()
  73. Expect(limit).To(Equal(int64(2)))
  74. Expect(offset).To(Equal(int64(2)))
  75. limit, offset = pagination.New(3, 10, 2).Limit()
  76. Expect(limit).To(Equal(int64(2)))
  77. Expect(offset).To(Equal(int64(4)))
  78. limit, offset = pagination.New(4, 10, 2).Limit()
  79. Expect(limit).To(Equal(int64(2)))
  80. Expect(offset).To(Equal(int64(6)))
  81. limit, offset = pagination.New(5, 10, 2).Limit()
  82. Expect(limit).To(Equal(int64(2)))
  83. Expect(offset).To(Equal(int64(8)))
  84. })
  85. })
  86. })
  87. })
  88. func TestSuite(t *testing.T) {
  89. RegisterFailHandler(Fail)
  90. RunSpecs(t, "pagination")
  91. }