sort_test.go 1.1 KB

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