Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog: add comments #1

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ A new binary will be created under `./build/regen`.

To run the node, you can refer to the documentation [here](https://docs.cosmos.network/master/run-node/).

## Tests
if you would like to run test, run:
```bash
make test
```

## Resources

- Cosmos SDK Documentation: https://docs.cosmos.network/master/.
Expand Down
14 changes: 13 additions & 1 deletion proto/blog/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ package blog.v1;
option go_package = "github.com/regen-network/bec/x/blog";

import "cosmos/base/query/v1beta1/pagination.proto";
import "blog/v1/common.proto";
import "blog/v1/types.proto";

// Query defines the gRPC querier service.
service Query {
rpc AllPosts(QueryAllPostsRequest) returns (QueryAllPostsResponse);
rpc AllComments(QueryAllCommentsRequest) returns (QueryAllCommentsResponse);
}

message QueryAllPostsRequest {
Expand All @@ -20,3 +21,14 @@ message QueryAllPostsResponse {

cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryAllCommentsRequest {
string postSlug = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message QueryAllCommentsResponse {
repeated Comment comments = 1;

cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
11 changes: 11 additions & 0 deletions proto/blog/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option go_package = "github.com/regen-network/bec/x/blog";
// Msg is the blog.v1 Msg service
service Msg {
rpc CreatePost(MsgCreatePost) returns (MsgCreatePostResponse);
rpc CreateComment(MsgCreateComment) returns (MsgCreateCommentResponse);
}

// MsgCreatePost is the Msg/CreatePost request type.
Expand All @@ -18,3 +19,13 @@ message MsgCreatePost {

// MsgCreatePostResponse is the Msg/CreatePost response type.
message MsgCreatePostResponse {}

// MsgCreateComment is the Msg/CreateComment request type.
message MsgCreateComment {
string post_slug = 1;
string author = 2;
string body = 3;
}

// MsgCreateCommentResponse is the Msg/CreateComment response type.
message MsgCreateCommentResponse {}
8 changes: 8 additions & 0 deletions proto/blog/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ message Post {
string title = 3;
string body = 4;
}

message Comment {
// post_slug is a short human-readable string for the post, used as unique
// identifier for the post.
string post_slug = 1;
string author = 2;
string body = 3;
}
4 changes: 2 additions & 2 deletions scripts/protocgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ protoc_gen_gocosmos

proto_dirs=$(find ./proto -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
for dir in $proto_dirs; do
buf protoc \
buf alpha protoc \
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protoc move to buf alpha

-I "proto" \
-I "third_party/proto" \
--gocosmos_out=plugins=interfacetype+grpc,\
Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types:. \
$(find "${dir}" -maxdepth 1 -name '*.proto')

# command to generate gRPC gateway (*.pb.gw.go in respective modules) files
buf protoc \
buf alpha protoc \
-I "proto" \
-I "third_party/proto" \
--grpc-gateway_out=logtostderr=true:. \
Expand Down
51 changes: 51 additions & 0 deletions x/blog/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,57 @@ func (s *IntegrationTestSuite) TestAllPosts() {
}
}

func (s *IntegrationTestSuite) TestCreateComment() {
val0 := s.network.Validators[0]

testCases := []struct {
postArgs []string
commentArgs []string
name string
expErr bool
expErrMsg string
}{
{
postArgs: []string{val0.Address.String(), "/post", "title", "body"},
commentArgs: []string{val0.Address.String(), "/post", "content"},
name: "valid comment request",
expErr: false,
expErrMsg: "",
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
postcmd := cli.CmdCreatePost()
args := append([]string{
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
}, tc.postArgs...)
_, err := clitestutil.ExecTestCLICmd(val0.ClientCtx, postcmd, args)
s.Require().NoError(err)

commentcmd := cli.CmdCreateComment()
args = append([]string{
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
}, tc.commentArgs...)
fmt.Printf("args %v+", args)
out, err := clitestutil.ExecTestCLICmd(val0.ClientCtx, commentcmd, args)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
var txRes sdk.TxResponse
err := val0.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes)
s.Require().NoError(err)
s.Require().Equal(uint32(0), txRes.Code)
Copy link
Owner Author

@eleijonmarck eleijonmarck Jun 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test failing currently due to:

...
    network.go:360: starting test network...
    network.go:365: started test network
    --- FAIL: TestKeeperTestSuite/TestCreateComment (3.34s)
        --- FAIL: TestKeeperTestSuite/TestCreateComment/valid_comment_request (3.34s)
            cli_test.go:230:
                	Error Trace:	cli_test.go:230
                	            				suite.go:77
                	Error:      	Not equal:
                	            	expected: 0x0
                	            	actual  : 0x12
                	Test:       	TestKeeperTestSuite/TestCreateComment/valid_comment_request
    cli_test.go:38: tearing down integration test suite

Have looked at: CreateComment method https://github.com/eleijonmarck/Blockchain-Engineer-Challenge/pull/1/files#diff-0a3af8172991c20cb50dfbc68b2c5a7b1da8f86849911f9962b249f184703399R42

But it does seem to run smoothly, so there might be an issue between sending the command to the grpc request to recieving the response. But looking for input here 🙏

}
})
}
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
34 changes: 34 additions & 0 deletions x/blog/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,37 @@ func CmdCreatePost() *cobra.Command {

return cmd
}

func CmdCreateComment() *cobra.Command {
cmd := &cobra.Command{
Use: "create-comment [author] [post_slug] [body]",
Short: "Creates a new comment on a post",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Flags().Set(flags.FlagFrom, args[0])
if err != nil {
return err
}

argsPostSlug := string(args[1])
argsBody := string(args[2])

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := &blog.MsgCreateComment{
Author: clientCtx.GetFromAddress().String(),
PostSlug: argsPostSlug,
Body: argsBody,
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
Loading