Skip to content

Commit

Permalink
feat: allow customizing the time used for CreatedAt/UpdatedAt
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hobson authored and aeneasr committed Apr 21, 2022
1 parent e6ba76b commit 63986aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions executors_test.go
Expand Up @@ -1964,3 +1964,28 @@ func Test_Delete(t *testing.T) {
r.Equal(count, ctx)
})
}

func Test_Create_Timestamps_With_NowFunc(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
}
transaction(func(tx *Connection) {
r := require.New(t)

originalNowFunc := nowFunc
// ensure the original function is restored
defer func() {
nowFunc = originalNowFunc
}()

fakeNow, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
SetNowFunc(func() time.Time { return fakeNow })

friend:= Friend{FirstName: "Yester", LastName: "Day"}
err := tx.Create(&friend)
r.NoError(err)

r.Equal(fakeNow, friend.CreatedAt)
r.Equal(fakeNow, friend.UpdatedAt)
})
}
5 changes: 5 additions & 0 deletions model.go
Expand Up @@ -15,6 +15,11 @@ import (

var nowFunc = time.Now

// SetNowFunc allows an override of time.Now for customizing CreatedAt/UpdatedAt
func SetNowFunc(f func() time.Time) {
nowFunc = f
}

// Value is the contents of a `Model`.
type Value interface{}

Expand Down

0 comments on commit 63986aa

Please sign in to comment.