mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-01-14 09:57:58 -05:00
## Description --- This PR introduces a new tool kind `firestore-query` that enables parameterized querying of Firestore collections with support for Firestore native JSON value types, ensuring proper type handling for complex queries. ### Feature A new Firestore tool that allows: - __Parameterized collection paths, filters, select, orderBy, limit and analyzeQuery__ using Go template syntax - __Native JSON value type support__ for proper type handling in queries - __Complex filter structures__ with AND/OR logical operators - __Dynamic query building__ with template parameter substitution Example usage: <img width="761" height="721" alt="Screenshot 2025-09-09 at 1 21 16 PM" src="https://github.com/user-attachments/assets/bb359ea8-f750-492d-9f13-cef8f3b6bfd1" /> ## PR Checklist --- > Thank you for opening a Pull Request! Before submitting your PR, there are a > few things you can do to make sure it goes smoothly: - [x] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md) - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/langchain-google-alloydb-pg-python/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) - [x] Make sure to add `!` if this involve a breaking change
298 lines
8.0 KiB
Go
298 lines
8.0 KiB
Go
// Copyright 2025 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package tools_test
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"text/template"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/googleapis/genai-toolbox/internal/tools"
|
|
)
|
|
|
|
func TestPopulateTemplate(t *testing.T) {
|
|
tcs := []struct {
|
|
name string
|
|
templateName string
|
|
templateString string
|
|
data map[string]any
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "simple string substitution",
|
|
templateName: "test",
|
|
templateString: "Hello {{.name}}!",
|
|
data: map[string]any{"name": "World"},
|
|
want: "Hello World!",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "multiple substitutions",
|
|
templateName: "test",
|
|
templateString: "{{.greeting}} {{.name}}, you are {{.age}} years old",
|
|
data: map[string]any{"greeting": "Hello", "name": "Alice", "age": 30},
|
|
want: "Hello Alice, you are 30 years old",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "empty template",
|
|
templateName: "test",
|
|
templateString: "",
|
|
data: map[string]any{},
|
|
want: "",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "no substitutions",
|
|
templateName: "test",
|
|
templateString: "Plain text without templates",
|
|
data: map[string]any{},
|
|
want: "Plain text without templates",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "invalid template syntax",
|
|
templateName: "test",
|
|
templateString: "{{.name",
|
|
data: map[string]any{"name": "World"},
|
|
want: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "missing field",
|
|
templateName: "test",
|
|
templateString: "{{.missing}}",
|
|
data: map[string]any{"name": "World"},
|
|
want: "<no value>",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "invalid function call",
|
|
templateName: "test",
|
|
templateString: "{{.name.invalid}}",
|
|
data: map[string]any{"name": "World"},
|
|
want: "",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tcs {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, err := tools.PopulateTemplate(tc.templateName, tc.templateString, tc.data)
|
|
if tc.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %s", err)
|
|
}
|
|
if diff := cmp.Diff(tc.want, got); diff != "" {
|
|
t.Fatalf("incorrect result (-want +got):\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPopulateTemplateWithFunc(t *testing.T) {
|
|
// Custom function for testing
|
|
customFuncs := template.FuncMap{
|
|
"upper": strings.ToUpper,
|
|
"add": func(a, b int) int {
|
|
return a + b
|
|
},
|
|
}
|
|
|
|
tcs := []struct {
|
|
name string
|
|
templateName string
|
|
templateString string
|
|
data map[string]any
|
|
funcMap template.FuncMap
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "with custom upper function",
|
|
templateName: "test",
|
|
templateString: "{{upper .text}}",
|
|
data: map[string]any{"text": "hello"},
|
|
funcMap: customFuncs,
|
|
want: "HELLO",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "with custom add function",
|
|
templateName: "test",
|
|
templateString: "Result: {{add .x .y}}",
|
|
data: map[string]any{"x": 5, "y": 3},
|
|
funcMap: customFuncs,
|
|
want: "Result: 8",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "nil funcMap",
|
|
templateName: "test",
|
|
templateString: "Hello {{.name}}",
|
|
data: map[string]any{"name": "World"},
|
|
funcMap: nil,
|
|
want: "Hello World",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "combine custom function with regular substitution",
|
|
templateName: "test",
|
|
templateString: "{{upper .greeting}} {{.name}}!",
|
|
data: map[string]any{"greeting": "hello", "name": "Alice"},
|
|
funcMap: customFuncs,
|
|
want: "HELLO Alice!",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "undefined function",
|
|
templateName: "test",
|
|
templateString: "{{undefined .text}}",
|
|
data: map[string]any{"text": "hello"},
|
|
funcMap: nil,
|
|
want: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "wrong number of arguments",
|
|
templateName: "test",
|
|
templateString: "{{upper}}",
|
|
data: map[string]any{},
|
|
funcMap: template.FuncMap{"upper": strings.ToUpper},
|
|
want: "",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tcs {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, err := tools.PopulateTemplateWithFunc(tc.templateName, tc.templateString, tc.data, tc.funcMap)
|
|
if tc.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %s", err)
|
|
}
|
|
if diff := cmp.Diff(tc.want, got); diff != "" {
|
|
t.Fatalf("incorrect result (-want +got):\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPopulateTemplateWithJSON(t *testing.T) {
|
|
tcs := []struct {
|
|
name string
|
|
templateName string
|
|
templateString string
|
|
data map[string]any
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "json string",
|
|
templateName: "test",
|
|
templateString: "Data: {{json .value}}",
|
|
data: map[string]any{"value": "hello"},
|
|
want: `Data: "hello"`,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "json number",
|
|
templateName: "test",
|
|
templateString: "Number: {{json .num}}",
|
|
data: map[string]any{"num": 42},
|
|
want: "Number: 42",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "json boolean",
|
|
templateName: "test",
|
|
templateString: "Bool: {{json .flag}}",
|
|
data: map[string]any{"flag": true},
|
|
want: "Bool: true",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "json array",
|
|
templateName: "test",
|
|
templateString: "Array: {{json .items}}",
|
|
data: map[string]any{"items": []any{"a", "b", "c"}},
|
|
want: `Array: ["a","b","c"]`,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "json object",
|
|
templateName: "test",
|
|
templateString: "Object: {{json .obj}}",
|
|
data: map[string]any{"obj": map[string]any{"name": "Alice", "age": 30}},
|
|
want: `Object: {"age":30,"name":"Alice"}`,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "json null",
|
|
templateName: "test",
|
|
templateString: "Null: {{json .nullValue}}",
|
|
data: map[string]any{"nullValue": nil},
|
|
want: "Null: null",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "combine json with regular substitution",
|
|
templateName: "test",
|
|
templateString: "User {{.name}} has data: {{json .data}}",
|
|
data: map[string]any{"name": "Bob", "data": map[string]any{"id": 123}},
|
|
want: `User Bob has data: {"id":123}`,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "missing field for json",
|
|
templateName: "test",
|
|
templateString: "{{json .missing}}",
|
|
data: map[string]any{},
|
|
want: "null",
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tcs {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, err := tools.PopulateTemplateWithJSON(tc.templateName, tc.templateString, tc.data)
|
|
if tc.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %s", err)
|
|
}
|
|
if diff := cmp.Diff(tc.want, got); diff != "" {
|
|
t.Fatalf("incorrect result (-want +got):\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|