|
1 | 1 | package jtdinfer |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "strconv" |
4 | 7 | "testing" |
| 8 | + "time" |
5 | 9 |
|
6 | 10 | jtd "github.com/jsontypedef/json-typedef-go" |
7 | 11 | "github.com/stretchr/testify/assert" |
8 | 12 | "github.com/stretchr/testify/require" |
9 | 13 | ) |
10 | 14 |
|
| 15 | +func TestInferString(t *testing.T) { |
| 16 | + cases := []struct { |
| 17 | + description string |
| 18 | + values []string |
| 19 | + expectedSchema Schema |
| 20 | + }{ |
| 21 | + { |
| 22 | + description: "boolean true value", |
| 23 | + values: []string{"true"}, |
| 24 | + expectedSchema: Schema{ |
| 25 | + Type: jtd.TypeBoolean, |
| 26 | + }, |
| 27 | + }, |
| 28 | + { |
| 29 | + description: "boolean false value", |
| 30 | + values: []string{"false"}, |
| 31 | + expectedSchema: Schema{ |
| 32 | + Type: jtd.TypeBoolean, |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + description: "object", |
| 37 | + values: []string{`{"name":"Joe"}`}, |
| 38 | + expectedSchema: Schema{ |
| 39 | + Properties: map[string]Schema{ |
| 40 | + "name": { |
| 41 | + Type: jtd.TypeString, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + description: "array", |
| 48 | + values: []string{`[1, 2, 3]`}, |
| 49 | + expectedSchema: Schema{ |
| 50 | + Elements: &Schema{ |
| 51 | + Type: jtd.TypeUint8, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + description: "unsigned integer", |
| 57 | + values: []string{"1"}, |
| 58 | + expectedSchema: Schema{ |
| 59 | + Type: jtd.TypeUint8, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + description: "signed integer", |
| 64 | + values: []string{"-1"}, |
| 65 | + expectedSchema: Schema{ |
| 66 | + Type: jtd.TypeInt8, |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + description: "signed max integer", |
| 71 | + values: []string{strconv.Itoa(math.MinInt32)}, |
| 72 | + expectedSchema: Schema{ |
| 73 | + Type: jtd.TypeInt32, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + description: "float without fraction", |
| 78 | + values: []string{"1.0"}, |
| 79 | + expectedSchema: Schema{ |
| 80 | + Type: jtd.TypeUint8, |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + description: "positive float", |
| 85 | + values: []string{"1.1"}, |
| 86 | + expectedSchema: Schema{ |
| 87 | + Type: jtd.TypeFloat64, |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + description: "negative float", |
| 92 | + values: []string{"-1.1"}, |
| 93 | + expectedSchema: Schema{ |
| 94 | + Type: jtd.TypeFloat64, |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + description: "string", |
| 99 | + values: []string{`"string"`}, |
| 100 | + expectedSchema: Schema{ |
| 101 | + Type: jtd.TypeString, |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + description: "number in string is still string", |
| 106 | + values: []string{`"2.2"`}, |
| 107 | + expectedSchema: Schema{ |
| 108 | + Type: jtd.TypeString, |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + description: "timestamp", |
| 113 | + values: []string{fmt.Sprintf(`"%s"`, time.Now().Format(time.RFC3339))}, |
| 114 | + expectedSchema: Schema{ |
| 115 | + Type: jtd.TypeTimestamp, |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + description: "null", |
| 120 | + values: []string{"null"}, |
| 121 | + expectedSchema: Schema{ |
| 122 | + Nullable: true, |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + for _, tc := range cases { |
| 128 | + t.Run(tc.description, func(t *testing.T) { |
| 129 | + gotSchema := InferStrings(tc.values, NewHints()).IntoSchema() |
| 130 | + assert.EqualValues(t, tc.expectedSchema, gotSchema) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
11 | 135 | func TestJTDInfer(t *testing.T) { |
12 | 136 | rows := []string{ |
13 | 137 | `{"name": "Joe", "age": 42, "hobbies": ["code", "animals"]}`, |
|
0 commit comments