2
0

Add UnmarshalJSON to generated ints

This commit is contained in:
Jack Christensen
2022-01-01 11:25:26 -06:00
parent 0403c34ae3
commit d2cf33ed40
4 changed files with 115 additions and 0 deletions
+41
View File
@@ -41,3 +41,44 @@ func TestInt2Codec(t *testing.T) {
{nil, new(*int16), isExpectedEq((*int16)(nil))},
})
}
func TestInt2MarshalJSON(t *testing.T) {
successfulTests := []struct {
source pgtype.Int2
result string
}{
{source: pgtype.Int2{Int: 0}, result: "null"},
{source: pgtype.Int2{Int: 1, Valid: true}, result: "1"},
}
for i, tt := range successfulTests {
r, err := tt.source.MarshalJSON()
if err != nil {
t.Errorf("%d: %v", i, err)
}
if string(r) != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
}
}
}
func TestInt2UnmarshalJSON(t *testing.T) {
successfulTests := []struct {
source string
result pgtype.Int2
}{
{source: "null", result: pgtype.Int2{Int: 0}},
{source: "1", result: pgtype.Int2{Int: 1, Valid: true}},
}
for i, tt := range successfulTests {
var r pgtype.Int2
err := r.UnmarshalJSON([]byte(tt.source))
if err != nil {
t.Errorf("%d: %v", i, err)
}
if r != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
}
}
}