From 32ae0a6fa6b7834b1b448227d56752498c91016c Mon Sep 17 00:00:00 2001 From: Daniel Jiang Date: Thu, 9 Apr 2020 20:13:25 +0800 Subject: [PATCH] Exclude deleted projects when counting projects associated with group ID Signed-off-by: Daniel Jiang --- src/common/dao/group/usergroup_test.go | 72 ++++++++++++++++++++------ src/common/dao/project.go | 4 +- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/common/dao/group/usergroup_test.go b/src/common/dao/group/usergroup_test.go index 9e7736b62..030c5654a 100644 --- a/src/common/dao/group/usergroup_test.go +++ b/src/common/dao/group/usergroup_test.go @@ -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) } }) diff --git a/src/common/dao/project.go b/src/common/dao/project.go index fec020f96..18a34ef1b 100644 --- a/src/common/dao/project.go +++ b/src/common/dao/project.go @@ -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)