openai/openai-go

Public

mirrored from https://github.com/openai/openai-goAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.3.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/param/encoder_test.go

326lines · modecode

1package param_test
2
3import (
4 "encoding/json"
5 "testing"
6 "time"
7
8 "github.com/openai/openai-go/packages/param"
9)
10
11type Struct struct {
12 A string `json:"a"`
13 B int64 `json:"b"`
14 param.APIObject
15}
16
17func (r Struct) MarshalJSON() (data []byte, err error) {
18 type shadow Struct
19 return param.MarshalObject(r, (*shadow)(&r))
20}
21
22// Note that the order of fields affects the JSON
23// key order. Changing the order of the fields in this struct
24// will fail tests unnecessarily.
25type FieldStruct struct {
26 A param.Opt[string] `json:"a,omitzero"`
27 B param.Opt[int64] `json:"b,omitzero"`
28 C Struct `json:"c,omitzero"`
29 D time.Time `json:"d,omitzero" format:"date"`
30 E time.Time `json:"e,omitzero"`
31 F param.Opt[time.Time] `json:"f,omitzero" format:"date"`
32 G param.Opt[time.Time] `json:"g,omitzero"`
33 H param.Opt[time.Time] `json:"h,omitzero" format:"date-time"`
34 param.APIObject
35}
36
37func (r FieldStruct) MarshalJSON() (data []byte, err error) {
38 type shadow FieldStruct
39 return param.MarshalObject(r, (*shadow)(&r))
40}
41
42type StructWithAdditionalProperties struct {
43 First string `json:"first"`
44 Second int `json:"second"`
45 ExtraFields map[string]any `json:"-"`
46 param.APIObject
47}
48
49func (s StructWithAdditionalProperties) MarshalJSON() ([]byte, error) {
50 type shadow StructWithAdditionalProperties
51 return param.MarshalWithExtras(s, (*shadow)(&s), s.ExtraFields)
52}
53
54func TestIsNullish(t *testing.T) {
55 nullTests := map[string]param.ParamNullable{
56 "null_string": param.Null[string](),
57 "null_int64": param.Null[int64](),
58 "null_time": param.Null[time.Time](),
59 "null_struct": param.NullStruct[Struct](),
60 }
61
62 for name, test := range nullTests {
63 t.Run(name, func(t *testing.T) {
64 if !param.IsNull(test) {
65 t.Fatalf("expected %s to be null", name)
66 }
67 if param.IsOmitted(test) {
68 t.Fatalf("expected %s to not be omitted", name)
69 }
70 })
71 }
72
73 omitTests := map[string]param.ParamNullable{
74 "omit_string": param.Opt[string]{},
75 "omit_int64": param.Opt[int64]{},
76 "omit_time": param.Opt[time.Time]{},
77 "omit_struct": Struct{},
78 }
79
80 for name, test := range omitTests {
81 t.Run(name, func(t *testing.T) {
82 if param.IsNull(test) {
83 t.Fatalf("expected %s to be null", name)
84 }
85 if !param.IsOmitted(test) {
86 t.Fatalf("expected %s to not be omitted", name)
87 }
88 })
89 }
90}
91
92func TestFieldMarshal(t *testing.T) {
93 tests := map[string]struct {
94 value any
95 expected string
96 }{
97 "null_string": {param.Null[string](), "null"},
98 "null_int64": {param.Null[int64](), "null"},
99 "null_time": {param.Null[time.Time](), "null"},
100 "null_struct": {param.NullStruct[Struct](), "null"},
101
102 "float_zero": {param.NewOpt(float64(0.0)), "0"},
103 "string_zero": {param.NewOpt(""), `""`},
104 "time_zero": {param.NewOpt(time.Time{}), `"0001-01-01T00:00:00Z"`},
105
106 "string": {param.Opt[string]{Value: "string"}, `"string"`},
107 "int": {param.Opt[int64]{Value: 123}, "123"},
108 "int64": {param.Opt[int64]{Value: int64(123456789123456789)}, "123456789123456789"},
109 "struct": {Struct{A: "yo", B: 123}, `{"a":"yo","b":123}`},
110 "datetime": {
111 param.Opt[time.Time]{Value: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC)},
112 `"2023-03-18T14:47:38Z"`,
113 },
114 "optional_date": {
115 FieldStruct{
116 F: param.Opt[time.Time]{Value: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC)},
117 },
118 `{"f":"2023-03-18"}`,
119 },
120 "optional_time": {
121 FieldStruct{
122 G: param.Opt[time.Time]{Value: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC)},
123 },
124 `{"g":"2023-03-18T14:47:38Z"}`,
125 },
126 "optional_datetime_explicit_format": {
127 FieldStruct{
128 H: param.Opt[time.Time]{Value: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC)},
129 },
130 `{"h":"2023-03-18T14:47:38Z"}`,
131 },
132 "param_struct": {
133 FieldStruct{
134 A: param.Opt[string]{Value: "hello"},
135 B: param.Opt[int64]{Value: int64(12)},
136 D: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC),
137 E: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC),
138 F: param.Opt[time.Time]{Value: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC)},
139 G: param.Opt[time.Time]{Value: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC)},
140 H: param.Opt[time.Time]{Value: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC)},
141 },
142 `{"a":"hello","b":12,"d":"2023-03-18","e":"2023-03-18T14:47:38Z","f":"2023-03-18","g":"2023-03-18T14:47:38Z","h":"2023-03-18T14:47:38Z"}`,
143 },
144 }
145
146 for name, test := range tests {
147 t.Run(name, func(t *testing.T) {
148 b, err := json.Marshal(test.value)
149 if err != nil {
150 t.Fatalf("didn't expect error %v, expected %s", err, test.expected)
151 }
152 if string(b) != test.expected {
153 t.Fatalf("expected %s, received %s", test.expected, string(b))
154 }
155 })
156 }
157}
158
159func TestAdditionalProperties(t *testing.T) {
160 s := StructWithAdditionalProperties{
161 First: "hello",
162 Second: 14,
163 ExtraFields: map[string]any{
164 "hi": "there",
165 },
166 }
167 exp := `{"first":"hello","second":14,"hi":"there"}`
168
169 bytes, err := json.Marshal(s)
170 if err != nil {
171 t.Fatalf("failed to marshal: %v", err)
172 }
173
174 if string(bytes) != exp {
175 t.Fatalf("expected %s, got %s", exp, string(bytes))
176 }
177}
178
179func TestExtraFields(t *testing.T) {
180 v := Struct{
181 A: "hello",
182 B: 123,
183 }
184 v.SetExtraFields(map[string]any{
185 "extra": Struct{A: "recursive"},
186 "b": nil,
187 })
188 bytes, err := json.Marshal(v)
189 if err != nil {
190 t.Fatalf("failed to marshal: %v", err)
191 }
192 if string(bytes) != `{"a":"hello","b":null,"extra":{"a":"recursive","b":0}}` {
193 t.Fatalf("failed to marshal: got %v", string(bytes))
194 }
195 if v.B != 123 {
196 t.Fatalf("marshal modified field B: got %v", v.B)
197 }
198}
199
200func TestExtraFieldsForceOmitted(t *testing.T) {
201 v := Struct{
202 // Testing with the zero value.
203 // A: "",
204 // B: 0,
205 }
206 v.SetExtraFields(map[string]any{
207 "b": param.Omit,
208 })
209 bytes, err := json.Marshal(v)
210 if err != nil {
211 t.Fatalf("failed to marshal: %v", err)
212 }
213 if string(bytes) != `{"a":""}` {
214 t.Fatalf("failed to marshal: got %v", string(bytes))
215 }
216}
217
218type UnionWithDates struct {
219 OfDate param.Opt[time.Time]
220 OfTime param.Opt[time.Time]
221}
222
223func (r UnionWithDates) MarshalJSON() (data []byte, err error) {
224 return param.MarshalUnion[UnionWithDates](param.EncodedAsDate(r.OfDate), r.OfTime)
225}
226
227func TestUnionDateMarshal(t *testing.T) {
228 tests := map[string]struct {
229 value UnionWithDates
230 expected string
231 }{
232 "date_only": {
233 UnionWithDates{
234 OfDate: param.Opt[time.Time]{Value: time.Date(2023, time.March, 18, 0, 0, 0, 0, time.UTC)},
235 },
236 `"2023-03-18"`,
237 },
238 "datetime_only": {
239 UnionWithDates{
240 OfTime: param.Opt[time.Time]{Value: time.Date(2023, time.March, 18, 14, 47, 38, 0, time.UTC)},
241 },
242 `"2023-03-18T14:47:38Z"`,
243 },
244 }
245
246 for name, test := range tests {
247 t.Run(name, func(t *testing.T) {
248 b, err := json.Marshal(test.value)
249 if err != nil {
250 t.Fatalf("didn't expect error %v, expected %s", err, test.expected)
251 }
252 if string(b) != test.expected {
253 t.Fatalf("expected %s, received %s", test.expected, string(b))
254 }
255 })
256 }
257}
258
259func TestOverride(t *testing.T) {
260 tests := map[string]struct {
261 value param.ParamStruct
262 expected string
263 }{
264 "param_struct": {
265 param.Override[FieldStruct](map[string]any{
266 "a": "hello",
267 "b": 12,
268 "c": nil,
269 }),
270 `{"a":"hello","b":12,"c":null}`,
271 },
272 "param_struct_primitive": {
273 param.Override[FieldStruct](12),
274 `12`,
275 },
276 "param_struct_null": {
277 param.Override[FieldStruct](nil),
278 `null`,
279 },
280 }
281
282 f := FieldStruct{}
283
284 f.SetExtraFields(map[string]any{
285 "z": "ok",
286 })
287
288 for name, test := range tests {
289 t.Run(name, func(t *testing.T) {
290 b, err := json.Marshal(test.value)
291 if err != nil {
292 t.Fatalf("didn't expect error %v, expected %s", err, test.expected)
293 }
294 if string(b) != test.expected {
295 t.Fatalf("expected %s, received %s", test.expected, string(b))
296 }
297 if _, ok := test.value.Overrides(); !ok {
298 t.Fatalf("expected to be overridden")
299 }
300 })
301 }
302}
303
304// Despite implementing the interface, this struct is not an param.Optional
305// since it was defined in a different package.
306type almostOpt struct{}
307
308func (almostOpt) Valid() bool { return true }
309func (almostOpt) Null() bool { return false }
310func (almostOpt) isZero() bool { return false }
311
312func (almostOpt) implOpt() {}
313
314func TestOptionalInterfaceAssignability(t *testing.T) {
315 optInt := param.Opt[int]{}
316 if _, ok := any(optInt).(param.Optional); !ok {
317 t.Fatalf("failed to assign")
318 }
319
320 notOpt := almostOpt{}
321 if _, ok := any(notOpt).(param.Optional); ok {
322 t.Fatalf("unexpected successful assignment")
323 }
324
325 notOpt.implOpt() // silence the warning
326}