query_test.go 2.6 KB

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