query_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  78. for _, tc := range testCases {
  79. t.Logf("Testing %+v", tc.Input)
  80. actualStr := tc.Input.String()
  81. if actualStr != tc.ExpectedStr {
  82. t.Errorf("Expected %q, got %q", tc.ExpectedStr, actualStr)
  83. }
  84. if tc.Input.Params == nil {
  85. t.Error("Expected empty slice, got nil")
  86. continue
  87. }
  88. if len(tc.Input.Params) != len(tc.ExpectedParams) {
  89. t.Errorf("Expected %d parameters, got %d", len(tc.ExpectedParams), len(tc.Input.Params))
  90. }
  91. for name, value := range tc.ExpectedParams {
  92. if tc.Input.Params[name] != value {
  93. t.Errorf("Expected parameter %q to be %v; got %v", name, value, tc.Input.Params[name])
  94. }
  95. }
  96. }
  97. }