github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/params/pg_test.go (about)

     1  package params
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     8  
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/pg"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
    12  )
    13  
    14  func TestPg(t *testing.T) {
    15  	type expected struct {
    16  		Type  *Ydb.Type
    17  		Value *Ydb.Value
    18  	}
    19  
    20  	tests := []struct {
    21  		method string
    22  		args   []any
    23  
    24  		expected expected
    25  	}{
    26  		{
    27  			method: "Unknown",
    28  			args:   []any{"123"},
    29  
    30  			expected: expected{
    31  				Type: &Ydb.Type{
    32  					Type: &Ydb.Type_PgType{
    33  						PgType: &Ydb.PgType{
    34  							Oid: pg.OIDUnknown,
    35  						},
    36  					},
    37  				},
    38  				Value: &Ydb.Value{
    39  					Value: &Ydb.Value_TextValue{TextValue: "123"},
    40  				},
    41  			},
    42  		},
    43  		{
    44  			method: "Int4",
    45  			args:   []any{int32(123)},
    46  
    47  			expected: expected{
    48  				Type: &Ydb.Type{
    49  					Type: &Ydb.Type_PgType{
    50  						PgType: &Ydb.PgType{
    51  							Oid: pg.OIDInt4,
    52  						},
    53  					},
    54  				},
    55  				Value: &Ydb.Value{
    56  					Value: &Ydb.Value_TextValue{TextValue: "123"},
    57  				},
    58  			},
    59  		},
    60  		{
    61  			method: "Int8",
    62  			args:   []any{int64(123)},
    63  
    64  			expected: expected{
    65  				Type: &Ydb.Type{
    66  					Type: &Ydb.Type_PgType{
    67  						PgType: &Ydb.PgType{
    68  							Oid: pg.OIDInt8,
    69  						},
    70  					},
    71  				},
    72  				Value: &Ydb.Value{
    73  					Value: &Ydb.Value_TextValue{TextValue: "123"},
    74  				},
    75  			},
    76  		},
    77  	}
    78  
    79  	for _, tc := range tests {
    80  		t.Run(tc.method, func(t *testing.T) {
    81  			a := allocator.New()
    82  			defer a.Free()
    83  
    84  			item := Builder{}.Param("$x").Pg()
    85  
    86  			result, ok := xtest.CallMethod(item, tc.method, tc.args...)[0].(Builder)
    87  			require.True(t, ok)
    88  
    89  			params := result.Build().ToYDB(a)
    90  
    91  			require.Equal(t, xtest.ToJSON(
    92  				map[string]*Ydb.TypedValue{
    93  					"$x": {
    94  						Type:  tc.expected.Type,
    95  						Value: tc.expected.Value,
    96  					},
    97  				}), xtest.ToJSON(params))
    98  		})
    99  	}
   100  }