pagination_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. })
  68. })
  69. func TestSuite(t *testing.T) {
  70. RegisterFailHandler(Fail)
  71. RunSpecs(t, "pagination")
  72. }