Skip to content

Commit e6c1d3e

Browse files
authored
Increase jsonschema test coverage (#1043)
* test: expand jsonschema coverage * test: fix package name for containsref tests
1 parent bd36c45 commit e6c1d3e

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

jsonschema/containsref_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package jsonschema_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/sashabaranov/go-openai/jsonschema"
7+
)
8+
9+
// SelfRef struct used to produce a self-referential schema.
10+
type SelfRef struct {
11+
Friends []SelfRef `json:"friends"`
12+
}
13+
14+
// Address struct referenced by Person without self-reference.
15+
type Address struct {
16+
Street string `json:"street"`
17+
}
18+
19+
type Person struct {
20+
Address Address `json:"address"`
21+
}
22+
23+
// TestGenerateSchemaForType_SelfRef ensures that self-referential types are not
24+
// flattened during schema generation.
25+
func TestGenerateSchemaForType_SelfRef(t *testing.T) {
26+
schema, err := jsonschema.GenerateSchemaForType(SelfRef{})
27+
if err != nil {
28+
t.Fatalf("unexpected error: %v", err)
29+
}
30+
if _, ok := schema.Defs["SelfRef"]; !ok {
31+
t.Fatal("expected defs to contain SelfRef for self reference")
32+
}
33+
}
34+
35+
// TestGenerateSchemaForType_NoSelfRef ensures that non-self-referential types
36+
// are flattened and do not reappear in $defs.
37+
func TestGenerateSchemaForType_NoSelfRef(t *testing.T) {
38+
schema, err := jsonschema.GenerateSchemaForType(Person{})
39+
if err != nil {
40+
t.Fatalf("unexpected error: %v", err)
41+
}
42+
if _, ok := schema.Defs["Person"]; ok {
43+
t.Fatal("unexpected Person definition in defs")
44+
}
45+
if _, ok := schema.Defs["Address"]; !ok {
46+
t.Fatal("expected Address definition in defs")
47+
}
48+
}

jsonschema/json_errors_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package jsonschema_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/sashabaranov/go-openai/jsonschema"
7+
)
8+
9+
// TestGenerateSchemaForType_ErrorPaths verifies error handling for unsupported types.
10+
func TestGenerateSchemaForType_ErrorPaths(t *testing.T) {
11+
type anon struct{ Ch chan int }
12+
tests := []struct {
13+
name string
14+
v any
15+
}{
16+
{"slice", []chan int{}},
17+
{"anon struct", anon{}},
18+
{"pointer", (*chan int)(nil)},
19+
}
20+
for _, tt := range tests {
21+
t.Run(tt.name, func(t *testing.T) {
22+
if _, err := jsonschema.GenerateSchemaForType(tt.v); err == nil {
23+
t.Errorf("expected error for %s", tt.name)
24+
}
25+
})
26+
}
27+
}

0 commit comments

Comments
 (0)