query_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package arango
  2. import (
  3. "testing"
  4. )
  5. func TestQueryAppend(t *testing.T) {
  6. type TestCase struct {
  7. Input *Query
  8. ExpectedStr string
  9. ExpectedParams map[string]any
  10. }
  11. testCases := []TestCase{
  12. {
  13. Input: NewQuery().
  14. Append("FOR doc IN @@collection", "recipes").
  15. Append("FILTER doc.title == @title", "Spaghetti").
  16. Append("RETURN doc"),
  17. ExpectedStr: `FOR doc IN @@collection
  18. FILTER doc.title == @title
  19. RETURN doc`,
  20. ExpectedParams: map[string]any{
  21. "collection": "recipes",
  22. "title": "Spaghetti",
  23. },
  24. },
  25. }
  26. for _, tc := range testCases {
  27. actualStr := tc.Input.String()
  28. if actualStr != tc.ExpectedStr {
  29. t.Logf("Expected: %q", tc.ExpectedStr)
  30. t.Logf("Actual: %q", actualStr)
  31. t.Fail()
  32. }
  33. if len(tc.Input.Params) != len(tc.ExpectedParams) {
  34. t.Errorf("Expected %d parameters; got %d", len(tc.ExpectedParams), len(tc.Input.Params))
  35. }
  36. for name, value := range tc.ExpectedParams {
  37. if tc.Input.Params[name] == nil {
  38. t.Errorf("Expected parameter %q to be %q; got nil", name, value)
  39. } else if tc.Input.Params[name] != value {
  40. t.Errorf("Expected parameter %q to be %q; got %q", name, value, tc.Input.Params[name])
  41. }
  42. }
  43. }
  44. }