[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

@ -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) {