database: use all tables to setup test suite (#7667)

pull/7669/head
Joe Chen 2024-02-18 21:26:30 -05:00 committed by GitHub
parent 3616bc03c9
commit dfe27ad556
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
22 changed files with 68 additions and 90 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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
}

View File

@ -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() {

View File

@ -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),

View File

@ -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 {

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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
}

View File

@ -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:"-"`

View File

@ -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 := &notices{
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)

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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() {

View File

@ -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)

View File

@ -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() {

View File

@ -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)

View File

@ -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)

View File

@ -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() {