Skip to content

Commit

Permalink
fix: fix getQuestionnairesTest and GetQuestionnaires
Browse files Browse the repository at this point in the history
  • Loading branch information
Eraxyso authored Jan 14, 2025
1 parent d168125 commit e0f9dbb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 146 deletions.
2 changes: 1 addition & 1 deletion model/questionnaires_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (*Questionnaire) GetQuestionnaires(ctx context.Context, userID string, sort
err = query.
Limit(20).
Offset(offset).
Group("questionnaires.id").
Group("questionnaires.id, targets.user_traqid").
Select("questionnaires.*, (targets.user_traqid = ? OR targets.user_traqid = 'traP') AS is_targeted", userID).
Find(&questionnaires).Error
if errors.Is(err, context.DeadlineExceeded) {
Expand Down
179 changes: 34 additions & 145 deletions model/questionnaires_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,166 +1064,51 @@ func getQuestionnairesTest(t *testing.T) {
},
},
{
description: "userID:valid, sort:created_at, search:no, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "created_at",
search: "",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: true,
},
},
{
description: "userID:valid, sort:-created_at, search:no, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "-created_at",
search: "",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: true,
},
},
{
description: "userID:valid, sort:title, search:no, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "title",
search: "",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: true,
},
},
{
description: "userID:valid, sort:-title, search:no, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "-title",
search: "",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: true,
},
},
{
description: "userID:valid, sort:modified_at, search:no, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "modified_at",
search: "",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: true,
},
},
{
description: "userID:valid, sort:-modified_at, search:no, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "-modified_at",
search: "",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: true,
},
},
{
description: "userID:valid, sort:no, search:GetQuestionnaireTest$, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "GetQuestionnaireTest$",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: true,
},
expect: expect{
isCheckLen: true,
length: 4,
},
},
{
description: "userID:valid, sort:no, search:no, page:2",
description: "userID:valid, sort:no, search:no, page:1, onlytargetted",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "",
pageNum: 2,
onlyTargetingMe: true,
onlyAdministratedByMe: true,
},
},
{
description: "too large page",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "",
pageNum: 100000,
pageNum: 1,
onlyTargetingMe: true,
onlyAdministratedByMe: true,
},
expect: expect{
isErr: true,
err: ErrTooLargePageNum,
},
},
{
description: "userID:valid, sort:no, search:no, page:1, nontargetted",
description: "userID:valid, sort:no, search:no, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "",
pageNum: 1,
onlyTargetingMe: true,
onlyAdministratedByMe: true,
onlyTargetingMe: false,
onlyAdministratedByMe: false,
},
},
{
description: "userID:valid, sort:no, search:notFoundQuestionnaire, page:1",
description: "userID:valid, sort:no, search:no, page:1, onlytargetted, onlyadministrated",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "notFoundQuestionnaire",
search: "",
pageNum: 1,
onlyTargetingMe: true,
onlyAdministratedByMe: true,
},
expect: expect{
isCheckLen: false,
length: 0,
onlyAdministratedByMe: false,
},
},
{
description: "userID:valid, sort:invalid, search:no, page:1",
description: "userID:valid, sort:created_at, search:no, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "hogehoge",
sort: "created_at",
search: "",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: true,
},
expect: expect{
isErr: true,
err: ErrInvalidSortParam,
},
},
{
description: "userID:valid, sort:no, search:no, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: false,
},
},
{
description: "userID:valid, sort:created_at, search:no, page:1",
description: "userID:valid, sort:created_at, search:no, page:1, onlyadministrated",
args: args{
userID: questionnairesTestUserID,
sort: "created_at",
Expand Down Expand Up @@ -1303,14 +1188,29 @@ func getQuestionnairesTest(t *testing.T) {
length: 4,
},
},
{
description: "userID:valid, sort:no, search:GetQuestionnaireTest$, page:1, onlyadministrated",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "GetQuestionnaireTest$",
pageNum: 1,
onlyTargetingMe: false,
onlyAdministratedByMe: true,
},
expect: expect{
isCheckLen: true,
length: 2,
},
},
{
description: "userID:valid, sort:no, search:no, page:2",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "",
pageNum: 2,
onlyTargetingMe: true,
onlyTargetingMe: false,
onlyAdministratedByMe: false,
},
},
Expand All @@ -1321,37 +1221,26 @@ func getQuestionnairesTest(t *testing.T) {
sort: "",
search: "",
pageNum: 100000,
onlyTargetingMe: true,
onlyTargetingMe: false,
onlyAdministratedByMe: false,
},
expect: expect{
isErr: true,
err: ErrTooLargePageNum,
},
},
{
description: "userID:valid, sort:no, search:no, page:1, nontargetted",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "",
pageNum: 1,
onlyTargetingMe: true,
onlyAdministratedByMe: false,
},
},
{
description: "userID:valid, sort:no, search:notFoundQuestionnaire, page:1",
args: args{
userID: questionnairesTestUserID,
sort: "",
search: "notFoundQuestionnaire",
pageNum: 1,
onlyTargetingMe: true,
onlyTargetingMe: false,
onlyAdministratedByMe: false,
},
expect: expect{
isCheckLen: false,
isCheckLen: true,
length: 0,
},
},
Expand Down Expand Up @@ -1403,13 +1292,13 @@ func getQuestionnairesTest(t *testing.T) {
actualQuestionnaireIDs = append(actualQuestionnaireIDs, questionnaire.ID)
}
if testCase.args.onlyTargetingMe {
for _, targettedQuestionnaireID := range userTargetMap[questionnairesTestUserID] {
assertion.NotContains(actualQuestionnaireIDs, targettedQuestionnaireID, testCase.description, "not contain(targetted)")
for _, actualQuestionnaireID := range actualQuestionnaireIDs {
assertion.Contains(userTargetMap[questionnairesTestUserID], actualQuestionnaireID, testCase.description, "contain(targetted)")
}
}
if testCase.args.onlyAdministratedByMe {
for _, targettedQuestionnaireID := range userTargetMap[questionnairesTestUserID] {
assertion.NotContains(actualQuestionnaireIDs, targettedQuestionnaireID, testCase.description, "not contain(administrated)")
for _, actualQuestionnaireID := range actualQuestionnaireIDs {
assertion.Contains(userAdministratorMap[questionnairesTestUserID], actualQuestionnaireID, testCase.description, "contain(administrated)")
}
}
for _, deletedQuestionnaireID := range deletedQuestionnaireIDs {
Expand Down

0 comments on commit e0f9dbb

Please sign in to comment.