2019-08-02 11:29:38 +02:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/go-jet/jet/internal/testutils"
|
2019-08-03 14:10:47 +02:00
|
|
|
. "github.com/go-jet/jet/mysql"
|
2019-08-02 11:29:38 +02:00
|
|
|
"github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/model"
|
|
|
|
|
. "github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/table"
|
2020-05-09 11:00:22 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-02 11:29:38 +02:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDeleteWithWhere(t *testing.T) {
|
|
|
|
|
initForDeleteTest(t)
|
|
|
|
|
|
|
|
|
|
var expectedSQL = `
|
|
|
|
|
DELETE FROM test_sample.link
|
|
|
|
|
WHERE link.name IN ('Gmail', 'Outlook');
|
|
|
|
|
`
|
|
|
|
|
deleteStmt := Link.
|
|
|
|
|
DELETE().
|
|
|
|
|
WHERE(Link.Name.IN(String("Gmail"), String("Outlook")))
|
|
|
|
|
|
|
|
|
|
testutils.AssertDebugStatementSql(t, deleteStmt, expectedSQL, "Gmail", "Outlook")
|
|
|
|
|
testutils.AssertExec(t, deleteStmt, db, 2)
|
2020-05-10 11:41:07 +02:00
|
|
|
requireLogged(t, deleteStmt)
|
2019-08-02 11:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-11 18:23:02 +02:00
|
|
|
func TestDeleteWithWhereOrderByLimit(t *testing.T) {
|
|
|
|
|
initForDeleteTest(t)
|
|
|
|
|
|
|
|
|
|
var expectedSQL = `
|
|
|
|
|
DELETE FROM test_sample.link
|
|
|
|
|
WHERE link.name IN ('Gmail', 'Outlook')
|
|
|
|
|
ORDER BY link.name
|
|
|
|
|
LIMIT 1;
|
|
|
|
|
`
|
|
|
|
|
deleteStmt := Link.
|
|
|
|
|
DELETE().
|
|
|
|
|
WHERE(Link.Name.IN(String("Gmail"), String("Outlook"))).
|
|
|
|
|
ORDER_BY(Link.Name).
|
|
|
|
|
LIMIT(1)
|
|
|
|
|
|
|
|
|
|
testutils.AssertDebugStatementSql(t, deleteStmt, expectedSQL, "Gmail", "Outlook", int64(1))
|
|
|
|
|
testutils.AssertExec(t, deleteStmt, db, 1)
|
2020-05-10 11:41:07 +02:00
|
|
|
requireLogged(t, deleteStmt)
|
2019-08-11 18:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-02 11:29:38 +02:00
|
|
|
func TestDeleteQueryContext(t *testing.T) {
|
|
|
|
|
initForDeleteTest(t)
|
|
|
|
|
|
|
|
|
|
deleteStmt := Link.
|
|
|
|
|
DELETE().
|
|
|
|
|
WHERE(Link.Name.IN(String("Gmail"), String("Outlook")))
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
dest := []model.Link{}
|
|
|
|
|
err := deleteStmt.QueryContext(ctx, db, &dest)
|
|
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.Error(t, err, "context deadline exceeded")
|
2020-05-10 11:41:07 +02:00
|
|
|
requireLogged(t, deleteStmt)
|
2019-08-02 11:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDeleteExecContext(t *testing.T) {
|
|
|
|
|
initForDeleteTest(t)
|
|
|
|
|
|
|
|
|
|
deleteStmt := Link.
|
|
|
|
|
DELETE().
|
|
|
|
|
WHERE(Link.Name.IN(String("Gmail"), String("Outlook")))
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
_, err := deleteStmt.ExecContext(ctx, db)
|
|
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.Error(t, err, "context deadline exceeded")
|
2019-08-02 11:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initForDeleteTest(t *testing.T) {
|
|
|
|
|
cleanUpLinkTable(t)
|
|
|
|
|
stmt := Link.INSERT(Link.URL, Link.Name, Link.Description).
|
|
|
|
|
VALUES("www.gmail.com", "Gmail", "Email service developed by Google").
|
|
|
|
|
VALUES("www.outlook.live.com", "Outlook", "Email service developed by Microsoft")
|
|
|
|
|
|
|
|
|
|
testutils.AssertExec(t, stmt, db, 2)
|
|
|
|
|
}
|