Merge pull request #11555 from reasonerjt/exclude-deleted-projects

Exclude deleted groups when counting groups associated with group ID
This commit is contained in:
He Weiwei 2020-04-11 16:52:35 +08:00 committed by GitHub
commit 1ce0a76bd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 18 deletions

View File

@ -282,7 +282,6 @@ func TestGetGroupProjects(t *testing.T) {
if err != nil {
t.Errorf("Error occurred when AddProject: %v", err)
}
defer dao.DeleteProject(projectID2)
groupID, err := AddUserGroup(models.UserGroup{
GroupName: "test_group_03",
GroupType: 1,
@ -298,8 +297,18 @@ func TestGetGroupProjects(t *testing.T) {
EntityType: "g",
})
defer project.DeleteProjectMemberByID(pmid)
pmid2, err := project.AddProjectMember(models.Member{
ProjectID: projectID2,
EntityID: groupID,
EntityType: "g",
})
defer project.DeleteProjectMemberByID(pmid2)
if err := dao.DeleteProject(projectID2); err != nil {
t.Errorf("Error occurred when DeleteProject: %v", err)
}
type args struct {
query *models.ProjectQueryParam
query *models.ProjectQueryParam
groupIDs []int
}
member := &models.MemberQuery{
Name: "grouptestu09",
@ -311,22 +320,30 @@ func TestGetGroupProjects(t *testing.T) {
wantErr bool
}{
{"Query with group DN",
args{&models.ProjectQueryParam{
Member: member,
}},
args{
query: &models.ProjectQueryParam{
Member: member,
},
groupIDs: []int{groupID},
},
1, false},
{"Query without group DN",
args{&models.ProjectQueryParam{}},
1, false},
args{
query: &models.ProjectQueryParam{
Member: member,
},
groupIDs: []int{},
},
0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := dao.GetGroupProjects([]int{groupID}, tt.args.query)
got, err := dao.GetGroupProjects(tt.args.groupIDs, tt.args.query)
if (err != nil) != tt.wantErr {
t.Errorf("GetGroupProjects() error = %v, wantErr %v", err, tt.wantErr)
return
}
if len(got) < tt.wantSize {
if len(got) != tt.wantSize {
t.Errorf("GetGroupProjects() size: %v, want %v", len(got), tt.wantSize)
}
})
@ -349,7 +366,7 @@ func TestGetTotalGroupProjects(t *testing.T) {
if err != nil {
t.Errorf("Error occurred when AddProject: %v", err)
}
defer dao.DeleteProject(projectID2)
groupID, err := AddUserGroup(models.UserGroup{
GroupName: "test_group_05",
GroupType: 1,
@ -365,8 +382,18 @@ func TestGetTotalGroupProjects(t *testing.T) {
EntityType: "g",
})
defer project.DeleteProjectMemberByID(pmid)
pmid2, err := project.AddProjectMember(models.Member{
ProjectID: projectID2,
EntityID: groupID,
EntityType: "g",
})
defer project.DeleteProjectMemberByID(pmid2)
if err := dao.DeleteProject(projectID2); err != nil { // deleted project should not be counted
t.Errorf("Error occurred when delete project: %v", err)
}
type args struct {
query *models.ProjectQueryParam
query *models.ProjectQueryParam
groupIDs []int
}
tests := []struct {
name string
@ -375,20 +402,33 @@ func TestGetTotalGroupProjects(t *testing.T) {
wantErr bool
}{
{"Query with group ID",
args{&models.ProjectQueryParam{}},
1, false},
args{
query: &models.ProjectQueryParam{
Member: &models.MemberQuery{
Name: "member_test_01",
},
},
groupIDs: []int{groupID},
},
2, false},
{"Query without group ID",
args{&models.ProjectQueryParam{}},
args{
query: &models.ProjectQueryParam{
Member: &models.MemberQuery{
Name: "member_test_01",
},
},
groupIDs: []int{}},
1, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := dao.GetTotalGroupProjects([]int{groupID}, tt.args.query)
got, err := dao.GetTotalGroupProjects(tt.args.groupIDs, tt.args.query)
if (err != nil) != tt.wantErr {
t.Errorf("GetGroupProjects() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got < tt.wantSize {
if got != tt.wantSize {
t.Errorf("GetGroupProjects() size: %v, want %v", got, tt.wantSize)
}
})

View File

@ -166,7 +166,7 @@ func GetGroupProjects(groupIDs []int, query *models.ProjectQueryParam) ([]*model
from project p
left join project_member pm on p.project_id = pm.project_id
left join user_group ug on ug.id = pm.entity_id and pm.entity_type = 'g'
where ug.id in ( %s )`,
where p.deleted=false and ug.id in ( %s )`,
sql, groupIDCondition)
}
sql = sql + ` order by name`
@ -192,7 +192,7 @@ func GetTotalGroupProjects(groupIDs []int, query *models.ProjectQueryParam) (int
from project p
left join project_member pm on p.project_id = pm.project_id
left join user_group ug on ug.id = pm.entity_id and pm.entity_type = 'g'
where ug.id in ( %s )) t`,
where p.deleted=false and ug.id in ( %s )) t`,
sqlCondition, groupIDCondition)
}
log.Debugf("query sql:%v", sql)