-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:givery-bootcamp/dena-2024-team10 in…
…to bug-fix/delete-post
- Loading branch information
Showing
14 changed files
with
726 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package usecases | ||
|
||
import ( | ||
"myapp/internal/entities" | ||
"myapp/internal/interfaces" | ||
) | ||
|
||
type GetAllPostCommentUsecase struct { | ||
CommentRepository interfaces.CommentRepository | ||
} | ||
|
||
func NewGetAllPostCommentUsecase(cr interfaces.CommentRepository) *GetAllPostCommentUsecase { | ||
return &GetAllPostCommentUsecase{ | ||
CommentRepository: cr, | ||
} | ||
} | ||
|
||
func (u *GetAllPostCommentUsecase) Execute(postId, limit, offset int64) ([]*entities.Comment, error) { | ||
// Validate limit | ||
if limit <= 0 { | ||
limit = 20 | ||
} | ||
if limit > 100 { | ||
limit = 100 | ||
} | ||
|
||
// Validate offset | ||
if offset < 0 { | ||
offset = 0 | ||
} | ||
|
||
comments, err := u.CommentRepository.GetAllByPostId(postId, limit, offset) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return comments, nil | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/test/mock/mock_interfaces/comment_repository_interface.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package usecases | ||
|
||
import ( | ||
"fmt" | ||
"myapp/internal/entities" | ||
"myapp/internal/usecases" | ||
"myapp/test/mock/mock_interfaces" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
gomock "go.uber.org/mock/gomock" | ||
) | ||
|
||
type getAllPostCommentInput struct { | ||
postId int64 | ||
limit int64 | ||
offset int64 | ||
} | ||
|
||
type responceFromCommentRepositoryGetAll struct { | ||
comments []*entities.Comment | ||
err error | ||
} | ||
|
||
func TestGetAllPostComment(t *testing.T) { | ||
testcases := []struct { | ||
testName string | ||
usecaseInput *getAllPostCommentInput | ||
repositoryOutput *responceFromCommentRepositoryGetAll | ||
expectedComment []*entities.Comment | ||
expectedError error | ||
}{ | ||
{ | ||
"Success get all posts", | ||
&getAllPostCommentInput{ | ||
postId: 1, | ||
limit: 1, | ||
offset: 0, | ||
}, | ||
&responceFromCommentRepositoryGetAll{ | ||
[]*entities.Comment{ | ||
{ | ||
Id: 1, | ||
PostId: 1, | ||
UserId: 1, | ||
Body: "mock comment", | ||
CreatedAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), | ||
UpdatedAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), | ||
}, | ||
}, | ||
nil, | ||
}, | ||
[]*entities.Comment{ | ||
{ | ||
Id: 1, | ||
PostId: 1, | ||
UserId: 1, | ||
Body: "mock comment", | ||
CreatedAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), | ||
UpdatedAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), | ||
}, | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"Success get all comments with empty posts", | ||
&getAllPostCommentInput{ | ||
postId: 1, | ||
limit: 1, | ||
offset: 0, | ||
}, | ||
&responceFromCommentRepositoryGetAll{ | ||
[]*entities.Comment{}, | ||
nil, | ||
}, | ||
[]*entities.Comment{}, | ||
nil, | ||
}, | ||
{ | ||
"Fail with error from GetAllCommentByPostId", | ||
&getAllPostCommentInput{ | ||
1, | ||
1, | ||
0, | ||
}, | ||
&responceFromCommentRepositoryGetAll{ | ||
nil, | ||
fmt.Errorf("error from GetAllCommentByPostId"), | ||
}, | ||
nil, | ||
fmt.Errorf("error from GetAllCommentByPostId"), | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.testName, func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
mockCommentRepository := mock_interfaces.NewMockCommentRepository(mockCtrl) | ||
mockCommentRepository.EXPECT(). | ||
GetAllByPostId(tc.usecaseInput.postId, tc.usecaseInput.limit, tc.usecaseInput.offset). | ||
Return(tc.repositoryOutput.comments, tc.repositoryOutput.err) | ||
usecase := usecases.NewGetAllPostCommentUsecase(mockCommentRepository) | ||
comments, err := usecase.Execute(tc.usecaseInput.postId, tc.usecaseInput.limit, tc.usecaseInput.offset) | ||
|
||
assert.Equal(t, tc.expectedError, err) | ||
assert.Len(t, comments, len(tc.expectedComment)) | ||
for i, comment := range comments { | ||
assert.Equal(t, tc.expectedComment[i], comment) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.