standardise arango tests
This commit is contained in:
+17
-5
@@ -9,6 +9,13 @@ func TestReadParams(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
testCases := []TestCase{
|
testCases := []TestCase{
|
||||||
|
{
|
||||||
|
Input: "FOR doc IN recipes FILTER doc.title == \"Spaghetti\" RETURN doc",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Input: "FOR doc IN recipes FILTER doc.title == @title RETURN doc",
|
||||||
|
Output: []string{"title"},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Input: "FOR doc IN @@collection FILTER doc.title == @title RETURN doc",
|
Input: "FOR doc IN @@collection FILTER doc.title == @title RETURN doc",
|
||||||
Output: []string{"collection", "title"},
|
Output: []string{"collection", "title"},
|
||||||
@@ -16,19 +23,24 @@ func TestReadParams(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Log("Input:", tc.Input)
|
t.Logf("Testing %q", tc.Input)
|
||||||
t.Log("Expected output:", tc.Output)
|
|
||||||
|
|
||||||
params := ReadParams(tc.Input)
|
params := ReadParams(tc.Input)
|
||||||
|
|
||||||
|
if params == nil {
|
||||||
|
t.Errorf("Expected empty slice, got nil")
|
||||||
|
continue
|
||||||
|
}
|
||||||
if len(params) != len(tc.Output) {
|
if len(params) != len(tc.Output) {
|
||||||
t.Errorf("Expected %d parameters", len(tc.Output))
|
t.Errorf("Expected %d parameters", len(tc.Output))
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, name := range tc.Output {
|
for i, name := range tc.Output {
|
||||||
if params[i] != name {
|
if i == len(params) {
|
||||||
t.Errorf("Expected parameter %d to be %q", i, name)
|
break
|
||||||
|
}
|
||||||
|
if name != params[i] {
|
||||||
|
t.Errorf("Expected %s for parameter %d, got %s", name, i, params[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func (query *Query) Append(line string, values ...any) *Query {
|
|||||||
if len(names) > 0 {
|
if len(names) > 0 {
|
||||||
params = map[string]any{}
|
params = map[string]any{}
|
||||||
for i, name := range names {
|
for i, name := range names {
|
||||||
if i > len(values) {
|
if i == len(values) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
params[name] = values[i]
|
params[name] = values[i]
|
||||||
@@ -33,17 +33,17 @@ func (query *Query) Append(line string, values ...any) *Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
query.Lines = append(query.Lines, line)
|
query.Lines = append(query.Lines, line)
|
||||||
return query.AssignMap(params)
|
return query.BindMap(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign assigns a value to a single bind parameter.
|
// Bind binds a value to a bind parameter.
|
||||||
func (query *Query) Assign(name string, value any) *Query {
|
func (query *Query) Bind(name string, value any) *Query {
|
||||||
query.Params[name] = value
|
query.Params[name] = value
|
||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssignMap assigns values to bind parameters.
|
// BindMap assigns values to bind parameters.
|
||||||
func (query *Query) AssignMap(params map[string]any) *Query {
|
func (query *Query) BindMap(params map[string]any) *Query {
|
||||||
if params != nil {
|
if params != nil {
|
||||||
for name, value := range params {
|
for name, value := range params {
|
||||||
query.Params[name] = value
|
query.Params[name] = value
|
||||||
@@ -64,16 +64,6 @@ func (query *Query) Copy() *Query {
|
|||||||
return newQuery
|
return newQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
// L (for "Line") is a shorthand for Append.
|
|
||||||
func (query *Query) L(line string, values ...any) *Query {
|
|
||||||
return query.Append(line, values...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// P (for "Parameter") is a shorthand for Assign.
|
|
||||||
func (query *Query) P(name string, value any) *Query {
|
|
||||||
return query.Assign(name, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (query *Query) String() string {
|
func (query *Query) String() string {
|
||||||
return strings.Join(query.Lines, "\n")
|
return strings.Join(query.Lines, "\n")
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-8
@@ -12,6 +12,7 @@ func TestQueryAppend(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
testCases := []TestCase{
|
testCases := []TestCase{
|
||||||
|
// Append with parameters
|
||||||
{
|
{
|
||||||
Input: NewQuery().
|
Input: NewQuery().
|
||||||
Append("FOR doc IN @@collection", "recipes").
|
Append("FOR doc IN @@collection", "recipes").
|
||||||
@@ -19,6 +20,51 @@ func TestQueryAppend(t *testing.T) {
|
|||||||
Append("RETURN doc"),
|
Append("RETURN doc"),
|
||||||
ExpectedStr: `FOR doc IN @@collection
|
ExpectedStr: `FOR doc IN @@collection
|
||||||
FILTER doc.title == @title
|
FILTER doc.title == @title
|
||||||
|
RETURN doc`,
|
||||||
|
ExpectedParams: map[string]any{
|
||||||
|
"collection": "recipes",
|
||||||
|
"title": "Spaghetti",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Append with too many parameters
|
||||||
|
{
|
||||||
|
Input: NewQuery().
|
||||||
|
Append("FOR doc IN @@collection", "recipes", "ignored").
|
||||||
|
Append("FILTER doc.title == @title", "Spaghetti", "also ignored").
|
||||||
|
Append("RETURN doc"),
|
||||||
|
ExpectedStr: `FOR doc IN @@collection
|
||||||
|
FILTER doc.title == @title
|
||||||
|
RETURN doc`,
|
||||||
|
ExpectedParams: map[string]any{
|
||||||
|
"collection": "recipes",
|
||||||
|
"title": "Spaghetti",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Append and bind
|
||||||
|
{
|
||||||
|
Input: NewQuery().
|
||||||
|
Append("FOR doc IN @@collection").
|
||||||
|
Append("FILTER doc.title == @title").
|
||||||
|
Append("RETURN doc").
|
||||||
|
Bind("collection", "recipes").
|
||||||
|
Bind("title", "Spaghetti"),
|
||||||
|
ExpectedStr: `FOR doc IN @@collection
|
||||||
|
FILTER doc.title == @title
|
||||||
|
RETURN doc`,
|
||||||
|
ExpectedParams: map[string]any{
|
||||||
|
"collection": "recipes",
|
||||||
|
"title": "Spaghetti",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Append and bind map
|
||||||
|
{
|
||||||
|
Input: NewQuery().
|
||||||
|
Append("FOR doc IN @@collection").
|
||||||
|
Append("FILTER doc.title == @title").
|
||||||
|
Append("RETURN doc").
|
||||||
|
BindMap(map[string]any{"collection": "recipes", "title": "Spaghetti"}),
|
||||||
|
ExpectedStr: `FOR doc IN @@collection
|
||||||
|
FILTER doc.title == @title
|
||||||
RETURN doc`,
|
RETURN doc`,
|
||||||
ExpectedParams: map[string]any{
|
ExpectedParams: map[string]any{
|
||||||
"collection": "recipes",
|
"collection": "recipes",
|
||||||
@@ -28,23 +74,21 @@ RETURN doc`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
t.Logf("Testing %+v", tc.Input)
|
||||||
|
|
||||||
actualStr := tc.Input.String()
|
actualStr := tc.Input.String()
|
||||||
|
|
||||||
if actualStr != tc.ExpectedStr {
|
if actualStr != tc.ExpectedStr {
|
||||||
t.Logf("Expected: %q", tc.ExpectedStr)
|
t.Errorf("Expected %q, got %q", tc.ExpectedStr, actualStr)
|
||||||
t.Logf("Actual: %q", actualStr)
|
|
||||||
t.Fail()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tc.Input.Params) != len(tc.ExpectedParams) {
|
if len(tc.Input.Params) != len(tc.ExpectedParams) {
|
||||||
t.Errorf("Expected %d parameters; got %d", len(tc.ExpectedParams), len(tc.Input.Params))
|
t.Errorf("Expected %d parameters, got %d", len(tc.ExpectedParams), len(tc.Input.Params))
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, value := range tc.ExpectedParams {
|
for name, value := range tc.ExpectedParams {
|
||||||
if tc.Input.Params[name] == nil {
|
if tc.Input.Params[name] != value {
|
||||||
t.Errorf("Expected parameter %q to be %q; got nil", name, value)
|
t.Errorf("Expected parameter %q to be %v; got %v", name, value, tc.Input.Params[name])
|
||||||
} else if tc.Input.Params[name] != value {
|
|
||||||
t.Errorf("Expected parameter %q to be %q; got %q", name, value, tc.Input.Params[name])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user