query_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // Append with parameters
  13. {
  14. Input: NewQuery().
  15. Append("FOR doc IN @@collection", "recipes").
  16. Append("FILTER doc.title == @title", "Spaghetti").
  17. Append("RETURN doc"),
  18. ExpectedStr: `FOR doc IN @@collection
  19. FILTER doc.title == @title
  20. RETURN doc`,
  21. ExpectedParams: map[string]any{
  22. "collection": "recipes",
  23. "title": "Spaghetti",
  24. },
  25. },
  26. // Append with too many parameters
  27. {
  28. Input: NewQuery().
  29. Append("FOR doc IN @@collection", "recipes", "ignored").
  30. Append("FILTER doc.title == @title", "Spaghetti", "also ignored").
  31. Append("RETURN doc"),
  32. ExpectedStr: `FOR doc IN @@collection
  33. FILTER doc.title == @title
  34. RETURN doc`,
  35. ExpectedParams: map[string]any{
  36. "collection": "recipes",
  37. "title": "Spaghetti",
  38. },
  39. },
  40. // Append and bind
  41. {
  42. Input: NewQuery().
  43. Append("FOR doc IN @@collection").
  44. Append("FILTER doc.title == @title").
  45. Append("RETURN doc").
  46. Bind("collection", "recipes").
  47. Bind("title", "Spaghetti"),
  48. ExpectedStr: `FOR doc IN @@collection
  49. FILTER doc.title == @title
  50. RETURN doc`,
  51. ExpectedParams: map[string]any{
  52. "collection": "recipes",
  53. "title": "Spaghetti",
  54. },
  55. },
  56. // Append and bind map
  57. {
  58. Input: NewQuery().
  59. Append("FOR doc IN @@collection").
  60. Append("FILTER doc.title == @title").
  61. Append("RETURN doc").
  62. BindMap(map[string]any{"collection": "recipes", "title": "Spaghetti"}),
  63. ExpectedStr: `FOR doc IN @@collection
  64. FILTER doc.title == @title
  65. RETURN doc`,
  66. ExpectedParams: map[string]any{
  67. "collection": "recipes",
  68. "title": "Spaghetti",
  69. },
  70. },
  71. }
  72. for _, tc := range testCases {
  73. t.Logf("Testing %+v", tc.Input)
  74. actualStr := tc.Input.String()
  75. if actualStr != tc.ExpectedStr {
  76. t.Errorf("Expected %q, got %q", tc.ExpectedStr, actualStr)
  77. }
  78. if len(tc.Input.Params) != len(tc.ExpectedParams) {
  79. t.Errorf("Expected %d parameters, got %d", len(tc.ExpectedParams), len(tc.Input.Params))
  80. }
  81. for name, value := range tc.ExpectedParams {
  82. if tc.Input.Params[name] != value {
  83. t.Errorf("Expected parameter %q to be %v; got %v", name, value, tc.Input.Params[name])
  84. }
  85. }
  86. }
  87. }