Skip to content

Unit test api mempool handler #831

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

Merged
merged 5 commits into from
May 14, 2020
Merged
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
148 changes: 148 additions & 0 deletions api/handler/mempoolHandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package handler

import (
"context"
"errors"
"reflect"
"testing"

"github.com/zoobc/zoobc-core/api/service"
"github.com/zoobc/zoobc-core/common/chaintype"
"github.com/zoobc/zoobc-core/common/model"
)

type (
MockGetMempoolTransactionError struct {
service.MempoolTransactionServiceInterface
}
MockGetMempoolTransactionSuccess struct {
service.MempoolTransactionServiceInterface
}
)

func (*MockGetMempoolTransactionError) GetMempoolTransaction(chainType chaintype.ChainType, params *model.GetMempoolTransactionRequest,
) (*model.GetMempoolTransactionResponse, error) {
return nil, errors.New("Error GetMempoolTransaction")
}

func (*MockGetMempoolTransactionSuccess) GetMempoolTransaction(chainType chaintype.ChainType, params *model.GetMempoolTransactionRequest,
) (*model.GetMempoolTransactionResponse, error) {
return &model.GetMempoolTransactionResponse{}, nil
}

func TestMempoolTransactionHandler_GetMempoolTransaction(t *testing.T) {
type fields struct {
Service service.MempoolTransactionServiceInterface
}
type args struct {
ctx context.Context
req *model.GetMempoolTransactionRequest
}
tests := []struct {
name string
fields fields
args args
want *model.GetMempoolTransactionResponse
wantErr bool
}{
{
name: "GetMempoolTransaction:Error",
fields: fields{
Service: &MockGetMempoolTransactionError{},
},
want: nil,
wantErr: true,
},
{
name: "GetMempoolTransaction:Success",
fields: fields{
Service: &MockGetMempoolTransactionSuccess{},
},
want: &model.GetMempoolTransactionResponse{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
uth := &MempoolTransactionHandler{
Service: tt.fields.Service,
}
got, err := uth.GetMempoolTransaction(tt.args.ctx, tt.args.req)
if (err != nil) != tt.wantErr {
t.Errorf("MempoolTransactionHandler.GetMempoolTransaction() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MempoolTransactionHandler.GetMempoolTransaction() = %v, want %v", got, tt.want)
}
})
}
}

type (
MockGetMempoolTransactionsError struct {
service.MempoolTransactionServiceInterface
}
MockGetMempoolTransactionsSuccess struct {
service.MempoolTransactionServiceInterface
}
)

func (*MockGetMempoolTransactionsError) GetMempoolTransactions(chainType chaintype.ChainType, params *model.GetMempoolTransactionsRequest,
) (*model.GetMempoolTransactionsResponse, error) {
return nil, errors.New("Error GetMempoolTransactions")
}

func (*MockGetMempoolTransactionsSuccess) GetMempoolTransactions(chainType chaintype.ChainType, params *model.GetMempoolTransactionsRequest,
) (*model.GetMempoolTransactionsResponse, error) {
return &model.GetMempoolTransactionsResponse{}, nil
}

func TestMempoolTransactionHandler_GetMempoolTransactions(t *testing.T) {
type fields struct {
Service service.MempoolTransactionServiceInterface
}
type args struct {
ctx context.Context
req *model.GetMempoolTransactionsRequest
}
tests := []struct {
name string
fields fields
args args
want *model.GetMempoolTransactionsResponse
wantErr bool
}{
{
name: "GetMempoolTransactions:Error",
fields: fields{
Service: &MockGetMempoolTransactionsError{},
},
want: nil,
wantErr: true,
},
{
name: "GetMempoolTransactions:Success",
fields: fields{
Service: &MockGetMempoolTransactionsSuccess{},
},
want: &model.GetMempoolTransactionsResponse{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
uth := &MempoolTransactionHandler{
Service: tt.fields.Service,
}
got, err := uth.GetMempoolTransactions(tt.args.ctx, tt.args.req)
if (err != nil) != tt.wantErr {
t.Errorf("MempoolTransactionHandler.GetMempoolTransactions() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MempoolTransactionHandler.GetMempoolTransactions() = %v, want %v", got, tt.want)
}
})
}
}