-
Notifications
You must be signed in to change notification settings - Fork 3
112 get account balance api #119
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
astaphobia
merged 11 commits into
zoobc:develop
from
andy-shi88:112-getAccountBalance-api
Aug 6, 2019
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8abb91b
#112 add GetAccountBalanceResponse
andy-shi88 a720d88
#112 register Account Balance rpc service
andy-shi88 5ced901
#112 add handler for account balance
andy-shi88 a81980e
#112 add GetAccountBalanceResponse
andy-shi88 86d82fe
#112 rename service file
andy-shi88 d02bfd2
#112 rename service file
andy-shi88 649b401
#112 add account balance api service implementation and test
andy-shi88 0617a14
Merge branch 'develop' of github.com:zoobc/zoobc-core into 112-getAcc…
andy-shi88 33c032d
#112 fix lint issue
andy-shi88 dd924d2
#112 update schema commit
andy-shi88 105fa39
#112 add unimplemented handler todo comment
andy-shi88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
log "github.com/sirupsen/logrus" | ||
rpc_model "github.com/zoobc/zoobc-core/common/model" | ||
rpc_service "github.com/zoobc/zoobc-core/common/service" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func main() { | ||
conn, err := grpc.Dial(":3001", grpc.WithInsecure()) | ||
if err != nil { | ||
log.Fatalf("did not connect: %s", err) | ||
} | ||
defer conn.Close() | ||
|
||
c := rpc_service.NewAccountBalanceServiceClient(conn) | ||
|
||
response, err := c.GetAccountBalance(context.Background(), &rpc_model.GetAccountBalanceRequest{ | ||
AccountType: 0, | ||
AccountAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN", | ||
}) | ||
|
||
if err != nil { | ||
log.Fatalf("error calling rpc_service.GetAccountBalance: %s", err) | ||
} | ||
|
||
log.Printf("response from remote rpc_service.GetBlockByID(): %s", response) | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/zoobc/zoobc-core/api/service" | ||
"github.com/zoobc/zoobc-core/common/model" | ||
) | ||
|
||
type AccountBalanceHandler struct { | ||
Service service.AccountBalanceServiceInterface | ||
} | ||
|
||
func (abh *AccountBalanceHandler) GetAccountBalance(ctx context.Context, | ||
request *model.GetAccountBalanceRequest) (*model.GetAccountBalanceResponse, error) { | ||
accountBalance, err := abh.Service.GetAccountBalance(request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return accountBalance, nil | ||
} | ||
|
||
func (abh *AccountBalanceHandler) GetAccountBalances(ctx context.Context, | ||
request *model.GetAccountBalancesRequest) (*model.GetAccountBalancesResponse, error) { | ||
// todo: implement this after have filter | ||
return nil, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package service | ||
|
||
import ( | ||
"errors" | ||
"github.com/zoobc/zoobc-core/common/model" | ||
"github.com/zoobc/zoobc-core/common/query" | ||
"github.com/zoobc/zoobc-core/common/util" | ||
) | ||
|
||
type ( | ||
AccountBalanceServiceInterface interface { | ||
GetAccountBalance(request *model.GetAccountBalanceRequest) (*model.GetAccountBalanceResponse, error) | ||
} | ||
|
||
AccountBalanceService struct { | ||
AccountBalanceQuery query.AccountBalanceQueryInterface | ||
Executor query.ExecutorInterface | ||
} | ||
) | ||
|
||
func NewAccountBalanceService(executor query.ExecutorInterface, | ||
accountBalanceQuery query.AccountBalanceQueryInterface) *AccountBalanceService { | ||
return &AccountBalanceService{ | ||
AccountBalanceQuery: accountBalanceQuery, | ||
Executor: executor, | ||
} | ||
} | ||
|
||
func (abs *AccountBalanceService) GetAccountBalance(request *model.GetAccountBalanceRequest) (*model.GetAccountBalanceResponse, error) { | ||
var ( | ||
err error | ||
accountBalances []*model.AccountBalance | ||
) | ||
accountID := util.CreateAccountIDFromAddress(request.AccountType, request.AccountAddress) | ||
accountBalanceQuery, arg := abs.AccountBalanceQuery.GetAccountBalanceByAccountID(accountID) | ||
rows, err := abs.Executor.ExecuteSelect(accountBalanceQuery, arg) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
accountBalances = abs.AccountBalanceQuery.BuildModel(accountBalances, rows) | ||
|
||
if len(accountBalances) == 0 { | ||
return nil, errors.New("error: account not found") | ||
} | ||
|
||
return &model.GetAccountBalanceResponse{ | ||
AccountBalance: accountBalances[0], | ||
}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package service | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/zoobc/zoobc-core/common/model" | ||
"github.com/zoobc/zoobc-core/common/query" | ||
"reflect" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
type ( | ||
// mock GetAccountBalance | ||
mockExecutorGetAccountBalanceFail struct { | ||
query.Executor | ||
} | ||
mockExecutorGetAccountBalanceNotFound struct { | ||
query.Executor | ||
} | ||
mockExecutorGetAccountBalanceSuccess struct { | ||
query.Executor | ||
} | ||
) | ||
|
||
var mockAccountBalanceQuery = query.NewAccountBalanceQuery() | ||
|
||
func (*mockExecutorGetAccountBalanceSuccess) ExecuteSelect(qe string, args ...interface{}) (*sql.Rows, error) { | ||
db, mock, _ := sqlmock.New() | ||
defer db.Close() | ||
mock.ExpectQuery(regexp.QuoteMeta(`SELECT`)).WillReturnRows(sqlmock.NewRows([]string{ | ||
"AccountId", "BlockHeight", "SpendableBalance", "Balance", "PopRevenue", "Latest"}).AddRow( | ||
[]byte{1}, 1, 10000, 10000, 0, 1)) | ||
rows, _ := db.Query(qe) | ||
return rows, nil | ||
} | ||
|
||
func (*mockExecutorGetAccountBalanceFail) ExecuteSelect(qe string, args ...interface{}) (*sql.Rows, error) { | ||
return nil, errors.New("mockError:executeSelectFail") | ||
} | ||
|
||
func (*mockExecutorGetAccountBalanceNotFound) ExecuteSelect(qe string, args ...interface{}) (*sql.Rows, error) { | ||
db, mock, _ := sqlmock.New() | ||
defer db.Close() | ||
mock.ExpectQuery(regexp.QuoteMeta(`SELECT`)).WillReturnRows(sqlmock.NewRows([]string{ | ||
"AccountId", "BlockHeight", "SpendableBalance", "Balance", "PopRevenue", "Latest"})) | ||
rows, _ := db.Query(qe) | ||
return rows, nil | ||
} | ||
|
||
func TestAccountBalanceService_GetAccountBalance(t *testing.T) { | ||
type fields struct { | ||
AccountBalanceQuery query.AccountBalanceQueryInterface | ||
Executor query.ExecutorInterface | ||
} | ||
type args struct { | ||
request *model.GetAccountBalanceRequest | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want *model.GetAccountBalanceResponse | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "GetAccountBalance:fail", | ||
fields: fields{ | ||
AccountBalanceQuery: mockAccountBalanceQuery, | ||
Executor: &mockExecutorGetAccountBalanceFail{}, | ||
}, | ||
args: args{request: &model.GetAccountBalanceRequest{ | ||
AccountType: 0, | ||
AccountAddress: "BCZ000000000000", | ||
}}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "GetAccountBalance:notFound", | ||
fields: fields{ | ||
AccountBalanceQuery: mockAccountBalanceQuery, | ||
Executor: &mockExecutorGetAccountBalanceNotFound{}, | ||
}, | ||
args: args{request: &model.GetAccountBalanceRequest{ | ||
AccountType: 0, | ||
AccountAddress: "BCZ000000000000", | ||
}}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "GetAccountBalance:success", | ||
fields: fields{ | ||
AccountBalanceQuery: mockAccountBalanceQuery, | ||
Executor: &mockExecutorGetAccountBalanceSuccess{}, | ||
}, | ||
args: args{request: &model.GetAccountBalanceRequest{ | ||
AccountType: 0, | ||
AccountAddress: "BCZ000000000000", | ||
}}, | ||
want: &model.GetAccountBalanceResponse{ | ||
AccountBalance: &model.AccountBalance{ | ||
AccountID: []byte{1}, | ||
BlockHeight: 1, | ||
SpendableBalance: 10000, | ||
Balance: 10000, | ||
PopRevenue: 0, | ||
Latest: true, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
abs := &AccountBalanceService{ | ||
AccountBalanceQuery: tt.fields.AccountBalanceQuery, | ||
Executor: tt.fields.Executor, | ||
} | ||
got, err := abs.GetAccountBalance(tt.args.request) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetAccountBalance() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetAccountBalance() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewAccountBalanceService(t *testing.T) { | ||
type args struct { | ||
executor query.ExecutorInterface | ||
accountBalanceQuery query.AccountBalanceQueryInterface | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *AccountBalanceService | ||
}{ | ||
{ | ||
name: "NewAccountBalanceService:success", | ||
args: args{ | ||
executor: nil, | ||
accountBalanceQuery: nil, | ||
}, | ||
want: &AccountBalanceService{ | ||
AccountBalanceQuery: nil, | ||
Executor: nil, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := NewAccountBalanceService(tt.args.executor, tt.args.accountBalanceQuery); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewAccountBalanceService() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be inserted
//TODO
or remove if it still not work yetThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added todo comment