You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.1 KiB

  1. package core
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestPaginationPage(t *testing.T) {
  8. cases := []struct {
  9. totalCount int
  10. perPage int
  11. page int
  12. newPage int
  13. }{
  14. {0, 0, 1, 1},
  15. {0, 10, 0, 1},
  16. {0, 10, 1, 1},
  17. {0, 10, 2, 1},
  18. {5, 10, -1, 1},
  19. {5, 10, 0, 1},
  20. {5, 10, 1, 1},
  21. {5, 10, 2, 1},
  22. {5, 10, 2, 1},
  23. {5, 5, 1, 1},
  24. {5, 5, 2, 1},
  25. {5, 5, 3, 1},
  26. {5, 3, 2, 2},
  27. {5, 3, 3, 2},
  28. {5, 2, 2, 2},
  29. {5, 2, 3, 3},
  30. {5, 2, 4, 3},
  31. }
  32. for _, c := range cases {
  33. p := validatePage(c.page, c.perPage, c.totalCount)
  34. assert.Equal(t, c.newPage, p, fmt.Sprintf("%v", c))
  35. }
  36. }
  37. func TestPaginationPerPage(t *testing.T) {
  38. cases := []struct {
  39. totalCount int
  40. perPage int
  41. newPerPage int
  42. }{
  43. {5, 0, defaultPerPage},
  44. {5, 1, 1},
  45. {5, 2, 2},
  46. {5, defaultPerPage, defaultPerPage},
  47. {5, maxPerPage - 1, maxPerPage - 1},
  48. {5, maxPerPage, maxPerPage},
  49. {5, maxPerPage + 1, defaultPerPage},
  50. }
  51. for _, c := range cases {
  52. p := validatePerPage(c.perPage)
  53. assert.Equal(t, c.newPerPage, p, fmt.Sprintf("%v", c))
  54. }
  55. }