join_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package qs
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. func TestReadJoins(t *testing.T) {
  7. type TestCase struct {
  8. Input string
  9. Opt *ReadJoinsOptions
  10. Output Joins
  11. Err error
  12. }
  13. testCases := []TestCase{
  14. {Input: ""},
  15. {
  16. Input: "join=ingredient",
  17. Output: Joins{"ingredient": true},
  18. },
  19. {
  20. Input: "join=author&join=ingredient",
  21. Output: Joins{"author": true, "ingredient": true},
  22. },
  23. }
  24. for n, tc := range testCases {
  25. t.Logf("(%d) Testing %q with options %+v", n, tc.Input, tc.Opt)
  26. joins, err := ReadStringJoins(tc.Input, nil)
  27. if !errors.Is(err, tc.Err) {
  28. t.Errorf("Expected error %v, got %v", tc.Err, err)
  29. }
  30. if tc.Err != nil {
  31. continue
  32. }
  33. if tc.Output == nil && joins != nil {
  34. t.Error("Expected nil")
  35. continue
  36. }
  37. if len(joins) != len(tc.Output) {
  38. t.Errorf("Expected %d joins, got %d", len(tc.Output), len(joins))
  39. }
  40. for name, join := range tc.Output {
  41. if join != joins[name] {
  42. t.Errorf("Expected %t for join %s, got %t", join, name, joins[name])
  43. }
  44. }
  45. }
  46. }