Explorar o código

standardise arango tests

Aneurin Barker Snook hai 1 ano
pai
achega
1fa7101a70
Modificáronse 3 ficheiros con 75 adicións e 29 borrados
  1. 17 5
      params_test.go
  2. 6 16
      query.go
  3. 52 8
      query_test.go

+ 17 - 5
params_test.go

@@ -9,6 +9,13 @@ func TestReadParams(t *testing.T) {
 	}
 
 	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",
 			Output: []string{"collection", "title"},
@@ -16,19 +23,24 @@ func TestReadParams(t *testing.T) {
 	}
 
 	for _, tc := range testCases {
-		t.Log("Input:", tc.Input)
-		t.Log("Expected output:", tc.Output)
+		t.Logf("Testing %q", tc.Input)
 
 		params := ReadParams(tc.Input)
 
+		if params == nil {
+			t.Errorf("Expected empty slice, got nil")
+			continue
+		}
 		if len(params) != len(tc.Output) {
 			t.Errorf("Expected %d parameters", len(tc.Output))
-			break
 		}
 
 		for i, name := range tc.Output {
-			if params[i] != name {
-				t.Errorf("Expected parameter %d to be %q", i, name)
+			if i == len(params) {
+				break
+			}
+			if name != params[i] {
+				t.Errorf("Expected %s for parameter %d, got %s", name, i, params[i])
 			}
 		}
 	}

+ 6 - 16
query.go

@@ -25,7 +25,7 @@ func (query *Query) Append(line string, values ...any) *Query {
 	if len(names) > 0 {
 		params = map[string]any{}
 		for i, name := range names {
-			if i > len(values) {
+			if i == len(values) {
 				break
 			}
 			params[name] = values[i]
@@ -33,17 +33,17 @@ func (query *Query) Append(line string, values ...any) *Query {
 	}
 
 	query.Lines = append(query.Lines, line)
-	return query.AssignMap(params)
+	return query.BindMap(params)
 }
 
-// Assign assigns a value to a single bind parameter.
-func (query *Query) Assign(name string, value any) *Query {
+// Bind binds a value to a bind parameter.
+func (query *Query) Bind(name string, value any) *Query {
 	query.Params[name] = value
 	return query
 }
 
-// AssignMap assigns values to bind parameters.
-func (query *Query) AssignMap(params map[string]any) *Query {
+// BindMap assigns values to bind parameters.
+func (query *Query) BindMap(params map[string]any) *Query {
 	if params != nil {
 		for name, value := range params {
 			query.Params[name] = value
@@ -64,16 +64,6 @@ func (query *Query) Copy() *Query {
 	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 {
 	return strings.Join(query.Lines, "\n")
 }

+ 52 - 8
query_test.go

@@ -12,6 +12,7 @@ func TestQueryAppend(t *testing.T) {
 	}
 
 	testCases := []TestCase{
+		// Append with parameters
 		{
 			Input: NewQuery().
 				Append("FOR doc IN @@collection", "recipes").
@@ -19,6 +20,51 @@ func TestQueryAppend(t *testing.T) {
 				Append("RETURN doc"),
 			ExpectedStr: `FOR doc IN @@collection
 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`,
 			ExpectedParams: map[string]any{
 				"collection": "recipes",
@@ -28,23 +74,21 @@ RETURN doc`,
 	}
 
 	for _, tc := range testCases {
+		t.Logf("Testing %+v", tc.Input)
+
 		actualStr := tc.Input.String()
 
 		if actualStr != tc.ExpectedStr {
-			t.Logf("Expected: %q", tc.ExpectedStr)
-			t.Logf("Actual: %q", actualStr)
-			t.Fail()
+			t.Errorf("Expected %q, got %q", tc.ExpectedStr, actualStr)
 		}
 
 		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 {
-			if tc.Input.Params[name] == nil {
-				t.Errorf("Expected parameter %q to be %q; got nil", name, value)
-			} else if tc.Input.Params[name] != value {
-				t.Errorf("Expected parameter %q to be %q; got %q", name, value, tc.Input.Params[name])
+			if tc.Input.Params[name] != value {
+				t.Errorf("Expected parameter %q to be %v; got %v", name, value, tc.Input.Params[name])
 			}
 		}
 	}