diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/org_team_test.go | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip |
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db
* Fix lint
* Fix lint
* Fix test
* Fix lint
* Fix lint
* revert unnecessary change
* Fix test
* Fix wrong replace string
* Use *Context
* Correct committer spelling and fix wrong replaced words
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/org_team_test.go')
-rw-r--r-- | models/org_team_test.go | 159 |
1 files changed, 80 insertions, 79 deletions
diff --git a/models/org_team_test.go b/models/org_team_test.go index 9dc26fd814..f1c8f88879 100644 --- a/models/org_team_test.go +++ b/models/org_team_test.go @@ -8,42 +8,43 @@ import ( "strings" "testing" + "code.gitea.io/gitea/models/db" "github.com/stretchr/testify/assert" ) func TestTeam_IsOwnerTeam(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) assert.True(t, team.IsOwnerTeam()) - team = AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team = db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.False(t, team.IsOwnerTeam()) } func TestTeam_IsMember(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) assert.True(t, team.IsMember(2)) assert.False(t, team.IsMember(4)) - assert.False(t, team.IsMember(NonexistentID)) + assert.False(t, team.IsMember(db.NonexistentID)) - team = AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team = db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.True(t, team.IsMember(2)) assert.True(t, team.IsMember(4)) - assert.False(t, team.IsMember(NonexistentID)) + assert.False(t, team.IsMember(db.NonexistentID)) } func TestTeam_GetRepositories(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(teamID int64) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.GetRepositories(&SearchTeamOptions{})) assert.Len(t, team.Repos, team.NumRepos) for _, repo := range team.Repos { - AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repo.ID}) + db.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repo.ID}) } } test(1) @@ -51,14 +52,14 @@ func TestTeam_GetRepositories(t *testing.T) { } func TestTeam_GetMembers(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(teamID int64) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.GetMembers(&SearchMembersOptions{})) assert.Len(t, team.Members, team.NumMembers) for _, member := range team.Members { - AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) + db.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) } } test(1) @@ -66,12 +67,12 @@ func TestTeam_GetMembers(t *testing.T) { } func TestTeam_AddMember(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(teamID, userID int64) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.AddMember(userID)) - AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) + db.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) } test(1, 2) @@ -80,71 +81,71 @@ func TestTeam_AddMember(t *testing.T) { } func TestTeam_RemoveMember(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) testSuccess := func(teamID, userID int64) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.RemoveMember(userID)) - AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) + db.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) CheckConsistencyFor(t, &Team{ID: teamID}) } testSuccess(1, 4) testSuccess(2, 2) testSuccess(3, 2) - testSuccess(3, NonexistentID) + testSuccess(3, db.NonexistentID) - team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) err := team.RemoveMember(2) assert.True(t, IsErrLastOrgOwner(err)) } func TestTeam_HasRepository(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(teamID, repoID int64, expected bool) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.Equal(t, expected, team.HasRepository(repoID)) } test(1, 1, false) test(1, 3, true) test(1, 5, true) - test(1, NonexistentID, false) + test(1, db.NonexistentID, false) test(2, 3, true) test(2, 5, false) } func TestTeam_AddRepository(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) testSuccess := func(teamID, repoID int64) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) - repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) assert.NoError(t, team.AddRepository(repo)) - AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) + db.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) } testSuccess(2, 3) testSuccess(2, 5) - team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) - repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.Error(t, team.AddRepository(repo)) CheckConsistencyFor(t, &Team{ID: 1}, &Repository{ID: 1}) } func TestTeam_RemoveRepository(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) testSuccess := func(teamID, repoID int64) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.RemoveRepository(repoID)) - AssertNotExistsBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) + db.AssertNotExistsBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) } testSuccess(2, 3) testSuccess(2, 5) - testSuccess(1, NonexistentID) + testSuccess(1, db.NonexistentID) } func TestIsUsableTeamName(t *testing.T) { @@ -153,17 +154,17 @@ func TestIsUsableTeamName(t *testing.T) { } func TestNewTeam(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) const teamName = "newTeamName" team := &Team{Name: teamName, OrgID: 3} assert.NoError(t, NewTeam(team)) - AssertExistsAndLoadBean(t, &Team{Name: teamName}) + db.AssertExistsAndLoadBean(t, &Team{Name: teamName}) CheckConsistencyFor(t, &Team{}, &User{ID: team.OrgID}) } func TestGetTeam(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) testSuccess := func(orgID int64, name string) { team, err := GetTeam(orgID, name) @@ -176,12 +177,12 @@ func TestGetTeam(t *testing.T) { _, err := GetTeam(3, "nonexistent") assert.Error(t, err) - _, err = GetTeam(NonexistentID, "Owners") + _, err = GetTeam(db.NonexistentID, "Owners") assert.Error(t, err) } func TestGetTeamByID(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) testSuccess := func(teamID int64) { team, err := GetTeamByID(teamID) @@ -193,25 +194,25 @@ func TestGetTeamByID(t *testing.T) { testSuccess(3) testSuccess(4) - _, err := GetTeamByID(NonexistentID) + _, err := GetTeamByID(db.NonexistentID) assert.Error(t, err) } func TestUpdateTeam(t *testing.T) { // successful update - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - team := AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) team.LowerName = "newname" team.Name = "newName" team.Description = strings.Repeat("A long description!", 100) team.Authorize = AccessModeAdmin assert.NoError(t, UpdateTeam(team, true, false)) - team = AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team) + team = db.AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team) assert.True(t, strings.HasPrefix(team.Description, "A long description!")) - access := AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: 3}).(*Access) + access := db.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: 3}).(*Access) assert.EqualValues(t, AccessModeAdmin, access.Mode) CheckConsistencyFor(t, &Team{ID: team.ID}) @@ -219,9 +220,9 @@ func TestUpdateTeam(t *testing.T) { func TestUpdateTeam2(t *testing.T) { // update to already-existing team - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - team := AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) team.LowerName = "owners" team.Name = "Owners" team.Description = strings.Repeat("A long description!", 100) @@ -232,24 +233,24 @@ func TestUpdateTeam2(t *testing.T) { } func TestDeleteTeam(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - team := AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.NoError(t, DeleteTeam(team)) - AssertNotExistsBean(t, &Team{ID: team.ID}) - AssertNotExistsBean(t, &TeamRepo{TeamID: team.ID}) - AssertNotExistsBean(t, &TeamUser{TeamID: team.ID}) + db.AssertNotExistsBean(t, &Team{ID: team.ID}) + db.AssertNotExistsBean(t, &TeamRepo{TeamID: team.ID}) + db.AssertNotExistsBean(t, &TeamUser{TeamID: team.ID}) // check that team members don't have "leftover" access to repos - user := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) - repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + user := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) accessMode, err := AccessLevel(user, repo) assert.NoError(t, err) assert.True(t, accessMode < AccessModeWrite) } func TestIsTeamMember(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(orgID, teamID, userID int64, expected bool) { isMember, err := IsTeamMember(orgID, teamID, userID) assert.NoError(t, err) @@ -258,25 +259,25 @@ func TestIsTeamMember(t *testing.T) { test(3, 1, 2, true) test(3, 1, 4, false) - test(3, 1, NonexistentID, false) + test(3, 1, db.NonexistentID, false) test(3, 2, 2, true) test(3, 2, 4, true) - test(3, NonexistentID, NonexistentID, false) - test(NonexistentID, NonexistentID, NonexistentID, false) + test(3, db.NonexistentID, db.NonexistentID, false) + test(db.NonexistentID, db.NonexistentID, db.NonexistentID, false) } func TestGetTeamMembers(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(teamID int64) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) members, err := GetTeamMembers(teamID) assert.NoError(t, err) assert.Len(t, members, team.NumMembers) for _, member := range members { - AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) + db.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) } } test(1) @@ -284,41 +285,41 @@ func TestGetTeamMembers(t *testing.T) { } func TestGetUserTeams(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(userID int64) { teams, _, err := SearchTeam(&SearchTeamOptions{UserID: userID}) assert.NoError(t, err) for _, team := range teams { - AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) + db.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) } } test(2) test(5) - test(NonexistentID) + test(db.NonexistentID) } func TestGetUserOrgTeams(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(orgID, userID int64) { teams, err := GetUserOrgTeams(orgID, userID) assert.NoError(t, err) for _, team := range teams { assert.EqualValues(t, orgID, team.OrgID) - AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) + db.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) } } test(3, 2) test(3, 4) - test(3, NonexistentID) + test(3, db.NonexistentID) } func TestAddTeamMember(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(teamID, userID int64) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, AddTeamMember(team, userID)) - AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) + db.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) } test(1, 2) @@ -327,42 +328,42 @@ func TestAddTeamMember(t *testing.T) { } func TestRemoveTeamMember(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) testSuccess := func(teamID, userID int64) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, RemoveTeamMember(team, userID)) - AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) + db.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) CheckConsistencyFor(t, &Team{ID: teamID}) } testSuccess(1, 4) testSuccess(2, 2) testSuccess(3, 2) - testSuccess(3, NonexistentID) + testSuccess(3, db.NonexistentID) - team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) err := RemoveTeamMember(team, 2) assert.True(t, IsErrLastOrgOwner(err)) } func TestHasTeamRepo(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(teamID, repoID int64, expected bool) { - team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.Equal(t, expected, HasTeamRepo(team.OrgID, teamID, repoID)) } test(1, 1, false) test(1, 3, true) test(1, 5, true) - test(1, NonexistentID, false) + test(1, db.NonexistentID, false) test(2, 3, true) test(2, 5, false) } func TestUsersInTeamsCount(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) test := func(teamIDs, userIDs []int64, expected int64) { count, err := UsersInTeamsCount(teamIDs, userIDs) |