-
-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathoutput_test.go
More file actions
199 lines (158 loc) · 5.37 KB
/
Copy pathoutput_test.go
File metadata and controls
199 lines (158 loc) · 5.37 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package ferret
import (
"context"
stdjson "encoding/json"
"errors"
"strings"
"testing"
ferretencoding "github.com/MontFerret/ferret/v2/pkg/encoding"
encodingjson "github.com/MontFerret/ferret/v2/pkg/encoding/json"
"github.com/MontFerret/ferret/v2/pkg/runtime"
)
type trackingJSONCloser struct {
closeErr error
name string
payload string
closed int
}
func newTrackingJSONCloser(name, payload string) *trackingJSONCloser {
return &trackingJSONCloser{name: name, payload: payload}
}
func (c *trackingJSONCloser) Close() error {
c.closed++
return c.closeErr
}
func (c *trackingJSONCloser) MarshalJSON() ([]byte, error) {
return []byte(c.payload), nil
}
func (c *trackingJSONCloser) String() string {
return c.name
}
func (c *trackingJSONCloser) Hash() uint64 {
return runtime.NewString(c.name).Hash()
}
func (c *trackingJSONCloser) Copy() runtime.Value {
return c
}
type aliasCodec struct {
base ferretencoding.Codec
contentType string
}
func (c aliasCodec) ContentType() string {
return c.contentType
}
func (c aliasCodec) Encode(value runtime.Value) ([]byte, error) {
return c.base.Encode(value)
}
func (c aliasCodec) EncodeWith() ferretencoding.EncoderConfigurer {
return c.base.EncodeWith()
}
func (c aliasCodec) Decode(data []byte) (runtime.Value, error) {
return c.base.Decode(data)
}
func (c aliasCodec) DecodeWith() ferretencoding.DecoderConfigurer {
return c.base.DecodeWith()
}
func TestSessionRunReturnsDefaultJSONOutput(t *testing.T) {
eng := mustNewEngine(t)
plan := mustCompilePlan(t, eng, "RETURN 1")
session := mustNewSession(t, plan)
out, err := session.Run(context.Background())
if err != nil {
t.Fatalf("expected session run to succeed, got %v", err)
}
if out.ContentType != encodingjson.ContentType {
t.Fatalf("unexpected content type: got %q, want %q", out.ContentType, encodingjson.ContentType)
}
if got := strings.TrimSpace(string(out.Content)); got != "1" {
t.Fatalf("unexpected output payload: got %q", got)
}
}
func TestSessionRunUsesRequestedOutputCodec(t *testing.T) {
const customContentType = "application/x-test-json"
eng := mustNewEngine(t, WithEncodingCodec(customContentType, aliasCodec{
contentType: customContentType,
base: encodingjson.Default,
}))
plan := mustCompilePlan(t, eng, "RETURN 1")
session := mustNewSession(t, plan, WithOutputContentType(customContentType))
out, err := session.Run(context.Background())
if err != nil {
t.Fatalf("expected session run to succeed, got %v", err)
}
if out.ContentType != customContentType {
t.Fatalf("unexpected content type: got %q, want %q", out.ContentType, customContentType)
}
if got := strings.TrimSpace(string(out.Content)); got != "1" {
t.Fatalf("unexpected output payload: got %q", got)
}
}
func TestSessionRunClosesResultWhenRequestedCodecIsMissing(t *testing.T) {
root := newTrackingJSONCloser("root-missing-codec", "1")
eng := mustNewEngine(t, WithFunctionsRegistrar(func(fns runtime.FunctionDefs) {
fns.A0().Add("MAKE", func(context.Context) (runtime.Value, error) {
return root, nil
})
}))
plan := mustCompilePlan(t, eng, "RETURN MAKE()")
session := mustNewSession(t, plan, WithOutputContentType("application/x-missing"))
out, err := session.Run(context.Background())
if out != nil {
t.Fatal("expected output to be nil when codec resolution fails")
}
if err == nil {
t.Fatal("expected session run to fail when codec is missing")
}
if !errors.Is(err, ferretencoding.ErrCodecNotFound) {
t.Fatalf("expected codec-not-found error, got %v", err)
}
if got := root.closed; got != 1 {
t.Fatalf("expected missing-codec path to close encountered result resources once, got %d closes", got)
}
}
func TestSessionRunReturnsOutputWhenResultCleanupFails(t *testing.T) {
closeErr := errors.New("close boom")
closer := newTrackingJSONCloser("cleanup-failure", "1")
closer.closeErr = closeErr
eng := mustNewEngine(t, WithFunctionsRegistrar(func(fns runtime.FunctionDefs) {
fns.A0().Add("MAKE", func(context.Context) (runtime.Value, error) {
return closer, nil
})
}))
plan := mustCompilePlan(t, eng, "RETURN MAKE()")
session := mustNewSession(t, plan)
out, err := session.Run(context.Background())
if out == nil {
t.Fatal("expected output to be returned when cleanup fails after materialization")
}
if !errors.Is(err, closeErr) {
t.Fatalf("expected cleanup failure to be returned, got %v", err)
}
if got := strings.TrimSpace(string(out.Content)); got != "1" {
t.Fatalf("unexpected output payload: got %q", got)
}
}
func TestSessionRunClosesNestedLiveValuesDiscoveredDuringEncoding(t *testing.T) {
nested := newTrackingJSONCloser("nested-live", `"ok"`)
eng := mustNewEngine(t, WithFunctionsRegistrar(func(fns runtime.FunctionDefs) {
fns.A0().Add("MAKE", func(context.Context) (runtime.Value, error) {
return runtime.NewArrayWith(nested), nil
})
}))
plan := mustCompilePlan(t, eng, "RETURN MAKE()")
session := mustNewSession(t, plan)
out, err := session.Run(context.Background())
if err != nil {
t.Fatalf("expected session run to succeed, got %v", err)
}
var decoded []string
if err := stdjson.Unmarshal(out.Content, &decoded); err != nil {
t.Fatalf("failed to decode output: %v", err)
}
if len(decoded) != 1 || decoded[0] != "ok" {
t.Fatalf("unexpected output payload: %v", decoded)
}
if got := nested.closed; got != 1 {
t.Fatalf("expected nested live value to be closed after materialization, got %d closes", got)
}
}