Skip to content

Commit

Permalink
updated logic of namespace.GetMessages handler, added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vvuwei committed Oct 17, 2023
1 parent 9baf5ce commit fc6191b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cmd/api/handler/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package handler
import (
"encoding/base64"
"encoding/hex"
storageTypes "github.com/dipdup-io/celestia-indexer/internal/storage/types"
"github.com/dipdup-io/celestia-indexer/pkg/types"
"net/http"

Expand Down Expand Up @@ -267,6 +268,7 @@ type getNamespaceMessages struct {
// @ID get-namespace-messages
// @Param id path string true "Namespace id in hexadecimal" minlength(56) maxlength(56)
// @Param version path integer true "Version of namespace"
// @Param type query string false "Type of message"
// @Param limit query integer false "Count of requested entities" minimum(1) maximum(100)
// @Param offset query integer false "Offset" minimum(1)
// @Produce json
Expand All @@ -291,7 +293,13 @@ func (handler *NamespaceHandler) GetMessages(c echo.Context) error {
return err
}

messages, err := handler.namespace.Messages(c.Request().Context(), ns.Id, nil, int(req.Limit), int(req.Offset))
var msgType *storageTypes.MsgType
msgT, err := storageTypes.ParseMsgType(req.Type)
if err == nil {
msgType = &msgT
}

messages, err := handler.namespace.Messages(c.Request().Context(), ns.Id, msgType, int(req.Limit), int(req.Offset))
if err := handleError(c, err, handler.namespace); err != nil {
return err
}
Expand Down
78 changes: 78 additions & 0 deletions cmd/api/handler/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,84 @@ func (s *NamespaceTestSuite) TestGetMessages() {
s.Require().EqualValues(1, msg.Tx.Id)
}

func (s *NamespaceTestSuite) TestGetMessagesByType() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/namespace/:id/:version/messages?type=MsgBeginRedelegate")
c.SetParamNames("id", "version")
c.SetParamValues(testNamespaceId, "1")
c.QueryParams().Set("type", "MsgBeginRedelegate")

s.namespaces.EXPECT().
ByNamespaceIdAndVersion(gomock.Any(), testNamespace.NamespaceID, byte(1)).
Return(testNamespace, nil)

msgType := types.MsgBeginRedelegate
s.namespaces.EXPECT().
Messages(gomock.Any(), testNamespace.Id, &msgType, 0, 0).
Return([]storage.NamespaceMessage{
{
NamespaceId: testNamespace.Id,
MsgId: 1,
Message: &storage.Message{
Id: 1,
TxId: 2,
Position: 3,
Type: types.MsgBeginRedelegate,
Height: 100,
Time: testTime,
},
TxId: 1,
Tx: &testTx,
Namespace: &testNamespace,
},
}, nil)

s.Require().NoError(s.handler.GetMessages(c))
s.Require().Equal(http.StatusOK, rec.Code)

var msgs []responses.NamespaceMessage
err := json.NewDecoder(rec.Body).Decode(&msgs)
s.Require().NoError(err)
s.Require().Len(msgs, 1)

msg := msgs[0]
s.Require().EqualValues(1, msg.Id)
s.Require().EqualValues(100, msg.Height)
s.Require().EqualValues(3, msg.Position)
s.Require().Equal(testTime, msg.Time)
s.Require().EqualValues(string(types.MsgBeginRedelegate), msg.Type)
s.Require().EqualValues(1, msg.Tx.Id)
}

func (s *NamespaceTestSuite) TestGetMessagesByTypeWithoutMessages() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/namespace/:id/:version/messages?type=MsgSend")
c.SetParamNames("id", "version")
c.SetParamValues(testNamespaceId, "1")
c.QueryParams().Set("type", "MsgSend")

s.namespaces.EXPECT().
ByNamespaceIdAndVersion(gomock.Any(), testNamespace.NamespaceID, byte(1)).
Return(testNamespace, nil)

msgType := types.MsgSend
s.namespaces.EXPECT().
Messages(gomock.Any(), testNamespace.Id, &msgType, 0, 0).
Return([]storage.NamespaceMessage{}, nil)

s.Require().NoError(s.handler.GetMessages(c))
s.Require().Equal(http.StatusOK, rec.Code)

var msgs []responses.NamespaceMessage
err := json.NewDecoder(rec.Body).Decode(&msgs)
s.Require().NoError(err)
s.Require().Empty(msgs)
}

func (s *NamespaceTestSuite) TestCount() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
Expand Down

0 comments on commit fc6191b

Please sign in to comment.