Skip to content
Merged
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ accounts.txt
.editorconfig
.manual
release/
github.token
github.token
*.back
*.bak
188 changes: 188 additions & 0 deletions api/handler/accountDatasetHandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package handler

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

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

type (
mockGetAccountDatasetsError struct {
service.AccountDatasetServiceInterface
}
mockGetAccountDatasetsSuccess struct {
service.AccountDatasetServiceInterface
}
)

func (*mockGetAccountDatasetsError) GetAccountDatasets(request *model.GetAccountDatasetsRequest) (*model.GetAccountDatasetsResponse, error) {
return nil, errors.New("Error GetAccountDatasets")
}
func (*mockGetAccountDatasetsSuccess) GetAccountDatasets(request *model.GetAccountDatasetsRequest) (*model.GetAccountDatasetsResponse, error) {
return &model.GetAccountDatasetsResponse{}, nil
}

func TestAccountDatasetHandler_GetAccountDatasets(t *testing.T) {
type fields struct {
Service service.AccountDatasetServiceInterface
}
type args struct {
in0 context.Context
request *model.GetAccountDatasetsRequest
}
tests := []struct {
name string
fields fields
args args
want *model.GetAccountDatasetsResponse
wantErr bool
}{
{
name: "GetAccountDatasets:LimitExceeded",
args: args{
request: &model.GetAccountDatasetsRequest{
Pagination: &model.Pagination{
Limit: uint32(600),
},
},
},
want: nil,
wantErr: true,
},
{
name: "GetAccountDatasets:Error",
fields: fields{
Service: &mockGetAccountDatasetsError{},
},
args: args{
request: &model.GetAccountDatasetsRequest{
Pagination: &model.Pagination{
Limit: uint32(250),
},
},
},
want: nil,
wantErr: true,
},
{
name: "GetAccountDatasets:Success",
fields: fields{
Service: &mockGetAccountDatasetsSuccess{},
},
args: args{
request: &model.GetAccountDatasetsRequest{
Pagination: &model.Pagination{
Limit: uint32(250),
},
},
},
want: &model.GetAccountDatasetsResponse{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
adh := &AccountDatasetHandler{
Service: tt.fields.Service,
}
got, err := adh.GetAccountDatasets(tt.args.in0, tt.args.request)
if (err != nil) != tt.wantErr {
t.Errorf("AccountDatasetHandler.GetAccountDatasets() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AccountDatasetHandler.GetAccountDatasets() = %v, want %v", got, tt.want)
}
})
}
}

type (
mockGetAccountDatasetError struct {
service.AccountDatasetServiceInterface
}
mockGetAccountDatasetSuccess struct {
service.AccountDatasetServiceInterface
}
)

func (*mockGetAccountDatasetError) GetAccountDataset(request *model.GetAccountDatasetRequest) (*model.AccountDataset, error) {
return nil, errors.New("Error GetAccountDataset")
}
func (*mockGetAccountDatasetSuccess) GetAccountDataset(request *model.GetAccountDatasetRequest) (*model.AccountDataset, error) {
return &model.AccountDataset{}, nil
}

func TestAccountDatasetHandler_GetAccountDataset(t *testing.T) {
type fields struct {
Service service.AccountDatasetServiceInterface
}
type args struct {
in0 context.Context
request *model.GetAccountDatasetRequest
}
tests := []struct {
name string
fields fields
args args
want *model.AccountDataset
wantErr bool
}{
{
name: "GetAccountDataset:InvalidRequest",
args: args{
request: &model.GetAccountDatasetRequest{
RecipientAccountAddress: "",
Property: "",
},
},
want: nil,
wantErr: true,
},
{
name: "GetAccountDataset:Error",
fields: fields{
Service: &mockGetAccountDatasetError{},
},
args: args{
request: &model.GetAccountDatasetRequest{
RecipientAccountAddress: "H1ftvv3n6CF5NDzdjmZKLRrBg6yPKHXpmatVUhQ5NWYx",
},
},
want: nil,
wantErr: true,
},
{
name: "GetAccountDataset:Success",
fields: fields{
Service: &mockGetAccountDatasetSuccess{},
},
args: args{
request: &model.GetAccountDatasetRequest{
RecipientAccountAddress: "H1ftvv3n6CF5NDzdjmZKLRrBg6yPKHXpmatVUhQ5NWYx",
},
},
want: &model.AccountDataset{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
adh := &AccountDatasetHandler{
Service: tt.fields.Service,
}
got, err := adh.GetAccountDataset(tt.args.in0, tt.args.request)
if (err != nil) != tt.wantErr {
t.Errorf("AccountDatasetHandler.GetAccountDataset() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AccountDatasetHandler.GetAccountDataset() = %v, want %v", got, tt.want)
}
})
}
}
184 changes: 184 additions & 0 deletions api/handler/blockHandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
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 (
mockGetBlockError struct {
service.BlockServiceInterface
}
mockGetBlockSuccess struct {
service.BlockServiceInterface
}
)

func (*mockGetBlockError) GetBlockByID(chainType chaintype.ChainType, id int64) (*model.BlockExtendedInfo, error) {
return nil, errors.New("Error GetBlockByID")
}

func (*mockGetBlockError) GetBlockByHeight(chainType chaintype.ChainType, height uint32) (*model.BlockExtendedInfo, error) {
return nil, errors.New("Error GetBlockByHeight")
}

func (*mockGetBlockSuccess) GetBlockByID(chainType chaintype.ChainType, id int64) (*model.BlockExtendedInfo, error) {
return &model.BlockExtendedInfo{}, nil
}

func (*mockGetBlockSuccess) GetBlockByHeight(chainType chaintype.ChainType, height uint32) (*model.BlockExtendedInfo, error) {
return &model.BlockExtendedInfo{}, nil
}

func TestBlockHandler_GetBlock(t *testing.T) {
type fields struct {
Service service.BlockServiceInterface
}
type args struct {
ctx context.Context
req *model.GetBlockRequest
}
tests := []struct {
name string
fields fields
args args
want *model.BlockExtendedInfo
wantErr bool
}{
{
name: "GetBlock:Error",
fields: fields{
Service: &mockGetBlockError{},
},
args: args{
req: &model.GetBlockRequest{
ID: 1,
Height: 1,
},
},
want: nil,
wantErr: true,
},
{
name: "GetBlock:Success",
fields: fields{
Service: &mockGetBlockSuccess{},
},
args: args{
req: &model.GetBlockRequest{
ID: 1,
Height: 1,
},
},
want: &model.BlockExtendedInfo{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bs := &BlockHandler{
Service: tt.fields.Service,
}
got, err := bs.GetBlock(tt.args.ctx, tt.args.req)
if (err != nil) != tt.wantErr {
t.Errorf("BlockHandler.GetBlock() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("BlockHandler.GetBlock() = %v, want %v", got, tt.want)
}
})
}
}

type (
mockGetBlocksError struct {
service.BlockServiceInterface
}
mockGetBlocksSucess struct {
service.BlockServiceInterface
}
)

func (*mockGetBlocksError) GetBlocks(chainType chaintype.ChainType, count, height uint32) (*model.GetBlocksResponse, error) {
return nil, errors.New("Error GetBlocks")
}

func (*mockGetBlocksSucess) GetBlocks(chainType chaintype.ChainType, count, height uint32) (*model.GetBlocksResponse, error) {
return &model.GetBlocksResponse{}, nil
}

func TestBlockHandler_GetBlocks(t *testing.T) {
type fields struct {
Service service.BlockServiceInterface
}
type args struct {
ctx context.Context
req *model.GetBlocksRequest
}
tests := []struct {
name string
fields fields
args args
want *model.GetBlocksResponse
wantErr bool
}{
{
name: "GetBlocks:LimitExceeded",
args: args{
req: &model.GetBlocksRequest{
Limit: 1000,
},
},
want: nil,
wantErr: true,
},
{
name: "GetBlocks:Error",
args: args{
req: &model.GetBlocksRequest{
Limit: 500,
},
},
fields: fields{
Service: &mockGetBlocksError{},
},
want: nil,
wantErr: true,
},
{
name: "GetBlocks:Success",
args: args{
req: &model.GetBlocksRequest{
Limit: 500,
},
},
fields: fields{
Service: &mockGetBlocksSucess{},
},
want: &model.GetBlocksResponse{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bs := &BlockHandler{
Service: tt.fields.Service,
}
got, err := bs.GetBlocks(tt.args.ctx, tt.args.req)
if (err != nil) != tt.wantErr {
t.Errorf("BlockHandler.GetBlocks() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("BlockHandler.GetBlocks() = %v, want %v", got, tt.want)
}
})
}
}
Loading