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.

67 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, 10, 0, 1},
  15. {0, 10, 1, 1},
  16. {0, 10, 2, 1},
  17. {5, 10, -1, 1},
  18. {5, 10, 0, 1},
  19. {5, 10, 1, 1},
  20. {5, 10, 2, 1},
  21. {5, 10, 2, 1},
  22. {5, 5, 1, 1},
  23. {5, 5, 2, 1},
  24. {5, 5, 3, 1},
  25. {5, 3, 2, 2},
  26. {5, 3, 3, 2},
  27. {5, 2, 2, 2},
  28. {5, 2, 3, 3},
  29. {5, 2, 4, 3},
  30. }
  31. for _, c := range cases {
  32. p := validatePage(c.page, c.perPage, c.totalCount)
  33. assert.Equal(t, c.newPage, p, fmt.Sprintf("%v", c))
  34. }
  35. }
  36. func TestPaginationPerPage(t *testing.T) {
  37. cases := []struct {
  38. totalCount int
  39. perPage int
  40. newPerPage int
  41. }{
  42. {5, 0, defaultPerPage},
  43. {5, 1, 1},
  44. {5, 2, 2},
  45. {5, defaultPerPage, defaultPerPage},
  46. {5, maxPerPage - 1, maxPerPage - 1},
  47. {5, maxPerPage, maxPerPage},
  48. {5, maxPerPage + 1, defaultPerPage},
  49. }
  50. for _, c := range cases {
  51. p := validatePerPage(c.perPage)
  52. assert.Equal(t, c.newPerPage, p, fmt.Sprintf("%v", c))
  53. }
  54. }