github.com/prebid/prebid-server/v2@v2.18.0/util/jsonutil/stringInt_test.go (about) 1 package jsonutil 2 3 import ( 4 "testing" 5 6 "github.com/buger/jsonparser" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestStringIntUnmarshalJSON(t *testing.T) { 11 type Item struct { 12 ItemId StringInt `json:"item_id"` 13 } 14 15 t.Run("string", func(t *testing.T) { 16 jsonData := []byte(`{"item_id":"30"}`) 17 var item Item 18 assert.NoError(t, UnmarshalValid(jsonData, &item)) 19 assert.Equal(t, 30, int(item.ItemId)) 20 }) 21 22 t.Run("int", func(t *testing.T) { 23 jsonData := []byte(`{"item_id":30}`) 24 var item Item 25 assert.NoError(t, UnmarshalValid(jsonData, &item)) 26 assert.Equal(t, 30, int(item.ItemId)) 27 }) 28 29 t.Run("empty_id", func(t *testing.T) { 30 jsonData := []byte(`{"item_id": ""}`) 31 var item Item 32 assert.NoError(t, UnmarshalValid(jsonData, &item)) 33 }) 34 35 t.Run("invalid_input", func(t *testing.T) { 36 jsonData := []byte(`{"item_id":true}`) 37 var item Item 38 err := UnmarshalValid(jsonData, &item) 39 assert.EqualError(t, err, "cannot unmarshal jsonutil.Item.ItemId: "+jsonparser.MalformedValueError.Error()) 40 }) 41 }