diff options
author | Joe Chen <jc@unknwon.io> | 2022-06-04 13:35:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-04 13:35:42 +0800 |
commit | 4455cc1244f83a75e1ed665f4f49856744fe5482 (patch) | |
tree | 036c87f48888ad051cf48200308a5d3ff51824f9 /internal/db | |
parent | 38aff73251cc46ced96dd608dab6190415032a82 (diff) |
db: code tidy up for `AccessTokens` (#7006)
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/access_tokens.go | 6 | ||||
-rw-r--r-- | internal/db/access_tokens_test.go | 25 |
2 files changed, 17 insertions, 14 deletions
diff --git a/internal/db/access_tokens.go b/internal/db/access_tokens.go index ab62c52d..a419a267 100644 --- a/internal/db/access_tokens.go +++ b/internal/db/access_tokens.go @@ -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() diff --git a/internal/db/access_tokens_test.go b/internal/db/access_tokens_test.go index ef629320..05f3173e 100644 --- a/internal/db/access_tokens_test.go +++ b/internal/db/access_tokens_test.go @@ -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 { |