Skip to content

Commit

Permalink
#45 upgrade unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
setiadijoe committed Jun 3, 2021
1 parent 180dacd commit 86d93d1
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func MakeCreateComment(ctx context.Context, usecase usecase.UsecaseI) endpoint.E
if err = usecase.CreateCommentOnPost(ctx, &model.CreateCommentRequest{
UserPostID: req.UserPostID,
Text: req.Comment,
Status: helper.GetInt64FromPointer(req.Status),
}); err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions endpoint/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ func Validate(in interface{}) error {
)
} else if obj, ok := in.(*UpdateStatusOrTitle); ok {
err = validation.ValidateStruct(in,
validation.Field(obj.ID, validation.Required),
validation.Field(&obj.ID, validation.Required),
validation.Field(&obj.Title, validation.Required, validation.Length(10, 0)),
validation.Field(&obj.Status, validation.Required, validation.In(helper.ACTIVED, helper.DELETED, helper.INACTIVED)),
)
} else if obj, ok := in.(*CreateCommentRequest); ok {
err = validation.ValidateStruct(in,
validation.Field(obj.UserPostID, validation.Required),
validation.Field(obj.Comment, validation.Required, validation.Length(10, 0)),
validation.Field(&obj.UserPostID, validation.Required),
validation.Field(&obj.Comment, validation.Required, validation.Length(10, 0)),
validation.Field(&obj.Status, validation.Required, validation.In(helper.ACTIVED, helper.DELETED, helper.INACTIVED)),
)
}
Expand Down
2 changes: 1 addition & 1 deletion mocks/testcases/create-new-post.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var newRepositoryRequest = &model.CreateNewPostRequestRepository{
Images: "[{\"path\":\"http://localhost\"}]",
Tags: tags,
Status: helper.ACTIVED,
// ActorID: 1,
ActorID: 1,
}

type CreateNewUserPost struct {
Expand Down
3 changes: 2 additions & 1 deletion mocks/testcases/get-list-user-post.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ var (
actorResponse = &model.UserResponse{
ID: 1,
Name: sql.NullString{String: "John Doe", Valid: true},
PhotoURL: sql.NullString{String: "sample", Valid: true},
PhotoURL: sql.NullString{String: "www.instagram.com/htm-medium=?p9878y2y3", Valid: true},
Role: sql.NullInt64{Int64: 99, Valid: true},
Regency: sql.NullString{String: "regency", Valid: true},
District: sql.NullString{String: "district", Valid: true},
Village: sql.NullString{String: "village", Valid: true},
RW: sql.NullString{String: "rw", Valid: true},
Status: 10,
}
actor = &model.Actor{
ID: 1,
Expand Down
1 change: 1 addition & 0 deletions model/repository_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type CreateCommentRequestRepository struct {
UserPostID int64
Text string
ActorID int64
Status int64
}

type CreateNewPostRequestRepository struct {
Expand Down
1 change: 1 addition & 0 deletions model/usecase_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type UpdatePostRequest struct {
type CreateCommentRequest struct {
UserPostID int64 `json:"user_post_id"`
Text string `json:"comment"`
Status int64 `json:"status"`
}

type ActorFromContext struct {
Expand Down
5 changes: 3 additions & 2 deletions repository/mysql/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ func (r *Comment) Create(ctx context.Context, req *model.CreateCommentRequestRep
var err error
_, unixTime := helper.GetCurrentTimeUTC()

query.WriteString("INSERT INTO user_post_comments (user_post_id, `text`, status, created_by, updated_by, created_at, updated_at)")
query.WriteString("VALUES(:user_post_id. :comment, :status, :actor, :actor, :current, :current)")
query.WriteString("INSERT INTO user_post_comments (user_post_id, `text`, status, created_by, updated_by, created_at, updated_at) ")
query.WriteString("VALUES(:user_post_id, :comment, :status, :actor, :actor, :current, :current)")
params := map[string]interface{}{
"user_post_id": req.UserPostID,
"comment": req.Text,
"actor": req.ActorID,
"current": unixTime,
"status": req.Status,
}

if ctx != nil {
Expand Down
7 changes: 5 additions & 2 deletions usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,15 @@ func (p *Post) GetCommentsByPostID(ctx context.Context, id int64) ([]*model.Comm
}

func (p *Post) CreateCommentOnPost(ctx context.Context, req *model.CreateCommentRequest) error {
// TODO: implement authorization and authenticationn
logger := kitlog.With(p.logger, "method", "CreateCommentOnPost")
actor := ctx.Value(helper.ACTORKEY).(*model.ActorFromContext).Data
// actor := ctx.Value(helper.ACTORKEY).(*model.ActorFromContext).Data
if err := p.repoComment.Create(ctx, &model.CreateCommentRequestRepository{
UserPostID: req.UserPostID,
Text: req.Text,
ActorID: actor["id"].(int64),
Status: req.Status,
// ActorID: actor["id"].(int64),
ActorID: 1, // TODO: actor not existed yet using admin as default
}); err != nil {
level.Error(logger).Log("error_create_comment", err)
return err
Expand Down
1 change: 1 addition & 0 deletions usecase/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var _ = Describe("Usecase", func() {
Expect(err).NotTo(BeNil())
Expect(resp).To(BeNil())
} else {
fmt.Println(resp)
Expect(resp).To(Equal(data.ResponseUsecase.Result))
}
}
Expand Down

0 comments on commit 86d93d1

Please sign in to comment.