vitess.io/vitess@v0.16.2/go/vt/vtgate/endtoend/last_insert_id_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package endtoend
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"vitess.io/vitess/go/vt/vtgate/evalengine"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"vitess.io/vitess/go/mysql"
    30  )
    31  
    32  func TestLastInsertId(t *testing.T) {
    33  	ctx := context.Background()
    34  	conn, err := mysql.Connect(ctx, &vtParams)
    35  	require.NoError(t, err)
    36  	defer conn.Close()
    37  
    38  	// figure out the last inserted id before we run change anything
    39  	qr := exec(t, conn, "select max(id) from t1_last_insert_id")
    40  	oldLastID, err := evalengine.ToUint64(qr.Rows[0][0])
    41  	require.NoError(t, err)
    42  
    43  	exec(t, conn, "insert into t1_last_insert_id(id1) values(42)")
    44  
    45  	// even without a transaction, we should get the last inserted id back
    46  	qr = exec(t, conn, "select last_insert_id()")
    47  	got := fmt.Sprintf("%v", qr.Rows)
    48  	want := fmt.Sprintf("[[UINT64(%d)]]", oldLastID+1)
    49  
    50  	if diff := cmp.Diff(want, got); diff != "" {
    51  		t.Error(diff)
    52  	}
    53  }
    54  
    55  func TestLastInsertIdWithRollback(t *testing.T) {
    56  	ctx := context.Background()
    57  	conn, err := mysql.Connect(ctx, &vtParams)
    58  	require.NoError(t, err)
    59  	defer conn.Close()
    60  
    61  	// figure out the last inserted id before we run our tests
    62  	qr := exec(t, conn, "select max(id) from t1_last_insert_id")
    63  	oldLastID, err := evalengine.ToUint64(qr.Rows[0][0])
    64  	require.NoError(t, err)
    65  
    66  	// add row inside explicit transaction
    67  	exec(t, conn, "begin")
    68  	exec(t, conn, "insert into t1_last_insert_id(id1) values(42)")
    69  	qr = exec(t, conn, "select last_insert_id()")
    70  	got := fmt.Sprintf("%v", qr.Rows)
    71  	want := fmt.Sprintf("[[UINT64(%d)]]", oldLastID+1)
    72  
    73  	if diff := cmp.Diff(want, got); diff != "" {
    74  		t.Error(diff)
    75  	}
    76  	// even if we do a rollback, we should still get the same last_insert_id
    77  	exec(t, conn, "rollback")
    78  	qr = exec(t, conn, "select last_insert_id()")
    79  	got = fmt.Sprintf("%v", qr.Rows)
    80  	if diff := cmp.Diff(want, got); diff != "" {
    81  		t.Error(diff)
    82  	}
    83  }