2023-10-07 10:22:56 +01:00
|
|
|
package arango
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestQueryAppend(t *testing.T) {
|
|
|
|
|
type TestCase struct {
|
|
|
|
|
Input *Query
|
|
|
|
|
ExpectedStr string
|
|
|
|
|
ExpectedParams map[string]any
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testCases := []TestCase{
|
|
|
|
|
{
|
|
|
|
|
Input: NewQuery().
|
|
|
|
|
Append("FOR doc IN @@collection", "recipes").
|
|
|
|
|
Append("FILTER doc.title == @title", "Spaghetti").
|
|
|
|
|
Append("RETURN doc"),
|
|
|
|
|
ExpectedStr: `FOR doc IN @@collection
|
|
|
|
|
FILTER doc.title == @title
|
|
|
|
|
RETURN doc`,
|
|
|
|
|
ExpectedParams: map[string]any{
|
|
|
|
|
"collection": "recipes",
|
|
|
|
|
"title": "Spaghetti",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-07 10:44:47 +01:00
|
|
|
for _, tc := range testCases {
|
|
|
|
|
actualStr := tc.Input.String()
|
2023-10-07 10:22:56 +01:00
|
|
|
|
2023-10-07 10:44:47 +01:00
|
|
|
if actualStr != tc.ExpectedStr {
|
|
|
|
|
t.Logf("Expected: %q", tc.ExpectedStr)
|
2023-10-07 10:22:56 +01:00
|
|
|
t.Logf("Actual: %q", actualStr)
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-07 10:44:47 +01:00
|
|
|
if len(tc.Input.Params) != len(tc.ExpectedParams) {
|
|
|
|
|
t.Errorf("Expected %d parameters; got %d", len(tc.ExpectedParams), len(tc.Input.Params))
|
2023-10-07 10:22:56 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-07 10:44:47 +01:00
|
|
|
for name, value := range tc.ExpectedParams {
|
|
|
|
|
if tc.Input.Params[name] == nil {
|
2023-10-07 10:22:56 +01:00
|
|
|
t.Errorf("Expected parameter %q to be %q; got nil", name, value)
|
2023-10-07 10:44:47 +01:00
|
|
|
} else if tc.Input.Params[name] != value {
|
|
|
|
|
t.Errorf("Expected parameter %q to be %q; got %q", name, value, tc.Input.Params[name])
|
2023-10-07 10:22:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|