1
0

query_test.go 2.9 KB

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