storj/private/dbutil/utccheck/db_test.go
Egon Elbre 080ba47a06 all: fix dots
Change-Id: I6a419c62700c568254ff67ae5b73efed2fc98aa2
2020-07-16 14:58:28 +00:00

96 lines
2.5 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package utccheck_test
import (
"context"
"database/sql"
"database/sql/driver"
"testing"
"time"
"github.com/stretchr/testify/require"
"storj.io/storj/private/dbutil/utccheck"
)
func TestUTCDB(t *testing.T) {
notUTC := time.FixedZone("not utc", -1)
db := sql.OpenDB(utccheck.WrapConnector(emptyConnector{}))
{ // time.Time not in UTC
_, err := db.Exec("", time.Now().In(notUTC))
require.Error(t, err)
}
{ // *time.Time not in UTC
now := time.Now().In(notUTC)
_, err := db.Exec("", &now)
require.Error(t, err)
}
{ // time.Time in UTC
_, err := db.Exec("", time.Now().UTC())
require.NoError(t, err)
}
{ // *time.Time in UTC
now := time.Now().UTC()
_, err := db.Exec("", &now)
require.NoError(t, err)
}
{ // nil *time.Time
_, err := db.Exec("", (*time.Time)(nil))
require.NoError(t, err)
}
}
//
// empty driver
//
type emptyConnector struct{}
func (emptyConnector) Connect(context.Context) (driver.Conn, error) { return emptyConn{}, nil }
func (emptyConnector) Driver() driver.Driver { return nil }
type emptyConn struct{}
func (emptyConn) Close() error { return nil }
func (emptyConn) Prepare(query string) (driver.Stmt, error) { return emptyStmt{}, nil }
func (emptyConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
return emptyStmt{}, nil
}
func (emptyConn) Begin() (driver.Tx, error) { return emptyTx{}, nil }
func (emptyConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return emptyTx{}, nil
}
func (emptyConn) Query(query string, args []driver.Value) (driver.Rows, error) { return nil, nil }
func (emptyConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
return nil, nil
}
func (emptyConn) Exec(query string, args []driver.Value) (driver.Result, error) { return nil, nil }
func (emptyConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
return nil, nil
}
type emptyTx struct{}
func (emptyTx) Commit() error { return nil }
func (emptyTx) Rollback() error { return nil }
type emptyStmt struct{}
func (emptyStmt) Close() error { return nil }
func (emptyStmt) Exec(args []driver.Value) (driver.Result, error) { return nil, nil }
func (emptyStmt) Query(args []driver.Value) (driver.Rows, error) { return nil, nil }
// must be 1 so that we can pass 1 argument.
func (emptyStmt) NumInput() int { return 1 }