-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_misuse_test.go
More file actions
88 lines (77 loc) · 2.57 KB
/
Copy pathcommon_misuse_test.go
File metadata and controls
88 lines (77 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package test_error
import (
"strings"
"testing"
graphql "github.com/lascyb/struct-to-graphql"
)
// UnionAnonymousMemberError union 分支使用匿名结构体(未指定 type)应报错
type UnionAnonymousMemberError struct {
Typename string `graphql:"__typename,union"`
Item struct {
Name string `graphql:"name"`
}
}
type UnionAnonymousMemberQuery struct {
Content UnionAnonymousMemberError `graphql:"content"`
}
func TestCommonMisuse_UnionAnonymousMemberShouldFail(t *testing.T) {
_, err := graphql.Marshal(UnionAnonymousMemberQuery{})
if err == nil {
t.Fatal("expected error for anonymous struct in union member, got nil")
}
if !strings.Contains(err.Error(), "embedded struct field") && !strings.Contains(err.Error(), "named struct type") {
t.Fatalf("unexpected error: %v", err)
}
}
// VariableTypeConflictError 同名变量类型冲突应报错
type VariableTypeConflictError struct {
Products []struct {
ID string `graphql:"id"`
} `graphql:"products(author:$author:Int!)"`
Contents []struct {
ID string `graphql:"id"`
} `graphql:"contents(author:$author:String!)"`
}
func TestCommonMisuse_VariableTypeConflictShouldFail(t *testing.T) {
_, err := graphql.Marshal(VariableTypeConflictError{})
if err == nil {
t.Fatal("expected variable type conflict error, got nil")
}
if !strings.Contains(err.Error(), "类型不统一") && !strings.Contains(err.Error(), "author") {
t.Fatalf("unexpected error: %v", err)
}
}
// EmptyLiteralArgumentError 参数字面量为空应报错
type EmptyLiteralArgumentError struct {
Items []struct {
ID string `graphql:"id"`
} `graphql:"items(query::String!)"`
}
func TestCommonMisuse_EmptyLiteralArgumentShouldFail(t *testing.T) {
_, err := graphql.Marshal(EmptyLiteralArgumentError{})
if err == nil {
t.Fatal("expected empty literal argument error, got nil")
}
if !strings.Contains(err.Error(), "参数值不能定义为空") && !strings.Contains(err.Error(), "unexpected token") {
t.Fatalf("unexpected error: %v", err)
}
}
// MissingVariableTypeError 变量未定义类型,组装完整 Query 时应报错
type MissingVariableTypeError struct {
Items []struct {
ID string `graphql:"id"`
} `graphql:"items(id:$id)"`
}
func TestCommonMisuse_MissingVariableTypeShouldFail(t *testing.T) {
exec, err := graphql.Marshal(MissingVariableTypeError{})
if err != nil {
t.Fatalf("marshal should succeed, got: %v", err)
}
_, err = exec.Query("MissingType")
if err == nil {
t.Fatal("expected missing variable type error, got nil")
}
if !strings.Contains(err.Error(), "缺少类型定义") {
t.Fatalf("unexpected error: %v", err)
}
}