jet/tests/mysql/cast_test.go

72 lines
2 KiB
Go
Raw Normal View History

2019-07-31 18:43:54 +02:00
package mysql
import (
"github.com/go-jet/jet/internal/testutils"
. "github.com/go-jet/jet/mysql"
. "github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/table"
"github.com/stretchr/testify/assert"
2019-07-31 18:43:54 +02:00
"testing"
"time"
)
func TestCast(t *testing.T) {
query := SELECT(
2019-08-01 10:39:57 +02:00
CAST(String("test")).AS("CHAR CHARACTER SET utf8").AS("result.AS1"),
2019-08-02 11:08:24 +02:00
CAST(String("2011-02-02")).AS_DATE().AS("result.date1"),
2019-07-31 18:43:54 +02:00
CAST(String("14:06:10")).AS_TIME().AS("result.time"),
CAST(String("2011-02-02 14:06:10")).AS_DATETIME().AS("result.datetime"),
2019-08-01 10:39:57 +02:00
CAST(Int(150)).AS_CHAR().AS("result.char1"),
CAST(Int(150)).AS_CHAR(30).AS("result.char2"),
2019-07-31 18:43:54 +02:00
CAST(Int(5).SUB(Int(10))).AS_SIGNED().AS("result.signed"),
CAST(Int(5).ADD(Int(10))).AS_UNSIGNED().AS("result.unsigned"),
CAST(String("Some text")).AS_BINARY().AS("result.binary"),
).FROM(AllTypes)
testutils.AssertStatementSql(t, query, `
2019-08-01 10:39:57 +02:00
SELECT CAST(? AS CHAR CHARACTER SET utf8) AS "result.AS1",
2019-08-02 11:08:24 +02:00
CAST(? AS DATE) AS "result.date1",
2019-07-31 18:43:54 +02:00
CAST(? AS TIME) AS "result.time",
CAST(? AS DATETIME) AS "result.datetime",
2019-08-01 10:39:57 +02:00
CAST(? AS CHAR) AS "result.char1",
CAST(? AS CHAR(30)) AS "result.char2",
2019-07-31 18:43:54 +02:00
CAST((? - ?) AS SIGNED) AS "result.signed",
CAST((? + ?) AS UNSIGNED) AS "result.unsigned",
CAST(? AS BINARY) AS "result.binary"
FROM test_sample.all_types;
2019-08-01 10:39:57 +02:00
`, "test", "2011-02-02", "14:06:10", "2011-02-02 14:06:10", int64(150), int64(150), int64(5),
int64(10), int64(5), int64(10), "Some text")
2019-07-31 18:43:54 +02:00
type Result struct {
2019-08-01 10:39:57 +02:00
As1 string
2019-08-02 11:08:24 +02:00
Date1 time.Time
2019-07-31 18:43:54 +02:00
Time time.Time
DateTime time.Time
2019-08-01 10:39:57 +02:00
Char1 string
Char2 string
2019-07-31 18:43:54 +02:00
Signed int
Unsigned int
Binary string
}
var dest Result
err := query.Query(db, &dest)
assert.NoError(t, err)
2019-07-31 18:43:54 +02:00
testutils.AssertDeepEqual(t, dest, Result{
2019-08-01 10:39:57 +02:00
As1: "test",
2019-08-02 11:08:24 +02:00
Date1: *testutils.Date("2011-02-02"),
2019-07-31 18:43:54 +02:00
Time: *testutils.TimeWithoutTimeZone("14:06:10"),
DateTime: *testutils.TimestampWithoutTimeZone("2011-02-02 14:06:10", 0),
2019-08-01 10:39:57 +02:00
Char1: "150",
Char2: "150",
2019-07-31 18:43:54 +02:00
Signed: -5,
Unsigned: 15,
Binary: "Some text",
})
}