From cf95822234c8d56d64cdc8cf489a446a301e830b Mon Sep 17 00:00:00 2001 From: Eraxyso <130852025+Eraxyso@users.noreply.github.com> Date: Thu, 9 Jan 2025 07:58:11 +0000 Subject: [PATCH] fix: fix GetRespondentDetails ,TestGetRespondentDetails and TestGetMyResponseIDs --- model/respondents_impl.go | 14 +++++++----- model/respondents_test.go | 47 +++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/model/respondents_impl.go b/model/respondents_impl.go index 456e3488..65902ae8 100755 --- a/model/respondents_impl.go +++ b/model/respondents_impl.go @@ -296,6 +296,9 @@ func (*Respondent) GetRespondentDetails(ctx context.Context, questionnaireID int Session(&gorm.Session{}). Where("respondents.questionnaire_id = ? AND respondents.submitted_at IS NOT NULL", questionnaireID). Select("ResponseID", "UserTraqid", "ModifiedAt", "SubmittedAt") + if onlyMyResponse { + query = query.Where("user_traqid = ?", userID) + } query, sortNum, err := setRespondentsOrder(query, sort) if err != nil { @@ -318,6 +321,9 @@ func (*Respondent) GetRespondentDetails(ctx context.Context, questionnaireID int } isAnonymous, err := NewQuestionnaire().GetResponseIsAnonymousByQuestionnaireID(ctx, questionnaireID) + if err != nil { + return nil, fmt.Errorf("failed to get response is anonymous by questionnaire id: %w", err) + } respondentDetails := make([]RespondentDetail, 0, len(respondents)) respondentDetailMap := make(map[int]*RespondentDetail, len(respondents)) @@ -341,7 +347,7 @@ func (*Respondent) GetRespondentDetails(ctx context.Context, questionnaireID int } questions := []Questions{} - query = db. + err = db. Preload("Responses", func(db *gorm.DB) *gorm.DB { return db. Select("ResponseID", "QuestionID", "Body"). @@ -349,11 +355,7 @@ func (*Respondent) GetRespondentDetails(ctx context.Context, questionnaireID int }). Where("questionnaire_id = ?", questionnaireID). Order("question_num"). - Select("ID", "Type") - if onlyMyResponse { - query = query.Where("user_traqid = ?", userID) - } - err = query. + Select("ID", "Type"). Find(&questions).Error if err != nil { return nil, fmt.Errorf("failed to get questions: %w", err) diff --git a/model/respondents_test.go b/model/respondents_test.go index e7879224..6b88b846 100644 --- a/model/respondents_test.go +++ b/model/respondents_test.go @@ -897,8 +897,8 @@ func TestGetRespondentDetails(t *testing.T) { userID: userOne, }, expect: expect{ - length: 3, - sortIdx: []int{0, 1, 2}, + length: 1, + sortIdx: []int{0}, }, }, { @@ -910,8 +910,8 @@ func TestGetRespondentDetails(t *testing.T) { userID: userOne, }, expect: expect{ - length: 3, - sortIdx: []int{2, 1, 0}, + length: 1, + sortIdx: []int{0}, }, }, { @@ -923,8 +923,8 @@ func TestGetRespondentDetails(t *testing.T) { userID: userOne, }, expect: expect{ - length: 3, - sortIdx: []int{0, 2, 1}, + length: 1, + sortIdx: []int{0}, }, }, { @@ -936,8 +936,8 @@ func TestGetRespondentDetails(t *testing.T) { userID: userOne, }, expect: expect{ - length: 3, - sortIdx: []int{1, 2, 0}, + length: 1, + sortIdx: []int{0}, }, }, { @@ -962,8 +962,8 @@ func TestGetRespondentDetails(t *testing.T) { userID: userOne, }, expect: expect{ - length: 3, - sortIdx: []int{2, 1, 0}, + length: 1, + sortIdx: []int{0}, }, }, { @@ -975,8 +975,8 @@ func TestGetRespondentDetails(t *testing.T) { userID: userOne, }, expect: expect{ - length: 3, - sortIdx: []int{0, 1, 2}, + length: 1, + sortIdx: []int{0}, }, }, { @@ -988,8 +988,8 @@ func TestGetRespondentDetails(t *testing.T) { userID: userOne, }, expect: expect{ - length: 3, - sortIdx: []int{0, 1, 2}, + length: 1, + sortIdx: []int{0}, }, }, { @@ -1001,8 +1001,8 @@ func TestGetRespondentDetails(t *testing.T) { userID: userOne, }, expect: expect{ - length: 3, - sortIdx: []int{2, 1, 0}, + length: 1, + sortIdx: []int{0}, }, }, { @@ -1026,8 +1026,8 @@ func TestGetRespondentDetails(t *testing.T) { userID: userOne, }, expect: expect{ - length: 3, - sortIdx: []int{0, 1, 2}, + length: 1, + sortIdx: []int{0}, }, }, } @@ -1238,7 +1238,16 @@ func TestGetMyResponseIDs(t *testing.T) { } for _, testCase := range testCases { + newMyResponseIDs := []int{} MyResponseIDs, err := respondentImpl.GetMyResponseIDs(ctx, testCase.args.sort, testCase.args.userID) + for _, MyResponseID := range MyResponseIDs { + for _, responseID := range responseIDs { + if MyResponseID == responseID { + newMyResponseIDs = append(newMyResponseIDs, responseID) + break + } + } + } if !testCase.expect.isErr { assertion.NoError(err, testCase.description, "no error") @@ -1251,7 +1260,7 @@ func TestGetMyResponseIDs(t *testing.T) { continue } - assertion.Equal(testCase.expect.responseIDs, MyResponseIDs, testCase.description, "responseIDs") + assertion.Equal(testCase.expect.responseIDs, newMyResponseIDs, testCase.description, "responseIDs") } }