Skip to content

Commit

Permalink
[TEST] optimize unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
myth committed May 17, 2024
1 parent acdcffd commit b056c6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
10 changes: 6 additions & 4 deletions internal/contract/services/contract_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ func (s *ContractReaderService) SendTransactionV2(ctx context.Context, req *pb.S
if userCtx.UserID == "" {
return nil, status.Errorf(codes.Unauthenticated, "unauthenticated")
}

var amount big.Int
if _, ok := amount.SetString(req.Amount, 10); !ok {
return nil, status.Errorf(codes.InvalidArgument, "amount is not valid")
}

resp, err := s.userClient.GetUserPrivateKeyByID(ctx, &userPb.GetUserPrivateKeyByIDRequest{
UserId: userCtx.UserID,
})
Expand All @@ -188,10 +194,6 @@ func (s *ContractReaderService) SendTransactionV2(ctx context.Context, req *pb.S
if err != nil {
return nil, status.Errorf(codes.Internal, "unable to get balance: %v", err)
}
var amount big.Int
if _, ok := amount.SetString(req.Amount, 10); !ok {
return nil, status.Errorf(codes.InvalidArgument, "amount is not valid")
}

if balance.Cmp(&amount) < 0 {
return nil, status.Errorf(codes.InvalidArgument, "balance is not enough")
Expand Down
21 changes: 20 additions & 1 deletion internal/contract/services/contract_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestContractReaderService_SendTransactionV2(t *testing.T) {
},
},
{
name: "error unauthen ticated",
name: "error unauthenticated",
args: args{
ctx: context.Background(),
req: &pb.SendTransactionV2Request{
Expand All @@ -88,6 +88,25 @@ func TestContractReaderService_SendTransactionV2(t *testing.T) {
setup: func(fields fields) {
},
},
{
name: "error amount is not valid",
args: args{
ctx: ctx,
req: &pb.SendTransactionV2Request{
Signature: "0xc83d417a3b99535e784a72af0d9772c019c776aa0dfe4313c001a5548f6cf254477f5334c30da59531bb521278edc98f1959009253dda4ee9f63fe5562ead5aa1b",
To: "0x0d3d7a5d1a8fa8e7b8a6d1b9c8c7a9d7e7b8a6d1b9c8c7a9d7e7b8a6d1b9c8c7a9d7e",
Amount: "0.1",
},
},
fields: fields{
myTokenRepo: mocks.NewMyTokenRepository(t),
publisher: mocks.NewPublisher(t),
userClient: mocks.NewUserServiceClient(t),
},
wantErr: status.Errorf(codes.InvalidArgument, "amount is not valid"),
setup: func(fields fields) {
},
},
{
name: "error unable to get user by id",
args: args{
Expand Down

0 comments on commit b056c6d

Please sign in to comment.