@@ -3,6 +3,7 @@ package connection_pool_test
3
3
import (
4
4
"log"
5
5
"os"
6
+ "reflect"
6
7
"strings"
7
8
"testing"
8
9
"time"
@@ -1276,6 +1277,74 @@ func TestDo(t *testing.T) {
1276
1277
require .NotNilf (t , resp , "response is nil after Ping" )
1277
1278
}
1278
1279
1280
+ func TestNewPrepareStatement (t * testing.T ) {
1281
+ // Tarantool supports SQL since version 2.0.0
1282
+ isLess , err := test_helpers .IsTarantoolVersionLess (2 , 0 , 0 )
1283
+ if err != nil {
1284
+ t .Fatalf ("Could not check the Tarantool version" )
1285
+ }
1286
+ if isLess {
1287
+ t .Skip ()
1288
+ }
1289
+
1290
+ roles := []bool {true , true , false , true , false }
1291
+
1292
+ err = test_helpers .SetClusterRO (servers , connOpts , roles )
1293
+ require .Nilf (t , err , "fail to set roles for cluster" )
1294
+
1295
+ connPool , err := connection_pool .Connect (servers , connOpts )
1296
+ require .Nilf (t , err , "failed to connect" )
1297
+ require .NotNilf (t , connPool , "conn is nil after Connect" )
1298
+
1299
+ defer connPool .Close ()
1300
+
1301
+ stmt , err := connPool .NewPrepareStatement ("SELECT NAME0, NAME1 FROM SQL_TEST WHERE NAME0=:id AND NAME1=:name;" , connection_pool .ANY )
1302
+ require .Nilf (t , err , "fail to prepare statement: %v" , err )
1303
+
1304
+ executeReq := tarantool .NewExecutePreparedRequest (stmt )
1305
+ unprepareReq := tarantool .NewUnprepareRequest (stmt )
1306
+
1307
+ resp , err := connPool .Do (executeReq .Args ([]interface {}{1 , "test" }), connection_pool .ANY ).Get ()
1308
+ if err != nil {
1309
+ t .Fatalf ("failed to execute prepared: %v" , err )
1310
+ }
1311
+ if resp == nil {
1312
+ t .Fatalf ("nil response" )
1313
+ }
1314
+ if resp .Code != tarantool .OkCode {
1315
+ t .Fatalf ("failed to execute prepared: code %d" , resp .Code )
1316
+ }
1317
+ if reflect .DeepEqual (resp .Data [0 ], []interface {}{1 , "test" }) {
1318
+ t .Error ("Select with named arguments failed" )
1319
+ }
1320
+ if resp .MetaData [0 ].FieldType != "unsigned" ||
1321
+ resp .MetaData [0 ].FieldName != "NAME0" ||
1322
+ resp .MetaData [1 ].FieldType != "string" ||
1323
+ resp .MetaData [1 ].FieldName != "NAME1" {
1324
+ t .Error ("Wrong metadata" )
1325
+ }
1326
+
1327
+ resp , err = connPool .Do (unprepareReq , connection_pool .ANY ).Get ()
1328
+ if err != nil {
1329
+ t .Errorf ("failed to unprepare prepared statement: %v" , err )
1330
+ }
1331
+ if resp .Code != tarantool .OkCode {
1332
+ t .Errorf ("failed to unprepare prepared statement: code %d" , resp .Code )
1333
+ }
1334
+
1335
+ _ , err = connPool .Do (unprepareReq , connection_pool .ANY ).Get ()
1336
+ if err == nil {
1337
+ t .Errorf ("the statement must be already unprepared" )
1338
+ }
1339
+ require .Contains (t , err .Error (), "Prepared statement with id" )
1340
+
1341
+ _ , err = connPool .Do (executeReq , connection_pool .ANY ).Get ()
1342
+ if err == nil {
1343
+ t .Errorf ("the statement must be already unprepared" )
1344
+ }
1345
+ require .Contains (t , err .Error (), "Prepared statement with id" )
1346
+ }
1347
+
1279
1348
// runTestMain is a body of TestMain function
1280
1349
// (see https://pkg.go.dev/testing#hdr-Main).
1281
1350
// Using defer + os.Exit is not works so TestMain body
0 commit comments