Exclude deleted projects when counting projects associated with group ID

Signed-off-by: Daniel Jiang <jiangd@vmware.com>
This commit is contained in:
Daniel Jiang 2020-04-09 20:13:25 +08:00
parent 1b2efac156
commit 32ae0a6fa6
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)