// Copyright 2020 The Gogs Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package db import ( "context" "fmt" "os" "path/filepath" "strings" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gorm.io/gorm" "gogs.io/gogs/internal/auth" "gogs.io/gogs/internal/conf" "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/dbutil" "gogs.io/gogs/internal/errutil" "gogs.io/gogs/internal/osutil" "gogs.io/gogs/internal/repoutil" "gogs.io/gogs/internal/userutil" "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{ FullName: "user1