-
Notifications
You must be signed in to change notification settings - Fork 0
/
responseGenerator_test.go
162 lines (130 loc) · 2.99 KB
/
responseGenerator_test.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package hypeql
import (
"fmt"
"slices"
"testing"
)
// TEST #1
type A struct {
Foo string `json:"foo"`
Bar bool `json:"bar" fun:"Rbar"`
Blist []B `json:"blist" fun:"Rblist"`
Clist []C `json:"clist" fun:"Rclist"`
}
type B struct {
Text string `json:"text"`
}
type C struct {
Text string `json:"text" fun:"Rtext"`
Foo bool `json:"foo" fun:"Rfoo"`
}
func (a A) Rbar(ctx *map[string]any) bool {
return true
}
func (a A) Rblist(ctx *map[string]any, args map[string]any) []B {
return []B{
{}, {},
}
}
func (a A) Rclist(ctx *map[string]any, args map[string]any) []C {
return []C{
{}, {},
}
}
func (a C) Resolve(ctx *map[string]any, fields []string) {
if slices.Contains(fields, "text") {
(*ctx)["text"] = "Hello"
}
if slices.Contains(fields, "foo") {
(*ctx)["foo"] = "bar"
}
}
func (a C) Rtext(ctx *map[string]any) any {
return (*ctx)["text"]
}
func (a C) Rfoo(ctx *map[string]any) any {
return (*ctx)["foo"]
}
func TestGeneral(t *testing.T) {
parser := NewQueryParser(QueryParserConfig{})
generator := NewResponseGenerator(ResponseGeneratorConfig{})
query := `
{
foo
bar
clist {
text
}
}`
// mustBe := `{}`
parsed, err := parser.Parse(query)
if err != nil {
t.Fatal("Parsing error: " + err.Error())
}
resp, err := generator.Generate(parsed, A{}, map[string]any{})
if err != nil {
t.Fatal("Process error: " + err.Error())
}
mustBe := `{"bar":true,"clist":[{"text":"Hello"},{"text":"Hello"}],"foo":""}`
if resp != mustBe {
t.Fatal("Not equal")
}
}
// TEST #2
type ThrowsError struct {
Value bool `json:"value"`
}
func (a ThrowsError) Resolve(ctx *map[string]any, fields []string) error {
return fmt.Errorf("Error throws here")
}
func TestResolveError(t *testing.T) {
generator := NewResponseGenerator(ResponseGeneratorConfig{})
query := []any{
"value",
}
_, err := generator.Generate(query, ThrowsError{}, map[string]any{})
if err == nil || err.Error() != "Error throws here" {
t.Fatal("Not equal")
}
}
// TEST #3
type First struct {
Seconds []Second `json:"seconds"`
}
type Second struct {
Thirds []Third `json:"thirds"`
}
type Third struct {
Fourths []Fourth `json:"fourths"`
}
type Fourth struct {
CantReach string `json:"cantreach"` // Can't reach because of deep recursion limit
}
func TestDeepRecursionLimitGenerator(t *testing.T) {
parser := NewQueryParser(QueryParserConfig{})
generator := NewResponseGenerator(ResponseGeneratorConfig{
MaxDeepRecursion: 3,
})
parsedQuery, err := parser.Parse("{seconds{thirds{fourths{cantreach}}}}")
if err != nil {
t.Fatal("Parsing error: " + err.Error())
}
_, err = generator.Generate(parsedQuery, First{
Seconds: []Second{
{
Thirds: []Third{
{
Fourths: []Fourth{
{
CantReach: "",
},
},
},
},
},
},
}, map[string]any{})
if err == nil {
t.Fatal("Not equal")
}
}