db: code tidy up for `AccessTokens` (#7006)

pull/7007/head
Joe Chen 2022-06-04 13:35:42 +08:00 committed by GitHub
parent 38aff73251
commit 4455cc1244
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View File

@ -54,7 +54,7 @@ type AccessToken struct {
HasUsed bool `xorm:"-" gorm:"-" json:"-"`
}
// NOTE: This is a GORM create hook.
// BeforeCreate implements the GORM create hook.
func (t *AccessToken) BeforeCreate(tx *gorm.DB) error {
if t.CreatedUnix == 0 {
t.CreatedUnix = tx.NowFunc().Unix()
@ -62,13 +62,13 @@ func (t *AccessToken) BeforeCreate(tx *gorm.DB) error {
return nil
}
// NOTE: This is a GORM update hook.
// BeforeUpdate implements the GORM update hook.
func (t *AccessToken) BeforeUpdate(tx *gorm.DB) error {
t.UpdatedUnix = tx.NowFunc().Unix()
return nil
}
// NOTE: This is a GORM query hook.
// AfterFind implements the GORM query hook.
func (t *AccessToken) AfterFind(tx *gorm.DB) error {
t.Created = time.Unix(t.CreatedUnix, 0).Local()
t.Updated = time.Unix(t.UpdatedUnix, 0).Local()

View File

@ -39,7 +39,7 @@ func TestAccessToken_BeforeCreate(t *testing.T) {
})
}
func Test_accessTokens(t *testing.T) {
func TestAccessTokens(t *testing.T) {
if testing.Short() {
t.Skip()
}
@ -55,11 +55,11 @@ func Test_accessTokens(t *testing.T) {
name string
test func(*testing.T, *accessTokens)
}{
{"Create", test_accessTokens_Create},
{"DeleteByID", test_accessTokens_DeleteByID},
{"GetBySHA", test_accessTokens_GetBySHA},
{"List", test_accessTokens_List},
{"Save", test_accessTokens_Save},
{"Create", accessTokensCreate},
{"DeleteByID", accessTokensDeleteByID},
{"GetBySHA", accessTokensGetBySHA},
{"List", accessTokensList},
{"Save", accessTokensSave},
} {
t.Run(tc.name, func(t *testing.T) {
t.Cleanup(func() {
@ -70,10 +70,13 @@ func Test_accessTokens(t *testing.T) {
})
tc.test(t, db)
})
if t.Failed() {
break
}
}
}
func test_accessTokens_Create(t *testing.T, db *accessTokens) {
func accessTokensCreate(t *testing.T, db *accessTokens) {
// Create first access token with name "Test"
token, err := db.Create(1, "Test")
if err != nil {
@ -97,7 +100,7 @@ func test_accessTokens_Create(t *testing.T, db *accessTokens) {
assert.Equal(t, expErr, err)
}
func test_accessTokens_DeleteByID(t *testing.T, db *accessTokens) {
func accessTokensDeleteByID(t *testing.T, db *accessTokens) {
// Create an access token with name "Test"
token, err := db.Create(1, "Test")
if err != nil {
@ -132,7 +135,7 @@ func test_accessTokens_DeleteByID(t *testing.T, db *accessTokens) {
assert.Equal(t, expErr, err)
}
func test_accessTokens_GetBySHA(t *testing.T, db *accessTokens) {
func accessTokensGetBySHA(t *testing.T, db *accessTokens) {
// Create an access token with name "Test"
token, err := db.Create(1, "Test")
if err != nil {
@ -151,7 +154,7 @@ func test_accessTokens_GetBySHA(t *testing.T, db *accessTokens) {
assert.Equal(t, expErr, err)
}
func test_accessTokens_List(t *testing.T, db *accessTokens) {
func accessTokensList(t *testing.T, db *accessTokens) {
// Create two access tokens for user 1
_, err := db.Create(1, "user1_1")
if err != nil {
@ -182,7 +185,7 @@ func test_accessTokens_List(t *testing.T, db *accessTokens) {
assert.Equal(t, "user1_2", tokens[1].Name)
}
func test_accessTokens_Save(t *testing.T, db *accessTokens) {
func accessTokensSave(t *testing.T, db *accessTokens) {
// Create an access token with name "Test"
token, err := db.Create(1, "Test")
if err != nil {