49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package store
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
func TestOrderCreatedTimeRespectsDatabaseSessionZone(t *testing.T) {
|
|
dsn := os.Getenv("OLONS_TEST_MYSQL_DSN")
|
|
if dsn == "" {
|
|
t.Skip("set OLONS_TEST_MYSQL_DSN to run the time integration test")
|
|
}
|
|
db, err := sqlx.Connect("mysql", dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db.SetMaxOpenConns(1)
|
|
db.SetMaxIdleConns(1)
|
|
t.Cleanup(func() { _ = db.Close() })
|
|
if _, err := db.Exec(`CREATE TEMPORARY TABLE order_time_fixture (created_at DATETIME(3))`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, test := range []struct{ zone, wallTime string }{
|
|
{"+00:00", "2026-10-22 13:06:00"},
|
|
{"+08:00", "2026-10-22 21:06:00"},
|
|
} {
|
|
t.Run(test.zone, func(t *testing.T) {
|
|
if _, err := db.Exec(`SET SESSION time_zone=?`, test.zone); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := db.Exec(`DELETE FROM order_time_fixture`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := db.Exec(`INSERT INTO order_time_fixture (created_at) VALUES (?)`, test.wallTime); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var actual string
|
|
if err := db.Get(&actual, `SELECT `+databaseWallTimeUTC("created_at")+` FROM order_time_fixture`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if actual != "2026-10-22T13:06:00Z" {
|
|
t.Fatalf("normalized timestamp = %q, want 2026-10-22T13:06:00Z", actual)
|
|
}
|
|
})
|
|
}
|
|
}
|