Skip to content

Commit

Permalink
[AB#1669514] type assertions on casts in tests to avoid panics
Browse files Browse the repository at this point in the history
  • Loading branch information
ChronosMasterOfAllTime committed Dec 2, 2024
1 parent 7b04952 commit 44119de
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions structured_type_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ type objectWithAllTypes struct {
}

func (o *objectWithAllTypes) Scan(val any) error {
st := val.(StructuredObject)
st, ok := val.(StructuredObject)
if !ok {
return fmt.Errorf("expected StructuredObject, got %T", val)
}

var err error
if o.s, err = st.GetString("s"); err != nil {
return err
Expand Down Expand Up @@ -193,7 +197,11 @@ type simpleObject struct {
}

func (so *simpleObject) Scan(val any) error {
st := val.(StructuredObject)
st, ok := val.(StructuredObject)
if !ok {
return fmt.Errorf("expected StructuredObject, got %T", val)
}

var err error
if so.s, err = st.GetString("s"); err != nil {
return err
Expand Down Expand Up @@ -319,7 +327,11 @@ type objectWithAllTypesNullable struct {
}

func (o *objectWithAllTypesNullable) Scan(val any) error {
st := val.(StructuredObject)
st, ok := val.(StructuredObject)
if !ok {
return fmt.Errorf("expected StructuredObject, got %T", val)
}

var err error
if o.s, err = st.GetNullString("s"); err != nil {
return err
Expand Down Expand Up @@ -492,7 +504,7 @@ func TestObjectWithAllTypesNullable(t *testing.T) {
})
t.Run("not null", func(t *testing.T) {
uid := newTestUUID()
rows := dbt.mustQueryContextT(ctx, t, fmt.Sprintf("select 1, object_construct_keep_null('s', 'abc', 'b', 1, 'i16', 2, 'i32', 3, 'i64', 9223372036854775807, 'f64', 2.2, 'bo', true, 'bi', TO_BINARY('616263', 'HEX'), 'date', '2024-03-21'::DATE, 'time', '13:03:02'::TIME, 'ltz', '2021-07-21 11:22:33'::TIMESTAMP_LTZ, 'tz', '2022-08-31 13:43:22 +0200'::TIMESTAMP_TZ, 'ntz', '2023-05-22 01:17:19'::TIMESTAMP_NTZ, 'so', {'s': 'child', 'i': 9}::OBJECT, 'sArr', ARRAY_CONSTRUCT('x', 'y', 'z'), 'f64Arr', ARRAY_CONSTRUCT(1.1, 2.2, 3.3), 'someMap', {'x': true, 'y': false}, 'uuid': '%s')::OBJECT(s VARCHAR, b TINYINT, i16 SMALLINT, i32 INTEGER, i64 BIGINT, f64 DOUBLE, bo BOOLEAN, bi BINARY, date DATE, time TIME, ltz TIMESTAMP_LTZ, tz TIMESTAMP_TZ, ntz TIMESTAMP_NTZ, so OBJECT(s VARCHAR, i INTEGER), sArr ARRAY(VARCHAR), f64Arr ARRAY(DOUBLE), someMap MAP(VARCHAR, BOOLEAN), uuid VARCHAR)", uid))
rows := dbt.mustQueryContextT(ctx, t, fmt.Sprintf("select 1, object_construct_keep_null('s', 'abc', 'b', 1, 'i16', 2, 'i32', 3, 'i64', 9223372036854775807, 'f64', 2.2, 'bo', true, 'bi', TO_BINARY('616263', 'HEX'), 'date', '2024-03-21'::DATE, 'time', '13:03:02'::TIME, 'ltz', '2021-07-21 11:22:33'::TIMESTAMP_LTZ, 'tz', '2022-08-31 13:43:22 +0200'::TIMESTAMP_TZ, 'ntz', '2023-05-22 01:17:19'::TIMESTAMP_NTZ, 'so', {'s': 'child', 'i': 9}::OBJECT, 'sArr', ARRAY_CONSTRUCT('x', 'y', 'z'), 'f64Arr', ARRAY_CONSTRUCT(1.1, 2.2, 3.3), 'someMap', {'x': true, 'y': false}, 'uuid', '%s')::OBJECT(s VARCHAR, b TINYINT, i16 SMALLINT, i32 INTEGER, i64 BIGINT, f64 DOUBLE, bo BOOLEAN, bi BINARY, date DATE, time TIME, ltz TIMESTAMP_LTZ, tz TIMESTAMP_TZ, ntz TIMESTAMP_NTZ, so OBJECT(s VARCHAR, i INTEGER), sArr ARRAY(VARCHAR), f64Arr ARRAY(DOUBLE), someMap MAP(VARCHAR, BOOLEAN), uuid VARCHAR)", uid))
defer rows.Close()
rows.Next()
var ignore sql.NullInt32
Expand Down Expand Up @@ -553,7 +565,11 @@ type objectWithAllTypesSimpleScan struct {
}

func (so *objectWithAllTypesSimpleScan) Scan(val any) error {
st := val.(StructuredObject)
st, ok := val.(StructuredObject)
if !ok {
return fmt.Errorf("expected StructuredObject, got %T", val)
}

return st.ScanTo(so)
}

Expand Down Expand Up @@ -651,7 +667,11 @@ type objectWithAllTypesNullableSimpleScan struct {
}

func (o *objectWithAllTypesNullableSimpleScan) Scan(val any) error {
st := val.(StructuredObject)
st, ok := val.(StructuredObject)
if !ok {
return fmt.Errorf("expected StructuredObject, got %T", val)
}

return st.ScanTo(o)
}

Expand Down Expand Up @@ -737,7 +757,11 @@ type objectWithCustomNameAndIgnoredField struct {
}

func (o *objectWithCustomNameAndIgnoredField) Scan(val any) error {
st := val.(StructuredObject)
st, ok := val.(StructuredObject)
if !ok {
return fmt.Errorf("expected StructuredObject, got %T", val)
}

return st.ScanTo(o)
}

Expand Down Expand Up @@ -1839,7 +1863,11 @@ type HigherPrecisionStruct struct {
}

func (hps *HigherPrecisionStruct) Scan(val any) error {
st := val.(StructuredObject)
st, ok := val.(StructuredObject)
if !ok {
return fmt.Errorf("expected StructuredObject, got %T", val)
}

var err error
if hps.i, err = st.GetBigInt("i"); err != nil {
return err
Expand Down

0 comments on commit 44119de

Please sign in to comment.