Skip to content

Commit

Permalink
Test pq's NullTime implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
aodin committed Jun 13, 2016
1 parent d9c24c7 commit 52aece2
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/lib/pq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -41,6 +42,11 @@ var things = sol.Table("things",
sol.Column("created_at", Timestamp().Default(Now)),
)

var thingsWithoutNow = sol.Table("things",
sol.Column("name", types.Varchar()),
sol.Column("created_at", Timestamp()),
)

type thing struct {
Name string
CreatedAt time.Time `db:",omitempty"`
Expand Down Expand Up @@ -82,6 +88,37 @@ func TestPostGres(t *testing.T) {
sol.IntegrationTest(t, conn, false)
}

func TestPostGres_NullTime(t *testing.T) {
conn := getConn(t) // TODO close

tx, err := conn.Begin()
require.Nil(t, err, "Creating a new transaction should not error")
defer tx.Rollback()

// TODO temp tables
require.Nil(t,
tx.Query(thingsWithoutNow.Create().Temporary().IfNotExists()),
`Create table "%s" should not error`, thingsWithoutNow.Name(),
)

type nullthing struct {
Name string
CreatedAt pq.NullTime `db:"created_at"`
}

nonzero := nullthing{
Name: "a",
CreatedAt: pq.NullTime{Valid: true, Time: time.Now()},
}
tx.Query(thingsWithoutNow.Insert().Values(nonzero))

var things []nullthing
tx.Query(thingsWithoutNow.Select(), &things)
require.Equal(t, 1, len(things))
assert.True(t, things[0].CreatedAt.Valid)
assert.False(t, things[0].CreatedAt.Time.IsZero())
}

func TestPostGres_Create(t *testing.T) {
expect := sol.NewTester(t, &PostGres{})

Expand Down

0 comments on commit 52aece2

Please sign in to comment.