[New] Constructor for time types, that accepts time.Time. (TimeT, TimezT, DateT, ...)

This commit is contained in:
go-jet 2019-08-06 11:41:45 +02:00
parent 647ef21aaf
commit 6ee2f45548
11 changed files with 182 additions and 22 deletions

View file

@ -1,7 +1,6 @@
package mysql
import (
"fmt"
"github.com/go-jet/jet/internal/testutils"
"github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/model"
. "github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/table"
@ -26,7 +25,7 @@ func TestAllTypes(t *testing.T) {
assert.NilError(t, err)
//testutils.JsonPrint(dest)
testutils.AssertJSON(t, allTypesJson, dest)
testutils.AssertJSON(t, dest, allTypesJson)
}
func TestExpressionOperators(t *testing.T) {
@ -71,7 +70,7 @@ LIMIT ?;
//testutils.JsonPrint(dest)
testutils.AssertJSON(t, `
testutils.AssertJSON(t, dest, `
[
{
"IsNull": false,
@ -90,7 +89,7 @@ LIMIT ?;
"NotInSelect": null
}
]
`, dest)
`)
}
func TestBoolOperators(t *testing.T) {
@ -452,7 +451,7 @@ func TestStringOperators(t *testing.T) {
//fmt.Println(query.Sql())
//fmt.Println(args[15])
fmt.Println(query.Sql())
// fmt.Println(query.Sql())
err := query.Query(db, &struct{}{})
@ -496,7 +495,7 @@ func TestTimeExpressions(t *testing.T) {
CURRENT_TIME(3),
)
fmt.Println(query.Sql())
//fmt.Println(query.Sql())
testutils.AssertStatementSql(t, query, `
SELECT ?,
@ -622,7 +621,7 @@ func TestDateTimeExpressions(t *testing.T) {
NOW(1),
)
fmt.Println(query.DebugSql())
//fmt.Println(query.DebugSql())
testutils.AssertDebugStatementSql(t, query, `
SELECT all_types.date_time = all_types.date_time,
@ -684,7 +683,7 @@ func TestTimestampExpressions(t *testing.T) {
CURRENT_TIMESTAMP(2),
)
fmt.Println(query.DebugSql())
//fmt.Println(query.DebugSql())
testutils.AssertDebugStatementSql(t, query, `
SELECT all_types.timestamp = all_types.timestamp,
@ -712,6 +711,51 @@ FROM test_sample.all_types;
assert.NilError(t, err)
}
func TestTimeLiterals(t *testing.T) {
loc, err := time.LoadLocation("Europe/Berlin")
assert.NilError(t, err)
var timeT = time.Date(2009, 11, 17, 20, 34, 58, 651387237, loc)
query := SELECT(
DateT(timeT).AS("date"),
TimeT(timeT).AS("time"),
DateTimeT(timeT).AS("datetime"),
TimestampT(timeT).AS("timestamp"),
).FROM(AllTypes).LIMIT(1)
//fmt.Println(query.Sql())
testutils.AssertStatementSql(t, query, `
SELECT CAST(? AS DATE) AS "date",
CAST(? AS TIME) AS "time",
CAST(? AS DATETIME) AS "datetime",
CAST(? AS DATETIME) AS "timestamp"
FROM test_sample.all_types
LIMIT ?;
`)
var dest struct {
Date time.Time
Time time.Time
DateTime time.Time
Timestamp time.Time
}
err = query.Query(db, &dest)
assert.NilError(t, err)
testutils.AssertJSON(t, dest, `
{
"Date": "2009-11-17T00:00:00Z",
"Time": "0000-01-01T19:34:59Z",
"DateTime": "2009-11-17T19:34:59Z",
"Timestamp": "2009-11-17T19:34:59Z"
}
`)
}
var allTypesJson = `
[
{

View file

@ -11,6 +11,7 @@ import (
"github.com/google/uuid"
"gotest.tools/assert"
"testing"
"time"
)
func TestAllTypesSelect(t *testing.T) {
@ -95,7 +96,7 @@ LIMIT $5;
//testutils.JsonPrint(dest)
testutils.AssertJSON(t, `
testutils.AssertJSON(t, dest, `
[
{
"IsNull": false,
@ -114,7 +115,7 @@ LIMIT $5;
"NotInSelect": null
}
]
`, dest)
`)
}
func TestExpressionCast(t *testing.T) {
@ -791,6 +792,54 @@ FROM`
}
}
func TestTimeLiterals(t *testing.T) {
loc, err := time.LoadLocation("Europe/Berlin")
assert.NilError(t, err)
var timeT = time.Date(2009, 11, 17, 20, 34, 58, 651387237, loc)
query := SELECT(
DateT(timeT).AS("date"),
TimeT(timeT).AS("time"),
TimezT(timeT).AS("timez"),
TimestampT(timeT).AS("timestamp"),
TimestampzT(timeT).AS("timestampz"),
).FROM(AllTypes).
LIMIT(1)
testutils.AssertStatementSql(t, query, `
SELECT $1::DATE AS "date",
$2::time without time zone AS "time",
$3::time with time zone AS "timez",
$4::timestamp without time zone AS "timestamp",
$5::timestamp with time zone AS "timestampz"
FROM test_sample.all_types
LIMIT $6;
`)
var dest struct {
Date time.Time
Time time.Time
Timez time.Time
Timestamp time.Time
Timestampz time.Time
}
err = query.Query(db, &dest)
assert.NilError(t, err)
testutils.AssertJSON(t, dest, `
{
"Date": "2009-11-17T00:00:00Z",
"Time": "0000-01-01T20:34:58.651387Z",
"Timez": "0000-01-01T20:34:58.651387+01:00",
"Timestamp": "2009-11-17T20:34:58.651387Z",
"Timestampz": "2009-11-17T20:34:58.651387+01:00"
}
`)
}
var allTypesRow0 = model.AllTypes{
SmallIntPtr: Int16Ptr(14),
SmallInt: 14,

View file

@ -142,7 +142,7 @@ ORDER BY "Employee"."EmployeeId";
assert.NilError(t, err)
assert.Equal(t, len(dest), 8)
testutils.AssertJSON(t, `
testutils.AssertJSON(t, dest[0:2], `
[
{
"EmployeeId": 1,
@ -197,7 +197,7 @@ ORDER BY "Employee"."EmployeeId";
}
}
]
`, dest[0:2])
`)
}

View file

@ -47,7 +47,7 @@ FROM test_sample.person;
err := query.Query(db, &result)
assert.NilError(t, err)
testutils.AssertJSON(t, `
testutils.AssertJSON(t, result, `
[
{
"PersonID": "b68dbff4-a87d-11e9-a7f2-98ded00c39c6",
@ -68,7 +68,7 @@ FROM test_sample.person;
"Mood": "ok"
}
]
`, result)
`)
}
func TestSelecSelfJoin1(t *testing.T) {

View file

@ -533,7 +533,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
assert.NilError(t, err)
assert.Equal(t, len(dest), 2)
testutils.AssertJSON(t, `
testutils.AssertJSON(t, dest, `
[
{
"CityID": 312,
@ -572,7 +572,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
]
}
]
`, dest)
`)
}
func TestJoinQuerySliceWithPtrs(t *testing.T) {
@ -1136,7 +1136,7 @@ func TestSelectStaff(t *testing.T) {
assert.NilError(t, err)
testutils.AssertJSON(t, `
testutils.AssertJSON(t, staffs, `
[
{
"StaffID": 1,
@ -1165,7 +1165,7 @@ func TestSelectStaff(t *testing.T) {
"Picture": null
}
]
`, staffs)
`)
}
func TestSelectTimeColumns(t *testing.T) {