1
0

sort_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package qs
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. func TestReadSorts(t *testing.T) {
  7. type TestCase struct {
  8. Input string
  9. Opt *ReadSortsOptions
  10. Output []Sort
  11. Err error
  12. }
  13. testCases := []TestCase{
  14. {Input: ""},
  15. {
  16. Input: "sort=title asc",
  17. Output: []Sort{
  18. {Field: "title", Direction: "asc"},
  19. },
  20. },
  21. {
  22. Input: "sort=title asc&sort=serves asc",
  23. Output: []Sort{
  24. {Field: "title", Direction: "asc"},
  25. {Field: "serves", Direction: "asc"},
  26. },
  27. },
  28. }
  29. for n, tc := range testCases {
  30. t.Logf("(%d) Testing %q with options %+v", n, tc.Input, tc.Opt)
  31. sorts, err := ReadStringSorts(tc.Input, nil)
  32. if !errors.Is(err, tc.Err) {
  33. t.Errorf("Expected error %v, got %v", tc.Err, err)
  34. }
  35. if tc.Err != nil {
  36. continue
  37. }
  38. if tc.Output == nil && sorts != nil {
  39. t.Error("Expected nil")
  40. continue
  41. }
  42. if len(sorts) != len(tc.Output) {
  43. t.Errorf("Expected %d sorts, got %d", len(tc.Output), len(sorts))
  44. }
  45. for i, sort := range tc.Output {
  46. if i == len(sorts) {
  47. break
  48. }
  49. if sort != sorts[i] {
  50. t.Errorf("Expected %+v for sort %d, got %+v", sort, i, sorts[i])
  51. }
  52. }
  53. }
  54. }