11package jtdinfer
22
33import (
4- "encoding/json"
54 "testing"
65
76 jtd "github.com/jsontypedef/json-typedef-go"
87 "github.com/stretchr/testify/assert"
9- "github.com/stretchr/testify/require"
108)
119
1210func TestJTDInfer (t * testing.T ) {
1311 rows := []string {
1412 `{"name": "Joe", "age": 42, "hobbies": ["code", "animals"]}` ,
1513 }
1614
17- inferrer := NewInferrer (Hints {})
18-
19- for _ , row := range rows {
20- rowAsJSON := make (map [string ]any , 0 )
21- require .NoError (t , json .Unmarshal ([]byte (row ), & rowAsJSON ))
22- inferrer = inferrer .Infer (rowAsJSON )
23- }
24-
2515 expectedSchema := Schema {
2616 Properties : map [string ]Schema {
2717 "name" : {Type : jtd .TypeString },
2818 "age" : {Type : jtd .TypeUint8 },
2919 "hobbies" : {Elements : & Schema {Type : jtd .TypeString }},
3020 },
3121 }
32- gotSchema := inferrer .IntoSchema ()
22+ gotSchema := InferStrings ( rows , Hints {}) .IntoSchema (Hints {} )
3323
3424 assert .EqualValues (t , expectedSchema , gotSchema )
3525}
3626
37- func TestJTDInferrerWithHints (t * testing.T ) {
27+ func TestJTDInferrerWithEnumHints (t * testing.T ) {
3828 hints := Hints {
39- Enums : HintSet {
40- Values : [][]string {
41- {"name" },
42- {"address" , "city" },
43- },
44- },
29+ Enums : NewHintSet ().
30+ Add ([]string {"name" }).
31+ Add ([]string {"address" , "city" }),
4532 }
4633
4734 rows := []string {
4835 `{"address": {"city": "Stockholm"}, "name": "Joe", "age": 42}` ,
4936 `{"address": {"city": "Umeå"}, "name": "Labero", "age": 42}` ,
5037 }
5138
52- inferrer := NewInferrer (hints )
53-
54- for _ , row := range rows {
55- rowAsJSON := make (map [string ]any , 0 )
56- require .NoError (t , json .Unmarshal ([]byte (row ), & rowAsJSON ))
57- inferrer = inferrer .Infer (rowAsJSON )
58- }
59-
6039 expectedSchema := Schema {
6140 Properties : map [string ]Schema {
6241 "name" : {Enum : []string {"Joe" , "Labero" }},
@@ -68,30 +47,40 @@ func TestJTDInferrerWithHints(t *testing.T) {
6847 },
6948 },
7049 }
71- gotSchema := inferrer .IntoSchema ()
50+ gotSchema := InferStrings ( rows , hints ) .IntoSchema (hints )
7251
7352 assert .EqualValues (t , expectedSchema , gotSchema )
7453}
7554
76- func TestJTDInferWithDiscriminatorHints (t * testing.T ) {
55+ func TestJTDInferWithValuesHints (t * testing.T ) {
7756 hints := Hints {
78- Discriminator : HintSet {
79- Values : [][]string {
80- {"-" , "type" },
81- },
82- },
57+ Values : NewHintSet ().Add ([]string {}),
8358 }
8459
8560 rows := []string {
86- `[{"type": "s", "value": "foo"},{"type": "n", "value": 3.14}]` ,
61+ `{"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]}` ,
62+ `{"x": [1, 2, 3], "y": [4, 5, -600], "z": [7, 8, 9]}` ,
8763 }
8864
89- inferrer := NewInferrer (hints )
65+ expectedSchema := Schema {
66+ Values : & Schema {
67+ Elements : & Schema {
68+ Type : jtd .TypeInt16 ,
69+ },
70+ },
71+ }
72+ gotSchema := InferStrings (rows , hints ).IntoSchema (hints )
73+
74+ assert .EqualValues (t , expectedSchema , gotSchema )
75+ }
9076
91- for _ , row := range rows {
92- rowAsJSON := make ([]any , 0 )
93- require .NoError (t , json .Unmarshal ([]byte (row ), & rowAsJSON ))
94- inferrer = inferrer .Infer (rowAsJSON )
77+ func TestJTDInferWithDiscriminatorHints (t * testing.T ) {
78+ hints := Hints {
79+ Discriminator : NewHintSet ().Add ([]string {"-" , "type" }),
80+ }
81+
82+ rows := []string {
83+ `[{"type": "s", "value": "foo"},{"type": "n", "value": 3.14}]` ,
9584 }
9685
9786 expectedSchema := Schema {
@@ -111,7 +100,7 @@ func TestJTDInferWithDiscriminatorHints(t *testing.T) {
111100 },
112101 },
113102 }
114- gotSchema := inferrer .IntoSchema ()
103+ gotSchema := InferStrings ( rows , hints ) .IntoSchema (hints )
115104
116105 assert .EqualValues (t , expectedSchema , gotSchema )
117106}
0 commit comments