github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/table/table_test.go (about)

     1  package table_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     8  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Formats"
     9  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table"
    10  
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
    12  	"github.com/ydb-platform/ydb-go-sdk/v3/table"
    13  	"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
    14  )
    15  
    16  func TestQueryParameters_String(t *testing.T) {
    17  	for _, tt := range []struct {
    18  		p *table.QueryParameters
    19  		s string
    20  	}{
    21  		{
    22  			p: nil,
    23  			s: `{}`,
    24  		},
    25  		{
    26  			p: table.NewQueryParameters(),
    27  			s: `{}`,
    28  		},
    29  		{
    30  			p: table.NewQueryParameters(
    31  				table.ValueParam("$a", types.TextValue("test")),
    32  			),
    33  			s: `{"$a":"test"u}`,
    34  		},
    35  		{
    36  			p: table.NewQueryParameters(
    37  				table.ValueParam("$a", types.TextValue("test")),
    38  				table.ValueParam("$b", types.BytesValue([]byte("test"))),
    39  			),
    40  			s: `{"$a":"test"u,"$b":"test"}`,
    41  		},
    42  		{
    43  			p: table.NewQueryParameters(
    44  				table.ValueParam("$a", types.TextValue("test")),
    45  				table.ValueParam("$b", types.BytesValue([]byte("test"))),
    46  				table.ValueParam("$c", types.Uint64Value(123456)),
    47  			),
    48  			s: `{"$a":"test"u,"$b":"test","$c":123456ul}`,
    49  		},
    50  		{
    51  			p: table.NewQueryParameters(
    52  				table.ValueParam("$a", types.TextValue("test")),
    53  				table.ValueParam("$b", types.BytesValue([]byte("test"))),
    54  				table.ValueParam("$c", types.Uint64Value(123456)),
    55  				table.ValueParam("$d", types.StructValue(
    56  					types.StructFieldValue("$a", types.TextValue("test")),
    57  					types.StructFieldValue("$b", types.BytesValue([]byte("test"))),
    58  					types.StructFieldValue("$c", types.Uint64Value(123456)),
    59  				)),
    60  			),
    61  			s: "{\"$a\":\"test\"u,\"$b\":\"test\",\"$c\":123456ul,\"$d\":<|`$a`:\"test\"u,`$b`:\"test\",`$c`:123456ul|>}",
    62  		},
    63  	} {
    64  		t.Run("", func(t *testing.T) {
    65  			require.Equal(t, tt.s, tt.p.String())
    66  		})
    67  	}
    68  }
    69  
    70  func TestBulkUpsertData(t *testing.T) {
    71  	for _, tt := range []struct {
    72  		name    string
    73  		data    table.BulkUpsertData
    74  		request *Ydb_Table.BulkUpsertRequest
    75  	}{
    76  		{
    77  			name: "Rows",
    78  			data: table.BulkUpsertDataRows(types.ListValue(
    79  				types.Uint64Value(123),
    80  				types.Uint64Value(321),
    81  			)),
    82  			request: &Ydb_Table.BulkUpsertRequest{
    83  				Table: "test",
    84  				Rows: &Ydb.TypedValue{
    85  					Type: &Ydb.Type{
    86  						Type: &Ydb.Type_ListType{
    87  							ListType: &Ydb.ListType{
    88  								Item: &Ydb.Type{Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_UINT64}},
    89  							},
    90  						},
    91  					},
    92  					Value: &Ydb.Value{
    93  						Items: []*Ydb.Value{
    94  							{
    95  								Value: &Ydb.Value_Uint64Value{
    96  									Uint64Value: 123,
    97  								},
    98  							},
    99  							{
   100  								Value: &Ydb.Value_Uint64Value{
   101  									Uint64Value: 321,
   102  								},
   103  							},
   104  						},
   105  					},
   106  				},
   107  			},
   108  		},
   109  		{
   110  			name: "Csv",
   111  			data: table.BulkUpsertDataCsv([]byte("123")),
   112  			request: &Ydb_Table.BulkUpsertRequest{
   113  				Table: "test",
   114  				Data:  []byte("123"),
   115  				DataFormat: &Ydb_Table.BulkUpsertRequest_CsvSettings{
   116  					CsvSettings: &Ydb_Formats.CsvSettings{},
   117  				},
   118  			},
   119  		},
   120  		{
   121  			name: "CsvWithDelimeter",
   122  			data: table.BulkUpsertDataCsv([]byte("123"), table.WithCsvDelimiter([]byte(";"))),
   123  			request: &Ydb_Table.BulkUpsertRequest{
   124  				Table: "test",
   125  				Data:  []byte("123"),
   126  				DataFormat: &Ydb_Table.BulkUpsertRequest_CsvSettings{
   127  					CsvSettings: &Ydb_Formats.CsvSettings{
   128  						Delimiter: []byte(";"),
   129  					},
   130  				},
   131  			},
   132  		},
   133  		{
   134  			name: "CsvWithHeader",
   135  			data: table.BulkUpsertDataCsv([]byte("123"), table.WithCsvHeader()),
   136  			request: &Ydb_Table.BulkUpsertRequest{
   137  				Table: "test",
   138  				Data:  []byte("123"),
   139  				DataFormat: &Ydb_Table.BulkUpsertRequest_CsvSettings{
   140  					CsvSettings: &Ydb_Formats.CsvSettings{
   141  						Header: true,
   142  					},
   143  				},
   144  			},
   145  		},
   146  		{
   147  			name: "CsvWithNullValue",
   148  			data: table.BulkUpsertDataCsv([]byte("123"), table.WithCsvNullValue([]byte("null"))),
   149  			request: &Ydb_Table.BulkUpsertRequest{
   150  				Table: "test",
   151  				Data:  []byte("123"),
   152  				DataFormat: &Ydb_Table.BulkUpsertRequest_CsvSettings{
   153  					CsvSettings: &Ydb_Formats.CsvSettings{
   154  						NullValue: []byte("null"),
   155  					},
   156  				},
   157  			},
   158  		},
   159  		{
   160  			name: "CsvWithNullValue",
   161  			data: table.BulkUpsertDataCsv([]byte("123"), table.WithCsvSkipRows(30)),
   162  			request: &Ydb_Table.BulkUpsertRequest{
   163  				Table: "test",
   164  				Data:  []byte("123"),
   165  				DataFormat: &Ydb_Table.BulkUpsertRequest_CsvSettings{
   166  					CsvSettings: &Ydb_Formats.CsvSettings{
   167  						SkipRows: 30,
   168  					},
   169  				},
   170  			},
   171  		},
   172  		{
   173  			name: "Arrow",
   174  			data: table.BulkUpsertDataArrow([]byte("123")),
   175  			request: &Ydb_Table.BulkUpsertRequest{
   176  				Table: "test",
   177  				Data:  []byte("123"),
   178  				DataFormat: &Ydb_Table.BulkUpsertRequest_ArrowBatchSettings{
   179  					ArrowBatchSettings: &Ydb_Formats.ArrowBatchSettings{},
   180  				},
   181  			},
   182  		},
   183  		{
   184  			name: "ArrowWithSchema",
   185  			data: table.BulkUpsertDataArrow([]byte("123"),
   186  				table.WithArrowSchema([]byte("schema")),
   187  			),
   188  			request: &Ydb_Table.BulkUpsertRequest{
   189  				Table: "test",
   190  				Data:  []byte("123"),
   191  				DataFormat: &Ydb_Table.BulkUpsertRequest_ArrowBatchSettings{
   192  					ArrowBatchSettings: &Ydb_Formats.ArrowBatchSettings{
   193  						Schema: []byte("schema"),
   194  					},
   195  				},
   196  			},
   197  		},
   198  	} {
   199  		t.Run(tt.name, func(t *testing.T) {
   200  			a := allocator.New()
   201  			request, err := tt.data.ToYDB(a, "test")
   202  			require.NoError(t, err)
   203  			require.Equal(t,
   204  				tt.request.String(),
   205  				request.String(),
   206  			)
   207  		})
   208  	}
   209  }