Skip to content

Commit

Permalink
fix: fix GetRespondentDetails ,TestGetRespondentDetails and TestGetMy…
Browse files Browse the repository at this point in the history
…ResponseIDs
  • Loading branch information
Eraxyso authored Jan 9, 2025
1 parent c78d59e commit cf95822
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
14 changes: 8 additions & 6 deletions model/respondents_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -341,19 +347,15 @@ 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").
Where("response_id IN (?)", responseIDs)
}).
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)
Expand Down
47 changes: 28 additions & 19 deletions model/respondents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
},
{
Expand All @@ -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},
},
},
{
Expand All @@ -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},
},
},
{
Expand All @@ -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},
},
},
{
Expand All @@ -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},
},
},
{
Expand All @@ -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},
},
},
{
Expand All @@ -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},
},
},
{
Expand All @@ -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},
},
},
{
Expand All @@ -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},
},
},
}
Expand Down Expand Up @@ -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")
Expand All @@ -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")
}
}

Expand Down

0 comments on commit cf95822

Please sign in to comment.