github.com/blend/go-sdk@v1.20220411.3/db/driver_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package db 9 10 import ( 11 "database/sql" 12 "fmt" 13 "testing" 14 "time" 15 16 "github.com/blend/go-sdk/assert" 17 "github.com/blend/go-sdk/uuid" 18 ) 19 20 // PGXTimestampTest is a test object. 21 type PGXTimestampTest struct { 22 ID uuid.UUID `db:"id,pk"` 23 24 IntValue int `db:"int_value"` 25 IntPtrValue *int `db:"int_ptr_value"` 26 27 JSONValue PGXjson `db:"json_value,json"` 28 29 Local time.Time 30 UTC time.Time 31 32 LocalWithTimezone time.Time `db:"local_with_timezone"` 33 LocalWithoutTimezone time.Time `db:"local_without_timezone"` 34 35 UTCWithTimezone time.Time `db:"utc_with_timezone"` 36 UTCWithoutTimezone time.Time `db:"utc_without_timezone"` 37 } 38 39 // TableName returns the mapped table name. 40 func (pgt PGXTimestampTest) TableName() string { return "pgx_timestamp_test" } 41 42 // PGXjson is a json object test. 43 type PGXjson struct { 44 Foo string 45 Bar string 46 } 47 48 func createPGXTimestampTestTable(conn *Connection, tx *sql.Tx) error { 49 _, err := conn.Invoke(OptTx(tx)).Exec(` 50 CREATE TABLE pgx_timestamp_test ( 51 id uuid not null primary key, 52 53 int_value int not null, 54 int_ptr_value int, 55 json_value jsonb, 56 57 local timestamp not null, 58 utc timestamp not null, 59 local_with_timezone timestamp with time zone not null, 60 local_without_timezone timestamp without time zone not null, 61 utc_with_timezone timestamp with time zone not null, 62 utc_without_timezone timestamp without time zone not null 63 ) 64 `) 65 return err 66 } 67 68 func dropPGXTimestampTestTable(conn *Connection, tx *sql.Tx) error { 69 _, err := conn.Invoke(OptTx(tx)).Exec(`DROP TABLE pgx_timestamp_test`) 70 return err 71 } 72 73 func Test_PGX_Timestamp(t *testing.T) { 74 assert := assert.New(t) 75 tx, err := defaultDB().Begin() 76 assert.Nil(err) 77 78 // create the test table 79 assert.Nil(createPGXTimestampTestTable(defaultDB(), tx)) 80 defer func() { _ = dropPGXTimestampTestTable(defaultDB(), tx) }() 81 82 now := time.Now() 83 intValue := 1234 84 85 testObj := PGXTimestampTest{ 86 ID: uuid.V4(), 87 IntValue: intValue, 88 IntPtrValue: &intValue, 89 JSONValue: PGXjson{ 90 Foo: "foo", 91 Bar: "bar", 92 }, 93 Local: now, 94 UTC: now.UTC(), 95 LocalWithTimezone: now, 96 LocalWithoutTimezone: now, 97 UTCWithTimezone: now.UTC(), 98 UTCWithoutTimezone: now.UTC(), 99 } 100 assert.Nil(defaultDB().Invoke(OptTx(tx)).Create(&testObj)) 101 102 var verify PGXTimestampTest 103 found, err := defaultDB().Invoke(OptTx(tx)).Get(&verify, testObj.ID) 104 assert.Nil(err) 105 assert.True(found) 106 assert.True(verify.ID.Equal(testObj.ID), "ID should be equal") 107 108 assert.Equal(intValue, verify.IntValue) 109 assert.NotNil(verify.IntPtrValue) 110 assert.Equal(intValue, *verify.IntPtrValue) 111 112 assert.Equal("foo", verify.JSONValue.Foo) 113 assert.Equal("bar", verify.JSONValue.Bar) 114 115 assertTimeEqual(assert, now, testObj.Local) 116 assertTimeEqual(assert, now.UTC(), testObj.Local.UTC()) 117 118 assertTimeEqual(assert, now, verify.LocalWithTimezone) 119 assertTimeEqual(assert, testObj.LocalWithTimezone, verify.LocalWithTimezone) 120 // assertTimeNotEqual(assert, testObj.LocalWithoutTimezone, verify.LocalWithoutTimezone) 121 assertTimeEqual(assert, testObj.UTCWithTimezone, verify.UTCWithTimezone) 122 assertTimeEqual(assert, testObj.UTCWithoutTimezone, verify.UTCWithoutTimezone) 123 } 124 125 func assertTimeEqual(a *assert.Assertions, expected, actual time.Time) { 126 a.InTimeDelta(expected, actual, time.Second, fmt.Sprintf("actual delta: %v", expected.Sub(actual))) 127 }