Ai
1 Star 0 Fork 0

idsutong/gqlgen

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
context_response_test.go 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
Adam 提交于 2020-02-17 07:27 +08:00 . bump to gqlparser v2
package graphql
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
)
func TestAddError(t *testing.T) {
ctx := WithResponseContext(context.Background(), DefaultErrorPresenter, nil)
root := &FieldContext{
Field: CollectedField{
Field: &ast.Field{
Alias: "foo",
},
},
}
ctx = WithFieldContext(ctx, root)
AddError(ctx, errors.New("foo1"))
AddError(ctx, errors.New("foo2"))
index := 1
child := &FieldContext{
Parent: root,
Index: &index,
}
userProvidedPath := &FieldContext{
Parent: child,
Field: CollectedField{
Field: &ast.Field{
Alias: "works",
},
},
}
ctx = WithFieldContext(ctx, child)
AddError(ctx, errors.New("bar"))
AddError(ctx, &gqlerror.Error{
Message: "foo3",
Path: append(child.Path(), ast.PathName("works")),
})
specs := []struct {
Name string
RCtx *FieldContext
Messages []string
}{
{
Name: "with root FieldContext",
RCtx: root,
Messages: []string{"foo1", "foo2"},
},
{
Name: "with child FieldContext",
RCtx: child,
Messages: []string{"bar"},
},
{
Name: "with user provided path",
RCtx: userProvidedPath,
Messages: []string{"foo3"},
},
}
for _, spec := range specs {
t.Run(spec.Name, func(t *testing.T) {
errList := GetFieldErrors(ctx, spec.RCtx)
require.Len(t, errList, len(spec.Messages))
for idx, err := range errList {
assert.Equal(t, spec.Messages[idx], err.Message)
}
})
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/idsutong/gqlgen.git
git@gitee.com:idsutong/gqlgen.git
idsutong
gqlgen
gqlgen
master

搜索帮助