Fix account_service

This commit is contained in:
Patrick O'Grady 2020-11-13 11:42:33 -08:00
parent be42005441
commit d9fb294826
No known key found for this signature in database
GPG key ID: 8DE11C985C0C8D85
2 changed files with 74 additions and 62 deletions

View file

@ -51,34 +51,6 @@ func (s *AccountAPIService) AccountBalance(
// TODO: filter balances by request currencies // TODO: filter balances by request currencies
// If we are fetching the current balance,
// return all coins for an address and calculate
// the balance from those coins.
if request.BlockIdentifier == nil {
coins, block, err := s.i.GetCoins(ctx, request.AccountIdentifier)
if err != nil {
return nil, wrapErr(ErrUnableToGetCoins, err)
}
balance := "0"
for _, coin := range coins {
balance, err = types.AddValues(balance, coin.Amount.Value)
if err != nil {
return nil, wrapErr(ErrUnableToParseIntermediateResult, err)
}
}
return &types.AccountBalanceResponse{
BlockIdentifier: block,
Balances: []*types.Amount{
{
Value: balance,
Currency: s.config.Currency,
},
},
}, nil
}
// If we are fetching a historical balance, // If we are fetching a historical balance,
// use balance storage and don't return coins. // use balance storage and don't return coins.
amount, block, err := s.i.GetBalance( amount, block, err := s.i.GetBalance(

View file

@ -38,6 +38,10 @@ func TestAccountBalance_Offline(t *testing.T) {
assert.Nil(t, bal) assert.Nil(t, bal)
assert.Equal(t, ErrUnavailableOffline.Code, err.Code) assert.Equal(t, ErrUnavailableOffline.Code, err.Code)
coins, err := servicer.AccountCoins(ctx, &types.AccountCoinsRequest{})
assert.Nil(t, coins)
assert.Equal(t, ErrUnavailableOffline.Code, err.Code)
mockIndexer.AssertExpectations(t) mockIndexer.AssertExpectations(t)
} }
@ -49,55 +53,33 @@ func TestAccountBalance_Online_Current(t *testing.T) {
mockIndexer := &mocks.Indexer{} mockIndexer := &mocks.Indexer{}
servicer := NewAccountAPIService(cfg, mockIndexer) servicer := NewAccountAPIService(cfg, mockIndexer)
ctx := context.Background() ctx := context.Background()
account := &types.AccountIdentifier{ account := &types.AccountIdentifier{
Address: "hello", Address: "hello",
} }
coins := []*types.Coin{
{
Amount: &types.Amount{
Value: "10",
},
CoinIdentifier: &types.CoinIdentifier{
Identifier: "coin 1",
},
},
{
Amount: &types.Amount{
Value: "15",
},
CoinIdentifier: &types.CoinIdentifier{
Identifier: "coin 2",
},
},
{
Amount: &types.Amount{
Value: "0",
},
CoinIdentifier: &types.CoinIdentifier{
Identifier: "coin 3",
},
},
}
block := &types.BlockIdentifier{ block := &types.BlockIdentifier{
Index: 1000, Index: 1000,
Hash: "block 1000", Hash: "block 1000",
} }
mockIndexer.On("GetCoins", ctx, account).Return(coins, block, nil).Once() amount := &types.Amount{
Value: "25",
Currency: bitcoin.MainnetCurrency,
}
mockIndexer.On(
"GetBalance",
ctx,
account,
bitcoin.MainnetCurrency,
(*types.PartialBlockIdentifier)(nil),
).Return(amount, block, nil).Once()
bal, err := servicer.AccountBalance(ctx, &types.AccountBalanceRequest{ bal, err := servicer.AccountBalance(ctx, &types.AccountBalanceRequest{
AccountIdentifier: account, AccountIdentifier: account,
}) })
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, &types.AccountBalanceResponse{ assert.Equal(t, &types.AccountBalanceResponse{
BlockIdentifier: block, BlockIdentifier: block,
Balances: []*types.Amount{ Balances: []*types.Amount{
{ amount,
Value: "25",
Currency: bitcoin.MainnetCurrency,
},
}, },
}, bal) }, bal)
@ -148,3 +130,61 @@ func TestAccountBalance_Online_Historical(t *testing.T) {
mockIndexer.AssertExpectations(t) mockIndexer.AssertExpectations(t)
} }
func TestAccountCoins_Online(t *testing.T) {
cfg := &configuration.Configuration{
Mode: configuration.Online,
Currency: bitcoin.MainnetCurrency,
}
mockIndexer := &mocks.Indexer{}
servicer := NewAccountAPIService(cfg, mockIndexer)
ctx := context.Background()
account := &types.AccountIdentifier{
Address: "hello",
}
coins := []*types.Coin{
{
Amount: &types.Amount{
Value: "10",
},
CoinIdentifier: &types.CoinIdentifier{
Identifier: "coin 1",
},
},
{
Amount: &types.Amount{
Value: "15",
},
CoinIdentifier: &types.CoinIdentifier{
Identifier: "coin 2",
},
},
{
Amount: &types.Amount{
Value: "0",
},
CoinIdentifier: &types.CoinIdentifier{
Identifier: "coin 3",
},
},
}
block := &types.BlockIdentifier{
Index: 1000,
Hash: "block 1000",
}
mockIndexer.On("GetCoins", ctx, account).Return(coins, block, nil).Once()
bal, err := servicer.AccountCoins(ctx, &types.AccountCoinsRequest{
AccountIdentifier: account,
})
assert.Nil(t, err)
assert.Equal(t, &types.AccountCoinsResponse{
BlockIdentifier: block,
Coins: coins,
}, bal)
mockIndexer.AssertExpectations(t)
}