|
| 1 | +package gojay |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDecodeSQLNullString(t *testing.T) { |
| 12 | + testCases := []struct { |
| 13 | + name string |
| 14 | + json string |
| 15 | + expectedNullString sql.NullString |
| 16 | + err bool |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "basic", |
| 20 | + json: `"test"`, |
| 21 | + expectedNullString: sql.NullString{String: "test", Valid: true}, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "basic", |
| 25 | + json: `"test`, |
| 26 | + expectedNullString: sql.NullString{String: "test", Valid: true}, |
| 27 | + err: true, |
| 28 | + }, |
| 29 | + } |
| 30 | + for _, testCase := range testCases { |
| 31 | + t.Run(testCase.name, func(t *testing.T) { |
| 32 | + nullString := sql.NullString{} |
| 33 | + dec := NewDecoder(strings.NewReader(testCase.json)) |
| 34 | + err := dec.DecodeSQLNullString(&nullString) |
| 35 | + if testCase.err { |
| 36 | + assert.NotNil(t, err) |
| 37 | + } else { |
| 38 | + assert.Nil(t, err) |
| 39 | + assert.Equal(t, testCase.expectedNullString, nullString) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | + t.Run( |
| 44 | + "should panic because decoder is pooled", |
| 45 | + func(t *testing.T) { |
| 46 | + dec := NewDecoder(nil) |
| 47 | + dec.Release() |
| 48 | + defer func() { |
| 49 | + err := recover() |
| 50 | + assert.NotNil(t, err, "err shouldnt be nil") |
| 51 | + assert.IsType(t, InvalidUsagePooledDecoderError(""), err, "err should be of type InvalidUsagePooledDecoderError") |
| 52 | + }() |
| 53 | + _ = dec.DecodeSQLNullString(&sql.NullString{}) |
| 54 | + assert.True(t, false, "should not be called as decoder should have panicked") |
| 55 | + }, |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +func TestDecodeSQLNullInt64(t *testing.T) { |
| 60 | + testCases := []struct { |
| 61 | + name string |
| 62 | + json string |
| 63 | + expectedNullInt64 sql.NullInt64 |
| 64 | + err bool |
| 65 | + }{ |
| 66 | + { |
| 67 | + name: "basic", |
| 68 | + json: `1`, |
| 69 | + expectedNullInt64: sql.NullInt64{Int64: 1, Valid: true}, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "basic", |
| 73 | + json: `"test`, |
| 74 | + expectedNullInt64: sql.NullInt64{Int64: 1, Valid: true}, |
| 75 | + err: true, |
| 76 | + }, |
| 77 | + } |
| 78 | + for _, testCase := range testCases { |
| 79 | + t.Run(testCase.name, func(t *testing.T) { |
| 80 | + nullInt64 := sql.NullInt64{} |
| 81 | + dec := NewDecoder(strings.NewReader(testCase.json)) |
| 82 | + err := dec.DecodeSQLNullInt64(&nullInt64) |
| 83 | + if testCase.err { |
| 84 | + assert.NotNil(t, err) |
| 85 | + } else { |
| 86 | + assert.Nil(t, err) |
| 87 | + assert.Equal(t, testCase.expectedNullInt64, nullInt64) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | + t.Run( |
| 92 | + "should panic because decoder is pooled", |
| 93 | + func(t *testing.T) { |
| 94 | + dec := NewDecoder(nil) |
| 95 | + dec.Release() |
| 96 | + defer func() { |
| 97 | + err := recover() |
| 98 | + assert.NotNil(t, err, "err shouldnt be nil") |
| 99 | + assert.IsType(t, InvalidUsagePooledDecoderError(""), err, "err should be of type InvalidUsagePooledDecoderError") |
| 100 | + }() |
| 101 | + _ = dec.DecodeSQLNullInt64(&sql.NullInt64{}) |
| 102 | + assert.True(t, false, "should not be called as decoder should have panicked") |
| 103 | + }, |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +func TestDecodeSQLNullFloat64(t *testing.T) { |
| 108 | + testCases := []struct { |
| 109 | + name string |
| 110 | + json string |
| 111 | + expectedNullFloat64 sql.NullFloat64 |
| 112 | + err bool |
| 113 | + }{ |
| 114 | + { |
| 115 | + name: "basic", |
| 116 | + json: `1`, |
| 117 | + expectedNullFloat64: sql.NullFloat64{Float64: 1, Valid: true}, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "basic", |
| 121 | + json: `"test`, |
| 122 | + expectedNullFloat64: sql.NullFloat64{Float64: 1, Valid: true}, |
| 123 | + err: true, |
| 124 | + }, |
| 125 | + } |
| 126 | + for _, testCase := range testCases { |
| 127 | + t.Run(testCase.name, func(t *testing.T) { |
| 128 | + nullFloat64 := sql.NullFloat64{} |
| 129 | + dec := NewDecoder(strings.NewReader(testCase.json)) |
| 130 | + err := dec.DecodeSQLNullFloat64(&nullFloat64) |
| 131 | + if testCase.err { |
| 132 | + assert.NotNil(t, err) |
| 133 | + } else { |
| 134 | + assert.Nil(t, err) |
| 135 | + assert.Equal(t, testCase.expectedNullFloat64, nullFloat64) |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | + t.Run( |
| 140 | + "should panic because decoder is pooled", |
| 141 | + func(t *testing.T) { |
| 142 | + dec := NewDecoder(nil) |
| 143 | + dec.Release() |
| 144 | + defer func() { |
| 145 | + err := recover() |
| 146 | + assert.NotNil(t, err, "err shouldnt be nil") |
| 147 | + assert.IsType(t, InvalidUsagePooledDecoderError(""), err, "err should be of type InvalidUsagePooledDecoderError") |
| 148 | + }() |
| 149 | + _ = dec.DecodeSQLNullFloat64(&sql.NullFloat64{}) |
| 150 | + assert.True(t, false, "should not be called as decoder should have panicked") |
| 151 | + }, |
| 152 | + ) |
| 153 | +} |
| 154 | + |
| 155 | +func TestDecodeSQLNullBool(t *testing.T) { |
| 156 | + testCases := []struct { |
| 157 | + name string |
| 158 | + json string |
| 159 | + expectedNullBool sql.NullBool |
| 160 | + err bool |
| 161 | + }{ |
| 162 | + { |
| 163 | + name: "basic", |
| 164 | + json: `true`, |
| 165 | + expectedNullBool: sql.NullBool{Bool: true, Valid: true}, |
| 166 | + }, |
| 167 | + { |
| 168 | + name: "basic", |
| 169 | + json: `"&`, |
| 170 | + expectedNullBool: sql.NullBool{Bool: true, Valid: true}, |
| 171 | + err: true, |
| 172 | + }, |
| 173 | + } |
| 174 | + for _, testCase := range testCases { |
| 175 | + t.Run(testCase.name, func(t *testing.T) { |
| 176 | + nullBool := sql.NullBool{} |
| 177 | + dec := NewDecoder(strings.NewReader(testCase.json)) |
| 178 | + err := dec.DecodeSQLNullBool(&nullBool) |
| 179 | + if testCase.err { |
| 180 | + assert.NotNil(t, err) |
| 181 | + } else { |
| 182 | + assert.Nil(t, err) |
| 183 | + assert.Equal(t, testCase.expectedNullBool, nullBool) |
| 184 | + } |
| 185 | + }) |
| 186 | + } |
| 187 | + t.Run( |
| 188 | + "should panic because decoder is pooled", |
| 189 | + func(t *testing.T) { |
| 190 | + dec := NewDecoder(nil) |
| 191 | + dec.Release() |
| 192 | + defer func() { |
| 193 | + err := recover() |
| 194 | + assert.NotNil(t, err, "err shouldnt be nil") |
| 195 | + assert.IsType(t, InvalidUsagePooledDecoderError(""), err, "err should be of type InvalidUsagePooledDecoderError") |
| 196 | + }() |
| 197 | + _ = dec.DecodeSQLNullBool(&sql.NullBool{}) |
| 198 | + assert.True(t, false, "should not be called as decoder should have panicked") |
| 199 | + }, |
| 200 | + ) |
| 201 | +} |
0 commit comments