From dfe27ad556c82f667ea993f9006be2d756fd682e Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Sun, 18 Feb 2024 21:26:30 -0500 Subject: [PATCH] database: use all tables to setup test suite (#7667) --- internal/database/access_tokens_test.go | 6 ++---- internal/database/actions_test.go | 6 ++---- internal/database/attachment.go | 2 +- internal/database/comment.go | 14 +++++++------- internal/database/database.go | 2 ++ internal/database/issue_label.go | 4 ++-- internal/database/lfs_test.go | 6 ++---- internal/database/login_sources_test.go | 6 ++---- internal/database/main_test.go | 10 +++++++--- internal/database/milestone.go | 12 ++++++------ internal/database/mirror.go | 6 +++--- internal/database/notices_test.go | 7 ++----- internal/database/org_team.go | 4 ++-- internal/database/orgs_test.go | 6 ++---- internal/database/perms_test.go | 7 ++----- internal/database/public_keys_test.go | 6 ++---- internal/database/release.go | 10 +++++----- internal/database/repos_test.go | 6 ++---- internal/database/ssh_key.go | 10 +++++----- internal/database/two_factors_test.go | 6 ++---- internal/database/users_test.go | 10 ++-------- internal/database/webhook.go | 12 ++++++------ 22 files changed, 68 insertions(+), 90 deletions(-) diff --git a/internal/database/access_tokens_test.go b/internal/database/access_tokens_test.go index d373d107f..72b68daf9 100644 --- a/internal/database/access_tokens_test.go +++ b/internal/database/access_tokens_test.go @@ -13,7 +13,6 @@ import ( "github.com/stretchr/testify/require" "gorm.io/gorm" - "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/errutil" ) @@ -99,9 +98,8 @@ func TestAccessTokens(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{new(AccessToken)} db := &accessTokens{ - DB: dbtest.NewDB(t, "accessTokens", tables...), + DB: newTestDB(t, "accessTokens"), } for _, tc := range []struct { @@ -116,7 +114,7 @@ func TestAccessTokens(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/actions_test.go b/internal/database/actions_test.go index ea419b234..81b87d637 100644 --- a/internal/database/actions_test.go +++ b/internal/database/actions_test.go @@ -16,7 +16,6 @@ import ( "gorm.io/gorm" "gogs.io/gogs/internal/conf" - "gogs.io/gogs/internal/dbtest" ) func TestIssueReferencePattern(t *testing.T) { @@ -100,9 +99,8 @@ func TestActions(t *testing.T) { ctx := context.Background() t.Parallel() - tables := []any{new(Action), new(User), new(Repository), new(EmailAddress), new(Watch)} db := &actions{ - DB: dbtest.NewDB(t, "actions", tables...), + DB: newTestDB(t, "actions"), } for _, tc := range []struct { @@ -123,7 +121,7 @@ func TestActions(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/attachment.go b/internal/database/attachment.go index d0da42474..e5dd77f52 100644 --- a/internal/database/attachment.go +++ b/internal/database/attachment.go @@ -28,7 +28,7 @@ type Attachment struct { ReleaseID int64 `xorm:"INDEX"` Name string - Created time.Time `xorm:"-" json:"-"` + Created time.Time `xorm:"-" json:"-" gorm:"-"` CreatedUnix int64 } diff --git a/internal/database/comment.go b/internal/database/comment.go index 7d6f2fbd4..ceb9ec106 100644 --- a/internal/database/comment.go +++ b/internal/database/comment.go @@ -53,26 +53,26 @@ type Comment struct { ID int64 Type CommentType PosterID int64 - Poster *User `xorm:"-" json:"-"` + Poster *User `xorm:"-" json:"-" gorm:"-"` IssueID int64 `xorm:"INDEX"` - Issue *Issue `xorm:"-" json:"-"` + Issue *Issue `xorm:"-" json:"-" gorm:"-"` CommitID int64 Line int64 Content string `xorm:"TEXT"` - RenderedContent string `xorm:"-" json:"-"` + RenderedContent string `xorm:"-" json:"-" gorm:"-"` - Created time.Time `xorm:"-" json:"-"` + Created time.Time `xorm:"-" json:"-" gorm:"-"` CreatedUnix int64 - Updated time.Time `xorm:"-" json:"-"` + Updated time.Time `xorm:"-" json:"-" gorm:"-"` UpdatedUnix int64 // Reference issue in commit message CommitSHA string `xorm:"VARCHAR(40)"` - Attachments []*Attachment `xorm:"-" json:"-"` + Attachments []*Attachment `xorm:"-" json:"-" gorm:"-"` // For view issue page. - ShowTag CommentTag `xorm:"-" json:"-"` + ShowTag CommentTag `xorm:"-" json:"-" gorm:"-"` } func (c *Comment) BeforeInsert() { diff --git a/internal/database/database.go b/internal/database/database.go index dd123c90d..a296b3346 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -40,6 +40,8 @@ func newLogWriter() (logger.Writer, error) { // Tables is the list of struct-to-table mappings. // // NOTE: Lines are sorted in alphabetical order, each letter in its own line. +// +// ⚠️ WARNING: This list is meant to be read-only. var Tables = []any{ new(Access), new(AccessToken), new(Action), new(EmailAddress), diff --git a/internal/database/issue_label.go b/internal/database/issue_label.go index 670a74965..5858ce9e2 100644 --- a/internal/database/issue_label.go +++ b/internal/database/issue_label.go @@ -61,8 +61,8 @@ type Label struct { Color string `xorm:"VARCHAR(7)"` NumIssues int NumClosedIssues int - NumOpenIssues int `xorm:"-" json:"-"` - IsChecked bool `xorm:"-" json:"-"` + NumOpenIssues int `xorm:"-" json:"-" gorm:"-"` + IsChecked bool `xorm:"-" json:"-" gorm:"-"` } func (label *Label) APIFormat() *api.Label { diff --git a/internal/database/lfs_test.go b/internal/database/lfs_test.go index 99a3ddd28..57966d116 100644 --- a/internal/database/lfs_test.go +++ b/internal/database/lfs_test.go @@ -12,7 +12,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/errutil" "gogs.io/gogs/internal/lfsutil" ) @@ -24,9 +23,8 @@ func TestLFS(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{new(LFSObject)} db := &lfs{ - DB: dbtest.NewDB(t, "lfs", tables...), + DB: newTestDB(t, "lfs"), } for _, tc := range []struct { @@ -39,7 +37,7 @@ func TestLFS(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/login_sources_test.go b/internal/database/login_sources_test.go index e8470dd48..f4299c975 100644 --- a/internal/database/login_sources_test.go +++ b/internal/database/login_sources_test.go @@ -19,7 +19,6 @@ import ( "gogs.io/gogs/internal/auth/ldap" "gogs.io/gogs/internal/auth/pam" "gogs.io/gogs/internal/auth/smtp" - "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/errutil" ) @@ -164,9 +163,8 @@ func TestLoginSources(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{new(LoginSource), new(User)} db := &loginSources{ - DB: dbtest.NewDB(t, "loginSources", tables...), + DB: newTestDB(t, "loginSources"), } for _, tc := range []struct { @@ -183,7 +181,7 @@ func TestLoginSources(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/main_test.go b/internal/database/main_test.go index 137871adc..e4938c987 100644 --- a/internal/database/main_test.go +++ b/internal/database/main_test.go @@ -16,6 +16,7 @@ import ( log "unknwon.dev/clog/v2" "gogs.io/gogs/internal/conf" + "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/testutil" ) @@ -50,13 +51,16 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -// clearTables removes all rows from given tables. -func clearTables(t *testing.T, db *gorm.DB, tables ...any) error { +func newTestDB(t *testing.T, suite string) *gorm.DB { + return dbtest.NewDB(t, suite, append(Tables, legacyTables...)...) +} + +func clearTables(t *testing.T, db *gorm.DB) error { if t.Failed() { return nil } - for _, t := range tables { + for _, t := range append(Tables, legacyTables...) { err := db.Where("TRUE").Delete(t).Error if err != nil { return err diff --git a/internal/database/milestone.go b/internal/database/milestone.go index a6e9d068a..d27b2665c 100644 --- a/internal/database/milestone.go +++ b/internal/database/milestone.go @@ -23,18 +23,18 @@ type Milestone struct { RepoID int64 `xorm:"INDEX"` Name string Content string `xorm:"TEXT"` - RenderedContent string `xorm:"-" json:"-"` + RenderedContent string `xorm:"-" json:"-" gorm:"-"` IsClosed bool NumIssues int NumClosedIssues int - NumOpenIssues int `xorm:"-" json:"-"` + NumOpenIssues int `xorm:"-" json:"-" gorm:"-"` Completeness int // Percentage(1-100). - IsOverDue bool `xorm:"-" json:"-"` + IsOverDue bool `xorm:"-" json:"-" gorm:"-"` - DeadlineString string `xorm:"-" json:"-"` - Deadline time.Time `xorm:"-" json:"-"` + DeadlineString string `xorm:"-" json:"-" gorm:"-"` + Deadline time.Time `xorm:"-" json:"-" gorm:"-"` DeadlineUnix int64 - ClosedDate time.Time `xorm:"-" json:"-"` + ClosedDate time.Time `xorm:"-" json:"-" gorm:"-"` ClosedDateUnix int64 } diff --git a/internal/database/mirror.go b/internal/database/mirror.go index 92e447342..8a4209576 100644 --- a/internal/database/mirror.go +++ b/internal/database/mirror.go @@ -30,14 +30,14 @@ var MirrorQueue = sync.NewUniqueQueue(1000) type Mirror struct { ID int64 RepoID int64 - Repo *Repository `xorm:"-" json:"-"` + Repo *Repository `xorm:"-" json:"-" gorm:"-"` Interval int // Hour. EnablePrune bool `xorm:"NOT NULL DEFAULT true"` // Last and next sync time of Git data from upstream - LastSync time.Time `xorm:"-" json:"-"` + LastSync time.Time `xorm:"-" json:"-" gorm:"-"` LastSyncUnix int64 `xorm:"updated_unix"` - NextSync time.Time `xorm:"-" json:"-"` + NextSync time.Time `xorm:"-" json:"-" gorm:"-"` NextSyncUnix int64 `xorm:"next_update_unix"` address string `xorm:"-"` diff --git a/internal/database/notices_test.go b/internal/database/notices_test.go index b2d74a093..3c7efcc90 100644 --- a/internal/database/notices_test.go +++ b/internal/database/notices_test.go @@ -12,8 +12,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gorm.io/gorm" - - "gogs.io/gogs/internal/dbtest" ) func TestNotice_BeforeCreate(t *testing.T) { @@ -67,9 +65,8 @@ func TestNotices(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{new(Notice)} db := ¬ices{ - DB: dbtest.NewDB(t, "notices", tables...), + DB: newTestDB(t, "notices"), } for _, tc := range []struct { @@ -84,7 +81,7 @@ func TestNotices(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/org_team.go b/internal/database/org_team.go index f51e75839..35fb007f2 100644 --- a/internal/database/org_team.go +++ b/internal/database/org_team.go @@ -25,8 +25,8 @@ type Team struct { Name string Description string Authorize AccessMode - Repos []*Repository `xorm:"-" json:"-"` - Members []*User `xorm:"-" json:"-"` + Repos []*Repository `xorm:"-" json:"-" gorm:"-"` + Members []*User `xorm:"-" json:"-" gorm:"-"` NumRepos int NumMembers int } diff --git a/internal/database/orgs_test.go b/internal/database/orgs_test.go index f3e00c8c1..188f5605c 100644 --- a/internal/database/orgs_test.go +++ b/internal/database/orgs_test.go @@ -11,7 +11,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/dbutil" ) @@ -22,9 +21,8 @@ func TestOrgs(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{new(User), new(EmailAddress), new(OrgUser)} db := &orgs{ - DB: dbtest.NewDB(t, "orgs", tables...), + DB: newTestDB(t, "orgs"), } for _, tc := range []struct { @@ -37,7 +35,7 @@ func TestOrgs(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/perms_test.go b/internal/database/perms_test.go index 6bd55eb70..4a4951330 100644 --- a/internal/database/perms_test.go +++ b/internal/database/perms_test.go @@ -10,8 +10,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - "gogs.io/gogs/internal/dbtest" ) func TestPerms(t *testing.T) { @@ -21,9 +19,8 @@ func TestPerms(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{new(Access)} db := &perms{ - DB: dbtest.NewDB(t, "perms", tables...), + DB: newTestDB(t, "perms"), } for _, tc := range []struct { @@ -36,7 +33,7 @@ func TestPerms(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/public_keys_test.go b/internal/database/public_keys_test.go index 925aa3c6f..bae252a9d 100644 --- a/internal/database/public_keys_test.go +++ b/internal/database/public_keys_test.go @@ -15,7 +15,6 @@ import ( "github.com/stretchr/testify/require" "gogs.io/gogs/internal/conf" - "gogs.io/gogs/internal/dbtest" ) func TestPublicKeys(t *testing.T) { @@ -25,9 +24,8 @@ func TestPublicKeys(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{new(PublicKey)} db := &publicKeys{ - DB: dbtest.NewDB(t, "publicKeys", tables...), + DB: newTestDB(t, "publicKeys"), } for _, tc := range []struct { @@ -38,7 +36,7 @@ func TestPublicKeys(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/release.go b/internal/database/release.go index bce7eb9ee..7dcfcf53c 100644 --- a/internal/database/release.go +++ b/internal/database/release.go @@ -24,24 +24,24 @@ import ( type Release struct { ID int64 RepoID int64 - Repo *Repository `xorm:"-" json:"-"` + Repo *Repository `xorm:"-" json:"-" gorm:"-"` PublisherID int64 - Publisher *User `xorm:"-" json:"-"` + Publisher *User `xorm:"-" json:"-" gorm:"-"` TagName string LowerTagName string Target string Title string Sha1 string `xorm:"VARCHAR(40)"` NumCommits int64 - NumCommitsBehind int64 `xorm:"-" json:"-"` + NumCommitsBehind int64 `xorm:"-" json:"-" gorm:"-"` Note string `xorm:"TEXT"` IsDraft bool `xorm:"NOT NULL DEFAULT false"` IsPrerelease bool - Created time.Time `xorm:"-" json:"-"` + Created time.Time `xorm:"-" json:"-" gorm:"-"` CreatedUnix int64 - Attachments []*Attachment `xorm:"-" json:"-"` + Attachments []*Attachment `xorm:"-" json:"-" gorm:"-"` } func (r *Release) BeforeInsert() { diff --git a/internal/database/repos_test.go b/internal/database/repos_test.go index e697db1cd..27b62fe3d 100644 --- a/internal/database/repos_test.go +++ b/internal/database/repos_test.go @@ -13,7 +13,6 @@ import ( "github.com/stretchr/testify/require" "gorm.io/gorm" - "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/errutil" ) @@ -86,9 +85,8 @@ func TestRepos(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{new(Repository), new(Access), new(Watch), new(User), new(EmailAddress), new(Star)} db := &repos{ - DB: dbtest.NewDB(t, "repos", tables...), + DB: newTestDB(t, "repos"), } for _, tc := range []struct { @@ -108,7 +106,7 @@ func TestRepos(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/ssh_key.go b/internal/database/ssh_key.go index b45917836..4b3902ab0 100644 --- a/internal/database/ssh_key.go +++ b/internal/database/ssh_key.go @@ -569,14 +569,14 @@ type DeployKey struct { RepoID int64 `xorm:"UNIQUE(s) INDEX"` Name string Fingerprint string - Content string `xorm:"-" json:"-"` + Content string `xorm:"-" json:"-" gorm:"-"` - Created time.Time `xorm:"-" json:"-"` + Created time.Time `xorm:"-" json:"-" gorm:"-"` CreatedUnix int64 - Updated time.Time `xorm:"-" json:"-"` // Note: Updated must below Created for AfterSet. + Updated time.Time `xorm:"-" json:"-" gorm:"-"` // Note: Updated must below Created for AfterSet. UpdatedUnix int64 - HasRecentActivity bool `xorm:"-" json:"-"` - HasUsed bool `xorm:"-" json:"-"` + HasRecentActivity bool `xorm:"-" json:"-" gorm:"-"` + HasUsed bool `xorm:"-" json:"-" gorm:"-"` } func (k *DeployKey) BeforeInsert() { diff --git a/internal/database/two_factors_test.go b/internal/database/two_factors_test.go index e03e06f06..f4f30e022 100644 --- a/internal/database/two_factors_test.go +++ b/internal/database/two_factors_test.go @@ -13,7 +13,6 @@ import ( "github.com/stretchr/testify/require" "gorm.io/gorm" - "gogs.io/gogs/internal/dbtest" "gogs.io/gogs/internal/errutil" ) @@ -68,9 +67,8 @@ func TestTwoFactors(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{new(TwoFactor), new(TwoFactorRecoveryCode)} db := &twoFactors{ - DB: dbtest.NewDB(t, "twoFactors", tables...), + DB: newTestDB(t, "twoFactors"), } for _, tc := range []struct { @@ -83,7 +81,7 @@ func TestTwoFactors(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/users_test.go b/internal/database/users_test.go index 1547c271c..6b2c3ac13 100644 --- a/internal/database/users_test.go +++ b/internal/database/users_test.go @@ -19,7 +19,6 @@ import ( "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" @@ -85,13 +84,8 @@ func TestUsers(t *testing.T) { t.Parallel() ctx := context.Background() - tables := []any{ - new(User), new(EmailAddress), new(Repository), new(Follow), new(PullRequest), new(PublicKey), new(OrgUser), - new(Watch), new(Star), new(Issue), new(AccessToken), new(Collaboration), new(Action), new(IssueUser), - new(Access), - } db := &users{ - DB: dbtest.NewDB(t, "users", tables...), + DB: newTestDB(t, "users"), } for _, tc := range []struct { @@ -129,7 +123,7 @@ func TestUsers(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) + err := clearTables(t, db.DB) require.NoError(t, err) }) tc.test(t, ctx, db) diff --git a/internal/database/webhook.go b/internal/database/webhook.go index 25049154f..8927f671c 100644 --- a/internal/database/webhook.go +++ b/internal/database/webhook.go @@ -109,9 +109,9 @@ type Webhook struct { Meta string `xorm:"TEXT"` // store hook-specific attributes LastStatus HookStatus // Last delivery status - Created time.Time `xorm:"-" json:"-"` + Created time.Time `xorm:"-" json:"-" gorm:"-"` CreatedUnix int64 - Updated time.Time `xorm:"-" json:"-"` + Updated time.Time `xorm:"-" json:"-" gorm:"-"` UpdatedUnix int64 } @@ -440,21 +440,21 @@ type HookTask struct { Type HookTaskType URL string `xorm:"TEXT"` Signature string `xorm:"TEXT"` - api.Payloader `xorm:"-" json:"-"` + api.Payloader `xorm:"-" json:"-" gorm:"-"` PayloadContent string `xorm:"TEXT"` ContentType HookContentType EventType HookEventType IsSSL bool IsDelivered bool Delivered int64 - DeliveredString string `xorm:"-" json:"-"` + DeliveredString string `xorm:"-" json:"-" gorm:"-"` // History info. IsSucceed bool RequestContent string `xorm:"TEXT"` - RequestInfo *HookRequest `xorm:"-" json:"-"` + RequestInfo *HookRequest `xorm:"-" json:"-" gorm:"-"` ResponseContent string `xorm:"TEXT"` - ResponseInfo *HookResponse `xorm:"-" json:"-"` + ResponseInfo *HookResponse `xorm:"-" json:"-" gorm:"-"` } func (t *HookTask) BeforeUpdate() {