Skip to content

Commit

Permalink
fix EligibleAt null / NewNullTime (#471)
Browse files Browse the repository at this point in the history
* fix EligibleAt null / NewNullTime

* test
  • Loading branch information
metachris authored Apr 11, 2024
1 parent e092c7c commit ce0265f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func NewNullString(s string) sql.NullString {
}
}

// NewNullTime returns a sql.NullTime with the given time.Time. If the time is
// the zero value, the NullTime is invalid.
func NewNullTime(t time.Time) sql.NullTime {
return sql.NullTime{
Time: t,
Valid: true,
Valid: t != time.Time{},
}
}

Expand Down
19 changes: 19 additions & 0 deletions database/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package database

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestNewNullTime(t *testing.T) {
var t1 time.Time
nt1 := NewNullTime(t1)
require.False(t, nt1.Valid)

t1 = time.Now()
nt1 = NewNullTime(t1)
require.True(t, nt1.Valid)
require.Equal(t, t1, nt1.Time)
}

0 comments on commit ce0265f

Please sign in to comment.