@@ -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,72 @@ func TestDo(t *testing.T) {
1276
1277
require .NotNilf (t , resp , "response is nil after Ping" )
1277
1278
}
1278
1279
1280
+ func TestNewPrepared (t * testing.T ) {
1281
+ test_helpers .SkipIfSQLUnsupported (t )
1282
+
1283
+ roles := []bool {true , true , false , true , false }
1284
+
1285
+ err := test_helpers .SetClusterRO (servers , connOpts , roles )
1286
+ require .Nilf (t , err , "fail to set roles for cluster" )
1287
+
1288
+ connPool , err := connection_pool .Connect (servers , connOpts )
1289
+ require .Nilf (t , err , "failed to connect" )
1290
+ require .NotNilf (t , connPool , "conn is nil after Connect" )
1291
+
1292
+ defer connPool .Close ()
1293
+
1294
+ stmt , err := connPool .NewPrepared ("SELECT NAME0, NAME1 FROM SQL_TEST WHERE NAME0=:id AND NAME1=:name;" , connection_pool .RO )
1295
+ require .Nilf (t , err , "fail to prepare statement: %v" , err )
1296
+
1297
+ if connPool .GetPoolInfo ()[stmt .Conn .Addr ()].ConnRole != connection_pool .RO {
1298
+ t .Errorf ("wrong role for the statement's connection" )
1299
+ }
1300
+
1301
+ executeReq := tarantool .NewExecutePreparedRequest (stmt )
1302
+ unprepareReq := tarantool .NewUnprepareRequest (stmt )
1303
+
1304
+ resp , err := connPool .Do (executeReq .Args ([]interface {}{1 , "test" }), connection_pool .ANY ).Get ()
1305
+ if err != nil {
1306
+ t .Fatalf ("failed to execute prepared: %v" , err )
1307
+ }
1308
+ if resp == nil {
1309
+ t .Fatalf ("nil response" )
1310
+ }
1311
+ if resp .Code != tarantool .OkCode {
1312
+ t .Fatalf ("failed to execute prepared: code %d" , resp .Code )
1313
+ }
1314
+ if reflect .DeepEqual (resp .Data [0 ], []interface {}{1 , "test" }) {
1315
+ t .Error ("Select with named arguments failed" )
1316
+ }
1317
+ if resp .MetaData [0 ].FieldType != "unsigned" ||
1318
+ resp .MetaData [0 ].FieldName != "NAME0" ||
1319
+ resp .MetaData [1 ].FieldType != "string" ||
1320
+ resp .MetaData [1 ].FieldName != "NAME1" {
1321
+ t .Error ("Wrong metadata" )
1322
+ }
1323
+
1324
+ // the second argument for unprepare request is unused - it already belongs to some connection
1325
+ resp , err = connPool .Do (unprepareReq , connection_pool .ANY ).Get ()
1326
+ if err != nil {
1327
+ t .Errorf ("failed to unprepare prepared statement: %v" , err )
1328
+ }
1329
+ if resp .Code != tarantool .OkCode {
1330
+ t .Errorf ("failed to unprepare prepared statement: code %d" , resp .Code )
1331
+ }
1332
+
1333
+ _ , err = connPool .Do (unprepareReq , connection_pool .ANY ).Get ()
1334
+ if err == nil {
1335
+ t .Errorf ("the statement must be already unprepared" )
1336
+ }
1337
+ require .Contains (t , err .Error (), "Prepared statement with id" )
1338
+
1339
+ _ , err = connPool .Do (executeReq , connection_pool .ANY ).Get ()
1340
+ if err == nil {
1341
+ t .Errorf ("the statement must be already unprepared" )
1342
+ }
1343
+ require .Contains (t , err .Error (), "Prepared statement with id" )
1344
+ }
1345
+
1279
1346
// runTestMain is a body of TestMain function
1280
1347
// (see https://pkg.go.dev/testing#hdr-Main).
1281
1348
// Using defer + os.Exit is not works so TestMain body
0 commit comments