feat: add progressive Eino examples and unify weather provider
This commit is contained in:
1 parent
73f6f0901e
commit
1373f0903f
18 files changed
+893
-380
No files matched your search
+27
-62
@@ -2,84 +2,49 @@ package tool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"example.com/ueino/pkg/model"
|
||||
)
|
||||
|
||||
func TestWeatherExecute(t *testing.T) {
|
||||
location := "北京 海淀/?&"
|
||||
func TestWeatherDefaultsToToday(t *testing.T) {
|
||||
today := time.Now().In(time.FixedZone("Asia/Shanghai", 8*60*60)).Format(time.DateOnly)
|
||||
calls := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
if r.Method != http.MethodGet || r.URL.EscapedPath() != "/"+url.PathEscape(location) {
|
||||
t.Errorf("地点编码不正确: %s", r.URL)
|
||||
if r.URL.Path == "/search" {
|
||||
if r.URL.Query().Get("name") != "北京" {
|
||||
t.Errorf("地点参数错误: %s", r.URL)
|
||||
}
|
||||
fmt.Fprint(w, `{"results":[{"name":"北京","country":"中国","latitude":39.9,"longitude":116.4}]}`)
|
||||
return
|
||||
}
|
||||
query := r.URL.Query()
|
||||
if query.Get("format") != "3" || query.Get("lang") != "zh" || !query.Has("m") {
|
||||
t.Errorf("查询参数不正确: %v", query)
|
||||
if r.URL.Path != "/forecast" || r.URL.Query().Get("start_date") != today || r.URL.Query().Get("end_date") != today {
|
||||
t.Errorf("未指定日期时应查询北京时间今天: %s", r.URL)
|
||||
}
|
||||
fmt.Fprint(w, "北京: 晴 +20°C\n")
|
||||
json.NewEncoder(w).Encode(map[string]any{"daily": map[string]any{
|
||||
"time": []string{today}, "weather_code": []int{0},
|
||||
"temperature_2m_min": []int{10}, "temperature_2m_max": []int{20},
|
||||
"precipitation_probability_max": []int{0},
|
||||
}})
|
||||
}))
|
||||
defer server.Close()
|
||||
weather := NewWeather()
|
||||
weather.baseURL = server.URL
|
||||
got, err := weather.Execute(context.Background(), `{"location":" 北京 海淀/?& "}`)
|
||||
if err != nil || got != "北京: 晴 +20°C" {
|
||||
t.Fatalf("天气 = %q,错误 = %v", got, err)
|
||||
weather.geocodingURL, weather.forecastURL = server.URL+"/search", server.URL+"/forecast"
|
||||
result, err := weather.Execute(context.Background(), `{"location":" 北京 "}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, arguments := range []string{`{`, `{}`, `null`, `{"location":" "}`, `{"location":123}`} {
|
||||
if _, err := weather.Execute(context.Background(), arguments); err == nil {
|
||||
t.Fatalf("参数 %s 应报错", arguments)
|
||||
}
|
||||
var forecast model.WeatherForecast
|
||||
if err := json.Unmarshal([]byte(result), &forecast); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatalf("无效参数不应请求接口,实际请求 %d 次", calls)
|
||||
}
|
||||
definition := weather.Definition()
|
||||
if definition.Type != "function" || definition.Function.Name != "get_weather" {
|
||||
t.Fatalf("工具定义不正确: %+v", definition)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWeatherResponseErrors(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
status int
|
||||
body string
|
||||
}{
|
||||
{name: "接口错误", status: 503, body: "unavailable"},
|
||||
{name: "空结果", status: 200, body: " \n"},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(tt.status); fmt.Fprint(w, tt.body) }))
|
||||
defer server.Close()
|
||||
weather := NewWeather()
|
||||
weather.baseURL = server.URL
|
||||
if _, err := weather.Execute(context.Background(), `{"location":"北京"}`); err == nil {
|
||||
t.Fatal("期望返回错误")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWeatherCancellationAndTimeout(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { <-r.Context().Done() }))
|
||||
defer server.Close()
|
||||
weather := NewWeather()
|
||||
weather.baseURL = server.URL
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if _, err := weather.Execute(ctx, `{"location":"北京"}`); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("取消错误 = %v", err)
|
||||
}
|
||||
weather.client.Timeout = 50 * time.Millisecond
|
||||
if _, err := weather.Execute(context.Background(), `{"location":"北京"}`); err == nil || !strings.Contains(err.Error(), "Client.Timeout") {
|
||||
t.Fatalf("超时错误 = %v", err)
|
||||
if calls != 2 || len(forecast.Days) != 1 || forecast.Days[0].Date != today || forecast.Days[0].Condition != "晴" {
|
||||
t.Fatalf("默认查询结果不正确: %+v,请求次数=%d", forecast, calls)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user