@@ -17,7 +17,7 @@ import (
1717 "github.com/ydb-platform/ydb-go-sdk/v3/table/types"
1818)
1919
20- func TestTableBulkUpsert (t * testing.T ) {
20+ func TestTableBulkUpsertSession (t * testing.T ) {
2121 var (
2222 scope = newScope (t )
2323 driver = scope .Driver ()
@@ -39,58 +39,40 @@ func TestTableBulkUpsert(t *testing.T) {
3939 return s .BulkUpsert (ctx , tablePath , types .ListValue (rows ... ))
4040 })
4141 scope .Require .NoError (err )
42- }
4342
44- func assertIdValueImpl (ctx context.Context , t * testing.T , tableName string , id int64 , val * string ) {
45- ctx , cancel := context .WithCancel (context .Background ())
46- defer cancel ()
43+ for i := int64 (0 ); i < 10 ; i ++ {
44+ val := fmt .Sprintf ("value for %v" , i )
45+ assertIdValue (scope .Ctx , t , tablePath , i , val )
46+ }
47+ }
4748
48- db , err := ydb .Open (ctx ,
49- os .Getenv ("YDB_CONNECTION_STRING" ),
50- // ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
49+ func TestTableBulkUpsert (t * testing.T ) {
50+ var (
51+ scope = newScope (t )
52+ driver = scope .Driver ()
53+ tablePath = scope .TablePath ()
5154 )
52- require .NoError (t , err )
53- err = db .Table ().DoTx (ctx , func (ctx context.Context , tx table.TransactionActor ) (err error ) {
54- res , err := tx .Execute (ctx , fmt .Sprintf ("SELECT val FROM `%s` WHERE id = %d" , tableName , id ), nil )
55- if err != nil {
56- return err
57- }
58- err = res .NextResultSetErr (ctx )
59- if err != nil {
60- return err
61- }
62- require .EqualValues (t , 1 , res .ResultSetCount ())
63- if ! res .NextRow () {
64- if err = res .Err (); err != nil {
65- return err
66- }
67- return fmt .Errorf ("unexpected empty result set" )
68- }
69- var resultVal * string
70- err = res .ScanNamed (
71- named .Optional ("val" , & resultVal ),
72- )
73- if err != nil {
74- return err
75- }
76- if val != nil {
77- require .NotEmpty (t , resultVal )
78- require .EqualValues (t , * val , * resultVal )
79- } else {
80- require .Nil (t , resultVal )
81- }
8255
83- return res .Err ()
84- }, table .WithTxSettings (table .TxSettings (table .WithSnapshotReadOnly ())), table .WithIdempotent ())
85- require .NoError (t , err )
86- }
56+ // upsert
57+ var rows []types.Value
8758
88- func assertIdValue (ctx context.Context , t * testing.T , tableName string , id int64 , val string ) {
89- assertIdValueImpl (ctx , t , tableName , id , & val )
90- }
59+ for i := int64 (0 ); i < 10 ; i ++ {
60+ val := fmt .Sprintf ("value for %v" , i )
61+ rows = append (rows , types .StructValue (
62+ types .StructFieldValue ("id" , types .Int64Value (i )),
63+ types .StructFieldValue ("val" , types .TextValue (val )),
64+ ))
65+ }
9166
92- func assertIdValueNil (ctx context.Context , t * testing.T , tableName string , id int64 ) {
93- assertIdValueImpl (ctx , t , tableName , id , nil )
67+ err := driver .Table ().BulkUpsert (scope .Ctx , tablePath , table .NewBulkUpsertRows (
68+ types .ListValue (rows ... ),
69+ ))
70+ scope .Require .NoError (err )
71+
72+ for i := int64 (0 ); i < 10 ; i ++ {
73+ val := fmt .Sprintf ("value for %v" , i )
74+ assertIdValue (scope .Ctx , t , tablePath , i , val )
75+ }
9476}
9577
9678func TestTableCsvBulkUpsert (t * testing.T ) {
@@ -209,3 +191,55 @@ func TestTableArrowBulkUpsert(t *testing.T) {
209191 assertIdValue (scope .Ctx , t , tablePath , 123 , "data1" )
210192 assertIdValue (scope .Ctx , t , tablePath , 234 , "data2" )
211193}
194+
195+ func assertIdValueImpl (ctx context.Context , t * testing.T , tableName string , id int64 , val * string ) {
196+ ctx , cancel := context .WithCancel (context .Background ())
197+ defer cancel ()
198+
199+ db , err := ydb .Open (ctx ,
200+ os .Getenv ("YDB_CONNECTION_STRING" ),
201+ // ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
202+ )
203+ require .NoError (t , err )
204+ err = db .Table ().DoTx (ctx , func (ctx context.Context , tx table.TransactionActor ) (err error ) {
205+ res , err := tx .Execute (ctx , fmt .Sprintf ("SELECT val FROM `%s` WHERE id = %d" , tableName , id ), nil )
206+ if err != nil {
207+ return err
208+ }
209+ err = res .NextResultSetErr (ctx )
210+ if err != nil {
211+ return err
212+ }
213+ require .EqualValues (t , 1 , res .ResultSetCount ())
214+ if ! res .NextRow () {
215+ if err = res .Err (); err != nil {
216+ return err
217+ }
218+ return fmt .Errorf ("unexpected empty result set" )
219+ }
220+ var resultVal * string
221+ err = res .ScanNamed (
222+ named .Optional ("val" , & resultVal ),
223+ )
224+ if err != nil {
225+ return err
226+ }
227+ if val != nil {
228+ require .NotEmpty (t , resultVal )
229+ require .EqualValues (t , * val , * resultVal )
230+ } else {
231+ require .Nil (t , resultVal )
232+ }
233+
234+ return res .Err ()
235+ }, table .WithTxSettings (table .TxSettings (table .WithSnapshotReadOnly ())), table .WithIdempotent ())
236+ require .NoError (t , err )
237+ }
238+
239+ func assertIdValue (ctx context.Context , t * testing.T , tableName string , id int64 , val string ) {
240+ assertIdValueImpl (ctx , t , tableName , id , & val )
241+ }
242+
243+ func assertIdValueNil (ctx context.Context , t * testing.T , tableName string , id int64 ) {
244+ assertIdValueImpl (ctx , t , tableName , id , nil )
245+ }
0 commit comments