Skip to content

Commit 4868f51

Browse files
committed
test: expand jsonschema coverage
1 parent bd36c45 commit 4868f51

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

jsonschema/containsref_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package jsonschema
2+
3+
import "testing"
4+
5+
// TestContainsRef ensures containsRef recursively searches definitions and items.
6+
func TestContainsRef(t *testing.T) {
7+
schema := Definition{
8+
Type: Object,
9+
Properties: map[string]Definition{
10+
"person": {Ref: "#/$defs/Person"},
11+
},
12+
Defs: map[string]Definition{
13+
"Person": {
14+
Type: Object,
15+
Properties: map[string]Definition{
16+
"friends": {
17+
Type: Array,
18+
Items: &Definition{Ref: "#/$defs/Person"},
19+
},
20+
},
21+
},
22+
},
23+
}
24+
25+
if !containsRef(schema, "#/$defs/Person") {
26+
t.Fatal("expected to find reference in root")
27+
}
28+
if !containsRef(schema.Defs["Person"], "#/$defs/Person") {
29+
t.Fatal("expected to find self reference in defs")
30+
}
31+
if containsRef(schema, "#/$defs/Unknown") {
32+
t.Fatal("unexpected reference found")
33+
}
34+
}

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)