page_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package qs
  2. import "testing"
  3. func TestReadPage(t *testing.T) {
  4. type TestCase struct {
  5. Input string
  6. Opt *ReadPageOptions
  7. Output *Page
  8. Err error
  9. }
  10. testCases := []TestCase{
  11. {
  12. Input: "",
  13. Output: &Page{
  14. Pagination: &Pagination{},
  15. },
  16. },
  17. {
  18. Input: "limit=10&page=2",
  19. Output: &Page{
  20. Pagination: &Pagination{Limit: 10, Offset: 10, Page: 2},
  21. },
  22. },
  23. {
  24. Input: "limit=10&page=2&filter=title eq Spaghetti",
  25. Output: &Page{
  26. Pagination: &Pagination{Limit: 10, Offset: 10, Page: 2},
  27. Filters: []Filter{
  28. {Field: "title", Operator: "eq", Value: "Spaghetti"},
  29. },
  30. },
  31. },
  32. {
  33. Input: "limit=10&page=2&filter=title eq Spaghetti&sort=serves desc",
  34. Output: &Page{
  35. Pagination: &Pagination{Limit: 10, Offset: 10, Page: 2},
  36. Filters: []Filter{
  37. {Field: "title", Operator: "eq", Value: "Spaghetti"},
  38. },
  39. Sorts: []Sort{
  40. {Field: "serves", Direction: "desc"},
  41. },
  42. },
  43. },
  44. {
  45. Input: "limit=10&page=2&filter=title eq Spaghetti&sort=serves desc&join=author",
  46. Output: &Page{
  47. Pagination: &Pagination{Limit: 10, Offset: 10, Page: 2},
  48. Filters: []Filter{
  49. {Field: "title", Operator: "eq", Value: "Spaghetti"},
  50. },
  51. Sorts: []Sort{
  52. {Field: "serves", Direction: "desc"},
  53. },
  54. Joins: Joins{"author": true},
  55. },
  56. },
  57. }
  58. for n, tc := range testCases {
  59. t.Logf("(%d) Testing %q with options %+v", n, tc.Input, tc.Opt)
  60. page, err := ReadStringPage(tc.Input, tc.Opt)
  61. if err != tc.Err {
  62. t.Errorf("Expected error %v, got %v", tc.Err, err)
  63. }
  64. if tc.Err != nil {
  65. continue
  66. }
  67. if tc.Output == nil && page != nil {
  68. t.Error("Expected nil")
  69. continue
  70. }
  71. // Compare pagination (see pagination_test.go)
  72. if *page.Pagination != *tc.Output.Pagination {
  73. t.Errorf("Expected %+v for pagination, got %+v", tc.Output, page.Pagination)
  74. }
  75. // Compare filters (see filter_test.go)
  76. if tc.Output.Filters == nil && page.Filters != nil {
  77. t.Error("Expected nil filters")
  78. }
  79. if len(page.Filters) != len(tc.Output.Filters) {
  80. t.Errorf("Expected %d filters, got %d", len(tc.Output.Filters), len(page.Filters))
  81. }
  82. for i, filter := range tc.Output.Filters {
  83. if i == len(page.Filters) {
  84. break
  85. }
  86. if filter != page.Filters[i] {
  87. t.Errorf("Expected %+v for filter %d, got %+v", filter, i, page.Filters[i])
  88. }
  89. }
  90. // Compare sorts (see sort_test.go)
  91. if tc.Output.Sorts == nil && page.Sorts != nil {
  92. t.Error("Expected nil sorts")
  93. }
  94. if len(page.Sorts) != len(tc.Output.Sorts) {
  95. t.Errorf("Expected %d sorts, got %d", len(tc.Output.Sorts), len(page.Sorts))
  96. }
  97. for i, sort := range tc.Output.Sorts {
  98. if i == len(page.Sorts) {
  99. break
  100. }
  101. if sort != page.Sorts[i] {
  102. t.Errorf("Expected %+v for sort %d, got %+v", sort, i, page.Sorts[i])
  103. }
  104. }
  105. // Compare joins (see join_test.go)
  106. if tc.Output.Joins == nil && page.Joins != nil {
  107. t.Error("Expected nil sorts")
  108. }
  109. if len(page.Joins) != len(tc.Output.Joins) {
  110. t.Errorf("Expected %d joins, got %d", len(tc.Output.Joins), len(page.Joins))
  111. }
  112. for name, join := range tc.Output.Joins {
  113. if join != page.Joins[name] {
  114. t.Errorf("Expected %t for join %s, got %t", join, name, page.Joins[name])
  115. }
  116. }
  117. }
  118. }