aboutsummaryrefslogtreecommitdiff
path: root/internal/db/users_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db/users_test.go')
-rw-r--r--internal/db/users_test.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/internal/db/users_test.go b/internal/db/users_test.go
index efd5681c..5645ff7b 100644
--- a/internal/db/users_test.go
+++ b/internal/db/users_test.go
@@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "gorm.io/gorm"
"gogs.io/gogs/internal/auth"
"gogs.io/gogs/internal/dbtest"
@@ -22,6 +23,54 @@ import (
"gogs.io/gogs/public"
)
+func TestUser_BeforeCreate(t *testing.T) {
+ now := time.Now()
+ db := &gorm.DB{
+ Config: &gorm.Config{
+ SkipDefaultTransaction: true,
+ NowFunc: func() time.Time {
+ return now
+ },
+ },
+ }
+
+ t.Run("CreatedUnix has been set", func(t *testing.T) {
+ user := &User{
+ CreatedUnix: 1,
+ }
+ _ = user.BeforeCreate(db)
+ assert.Equal(t, int64(1), user.CreatedUnix)
+ assert.Equal(t, int64(0), user.UpdatedUnix)
+ })
+
+ t.Run("CreatedUnix has not been set", func(t *testing.T) {
+ user := &User{}
+ _ = user.BeforeCreate(db)
+ assert.Equal(t, db.NowFunc().Unix(), user.CreatedUnix)
+ assert.Equal(t, db.NowFunc().Unix(), user.UpdatedUnix)
+ })
+}
+
+func TestUser_AfterFind(t *testing.T) {
+ now := time.Now()
+ db := &gorm.DB{
+ Config: &gorm.Config{
+ SkipDefaultTransaction: true,
+ NowFunc: func() time.Time {
+ return now
+ },
+ },
+ }
+
+ user := &User{
+ CreatedUnix: now.Unix(),
+ UpdatedUnix: now.Unix(),
+ }
+ _ = user.AfterFind(db)
+ assert.Equal(t, user.CreatedUnix, user.Created.Unix())
+ assert.Equal(t, user.UpdatedUnix, user.Updated.Unix())
+}
+
func TestUsers(t *testing.T) {
if testing.Short() {
t.Skip()