page_test.go 3.2 KB

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