Ai
1 Star 0 Fork 0

idsutong/gqlgen

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
context_operation_test.go 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
Adam 提交于 2020-02-20 10:02 +08:00 . Add function to check presense of operation context
package graphql
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2/ast"
)
func TestGetOperationContext(t *testing.T) {
rc := &OperationContext{}
t.Run("with operation context", func(t *testing.T) {
ctx := WithOperationContext(context.Background(), rc)
require.True(t, HasOperationContext(ctx))
require.Equal(t, rc, GetOperationContext(ctx))
})
t.Run("without operation context", func(t *testing.T) {
ctx := context.Background()
require.False(t, HasOperationContext(ctx))
require.Panics(t, func() {
GetOperationContext(ctx)
})
})
}
func TestCollectAllFields(t *testing.T) {
t.Run("collect fields", func(t *testing.T) {
ctx := testContext(ast.SelectionSet{
&ast.Field{
Name: "field",
},
})
s := CollectAllFields(ctx)
require.Equal(t, []string{"field"}, s)
})
t.Run("unique field names", func(t *testing.T) {
ctx := testContext(ast.SelectionSet{
&ast.Field{
Name: "field",
},
&ast.Field{
Name: "field",
Alias: "field alias",
},
})
s := CollectAllFields(ctx)
require.Equal(t, []string{"field"}, s)
})
t.Run("collect fragments", func(t *testing.T) {
ctx := testContext(ast.SelectionSet{
&ast.Field{
Name: "fieldA",
},
&ast.InlineFragment{
TypeCondition: "ExampleTypeA",
SelectionSet: ast.SelectionSet{
&ast.Field{
Name: "fieldA",
},
},
},
&ast.InlineFragment{
TypeCondition: "ExampleTypeB",
SelectionSet: ast.SelectionSet{
&ast.Field{
Name: "fieldB",
},
},
},
})
s := CollectAllFields(ctx)
require.Equal(t, []string{"fieldA", "fieldB"}, s)
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/idsutong/gqlgen.git
git@gitee.com:idsutong/gqlgen.git
idsutong
gqlgen
gqlgen
master

搜索帮助