mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql: reset session variables when query is done (#4364)
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -16,9 +18,10 @@ func TestWithVars(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
require.NoError(t, err)
|
||||
db.SetMaxOpenConns(1)
|
||||
drv := OpenDB("sqlite3", db)
|
||||
drv := OpenDB(dialect.Postgres, db)
|
||||
mock.ExpectExec("SET foo = 'bar'").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectQuery("SELECT 1").WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1))
|
||||
mock.ExpectExec("RESET foo").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
rows := &Rows{}
|
||||
err = drv.Query(
|
||||
WithVar(context.Background(), "foo", "bar"),
|
||||
@@ -27,12 +30,13 @@ func TestWithVars(t *testing.T) {
|
||||
rows,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
require.NoError(t, rows.Close(), "rows should be closed to release the connection")
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
|
||||
mock.ExpectExec("SET foo = 'bar'").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("SET foo = 'baz'").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectQuery("SELECT 1").WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1))
|
||||
mock.ExpectExec("RESET foo").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
err = drv.Query(
|
||||
WithVar(WithVar(context.Background(), "foo", "bar"), "foo", "baz"),
|
||||
"SELECT 1",
|
||||
@@ -40,8 +44,8 @@ func TestWithVars(t *testing.T) {
|
||||
rows,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
require.NoError(t, rows.Close(), "rows should be closed to release the connection")
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("SET foo = 'bar'").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
@@ -63,6 +67,7 @@ func TestWithVars(t *testing.T) {
|
||||
|
||||
mock.ExpectExec("SET foo = 'qux'").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("INSERT INTO users DEFAULT VALUES").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("RESET foo").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
err = drv.Exec(
|
||||
WithVar(context.Background(), "foo", "qux"),
|
||||
"INSERT INTO users DEFAULT VALUES",
|
||||
@@ -75,6 +80,7 @@ func TestWithVars(t *testing.T) {
|
||||
|
||||
mock.ExpectExec("SET foo = 'foo'").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("INSERT INTO users DEFAULT VALUES").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("RESET foo").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
err = drv.Exec(
|
||||
WithVar(context.Background(), "foo", "foo"),
|
||||
"INSERT INTO users DEFAULT VALUES",
|
||||
|
||||
Reference in New Issue
Block a user