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 _, testCase := range testCases {
  27. actualStr := testCase.Input.String()
  28. if actualStr != testCase.ExpectedStr {
  29. t.Logf("Expected: %q", testCase.ExpectedStr)
  30. t.Logf("Actual: %q", actualStr)
  31. t.Fail()
  32. }
  33. if len(testCase.Input.Params) != len(testCase.ExpectedParams) {
  34. t.Errorf("Expected %d parameters; got %d", len(testCase.ExpectedParams), len(testCase.Input.Params))
  35. }
  36. for name, value := range testCase.ExpectedParams {
  37. if testCase.Input.Params[name] == nil {
  38. t.Errorf("Expected parameter %q to be %q; got nil", name, value)
  39. } else if testCase.Input.Params[name] != value {
  40. t.Errorf("Expected parameter %q to be %q; got %q", name, value, testCase.Input.Params[name])
  41. }
  42. }
  43. }
  44. }