Browse Source

Merge pull request #440 from hermeznetwork/fix/api-empty-arrays

empty array instead of 404
feature/sql-semaphore1
Eduard S 3 years ago
committed by GitHub
parent
commit
fedbacaade
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 78 deletions
  1. +10
    -4
      api/batch_test.go
  2. +10
    -4
      api/bids_test.go
  3. +10
    -5
      api/coordinator_test.go
  4. +9
    -7
      api/exits_test.go
  5. +0
    -42
      api/swagger.yml
  6. +9
    -7
      api/txshistory_test.go
  7. +8
    -9
      db/historydb/historydb.go

+ 10
- 4
api/batch_test.go

@ -35,6 +35,9 @@ type testBatchesResponse struct {
}
func (t testBatchesResponse) GetPending() (pendingItems, lastItemID uint64) {
if len(t.Batches) == 0 {
return 0, 0
}
pendingItems = t.PendingItems
lastItemID = t.Batches[len(t.Batches)-1].ItemID
return pendingItems, lastItemID
@ -220,6 +223,13 @@ func TestGetBatches(t *testing.T) {
}
assertBatches(t, minMaxBatchNumBatches, fetchedBatches)
// Empty array
fetchedBatches = []testBatch{}
path = fmt.Sprintf("%s?slotNum=%d&minBatchNum=%d", endpoint, 1, 25)
err = doGoodReqPaginated(path, historydb.OrderAsc, &testBatchesResponse{}, appendIter)
assert.NoError(t, err)
assertBatches(t, []testBatch{}, fetchedBatches)
// 400
// Invalid minBatchNum
path = fmt.Sprintf("%s?minBatchNum=%d", endpoint, -2)
@ -229,10 +239,6 @@ func TestGetBatches(t *testing.T) {
path = fmt.Sprintf("%s?forgerAddr=%s", endpoint, "0xG0000001")
err = doBadReq("GET", path, nil, 400)
assert.NoError(t, err)
// 404
path = fmt.Sprintf("%s?slotNum=%d&minBatchNum=%d", endpoint, 1, 25)
err = doBadReq("GET", path, nil, 404)
assert.NoError(t, err)
}
func TestGetBatch(t *testing.T) {

+ 10
- 4
api/bids_test.go

@ -29,6 +29,9 @@ type testBidsResponse struct {
}
func (t testBidsResponse) GetPending() (pendingItems, lastItemID uint64) {
if len(t.Bids) == 0 {
return 0, 0
}
pendingItems = t.PendingItems
lastItemID = t.Bids[len(t.Bids)-1].ItemID
return pendingItems, lastItemID
@ -127,6 +130,13 @@ func TestGetBids(t *testing.T) {
}
assertBids(t, slotNumBidderAddrBids, fetchedBids)
// Empty array
fetchedBids = []testBid{}
path = fmt.Sprintf("%s?slotNum=%d&bidderAddr=%s", endpoint, 5, tc.bids[1].Bidder.String())
err = doGoodReqPaginated(path, historydb.OrderAsc, &testBidsResponse{}, appendIter)
assert.NoError(t, err)
assertBids(t, []testBid{}, fetchedBids)
// 400
// No filters
path = fmt.Sprintf("%s?limit=%d", endpoint, limit)
@ -140,10 +150,6 @@ func TestGetBids(t *testing.T) {
path = fmt.Sprintf("%s?bidderAddr=%s", endpoint, "0xG0000001")
err = doBadReq("GET", path, nil, 400)
assert.NoError(t, err)
// 404
path = fmt.Sprintf("%s?slotNum=%d&bidderAddr=%s", endpoint, 5, tc.bids[1].Bidder.String())
err = doBadReq("GET", path, nil, 404)
assert.NoError(t, err)
}
func assertBids(t *testing.T, expected, actual []testBid) {

+ 10
- 5
api/coordinator_test.go

@ -16,6 +16,9 @@ type testCoordinatorsResponse struct {
}
func (t testCoordinatorsResponse) GetPending() (pendingItems, lastItemID uint64) {
if len(t.Coordinators) == 0 {
return 0, 0
}
pendingItems = t.PendingItems
lastItemID = t.Coordinators[len(t.Coordinators)-1].ItemID
return pendingItems, lastItemID
@ -67,7 +70,6 @@ func TestGetCoordinators(t *testing.T) {
reversedCoordinators = append(reversedCoordinators, tc.coordinators[len(tc.coordinators)-1-i])
}
assertCoordinators(t, reversedCoordinators, fetchedCoordinators)
for _, filteredCoord := range tc.coordinators {
// By bidder
fetchedCoordinators = []historydb.CoordinatorAPI{}
@ -87,14 +89,17 @@ func TestGetCoordinators(t *testing.T) {
assertCoordinators(t, []historydb.CoordinatorAPI{filteredCoord}, fetchedCoordinators)
}
// Empty array
fetchedCoordinators = []historydb.CoordinatorAPI{}
path = fmt.Sprintf("%s?bidderAddr=0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint)
err = doGoodReqPaginated(path, historydb.OrderDesc, &testCoordinatorsResponse{}, appendIter)
assert.NoError(t, err)
assertCoordinators(t, []historydb.CoordinatorAPI{}, fetchedCoordinators)
// 400
path = fmt.Sprintf("%s?bidderAddr=0x001", endpoint)
err = doBadReq("GET", path, nil, 400)
assert.NoError(t, err)
// 404
path = fmt.Sprintf("%s?bidderAddr=0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint)
err = doBadReq("GET", path, nil, 404)
assert.NoError(t, err)
}
func assertCoordinator(t *testing.T, expected, actual historydb.CoordinatorAPI) {

+ 9
- 7
api/exits_test.go

@ -43,6 +43,9 @@ type testExitsResponse struct {
}
func (t testExitsResponse) GetPending() (pendingItems, lastItemID uint64) {
if len(t.Exits) == 0 {
return 0, 0
}
pendingItems = t.PendingItems
lastItemID = t.Exits[len(t.Exits)-1].ItemID
return pendingItems, lastItemID
@ -248,6 +251,12 @@ func TestGetExits(t *testing.T) {
err = doGoodReqPaginated(path, historydb.OrderDesc, &testExitsResponse{}, appendIter)
assert.NoError(t, err)
assertExitAPIs(t, flipedExits, fetchedExits)
// Empty array
fetchedExits = []testExit{}
path = fmt.Sprintf("%s?batchNum=999999", endpoint)
err = doGoodReqPaginated(path, historydb.OrderDesc, &testExitsResponse{}, appendIter)
assert.NoError(t, err)
assertExitAPIs(t, []testExit{}, fetchedExits)
// 400
path = fmt.Sprintf(
"%s?accountIndex=%s&hezEthereumAddress=%s",
@ -258,13 +267,6 @@ func TestGetExits(t *testing.T) {
path = fmt.Sprintf("%s?tokenId=X", endpoint)
err = doBadReq("GET", path, nil, 400)
assert.NoError(t, err)
// 404
path = fmt.Sprintf("%s?batchNum=999999", endpoint)
err = doBadReq("GET", path, nil, 404)
assert.NoError(t, err)
path = fmt.Sprintf("%s?fromItem=1000999999", endpoint)
err = doBadReq("GET", path, nil, 404)
assert.NoError(t, err)
}
func TestGetExit(t *testing.T) {

+ 0
- 42
api/swagger.yml

@ -215,12 +215,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error400'
'404':
description: Not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error404'
'500':
description: Internal server error.
content:
@ -347,12 +341,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error400'
'404':
description: Not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error404'
'500':
description: Internal server error.
content:
@ -565,12 +553,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error400'
'404':
description: Not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error404'
'500':
description: Internal server error.
content:
@ -687,12 +669,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error400'
'404':
description: Not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error404'
'500':
description: Internal server error.
content:
@ -850,12 +826,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error400'
'404':
description: Not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error404'
'500':
description: Internal server error.
content:
@ -960,12 +930,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error400'
'404':
description: Not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error404'
'500':
description: Internal server error.
content:
@ -1085,12 +1049,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error400'
'404':
description: Not found.
content:
application/json:
schema:
$ref: '#/components/schemas/Error404'
'500':
description: Internal server error.
content:

+ 9
- 7
api/txshistory_test.go

@ -88,6 +88,9 @@ type testTxsResponse struct {
}
func (t testTxsResponse) GetPending() (pendingItems, lastItemID uint64) {
if len(t.Txs) == 0 {
return 0, 0
}
pendingItems = t.PendingItems
lastItemID = t.Txs[len(t.Txs)-1].ItemID
return pendingItems, lastItemID
@ -420,6 +423,12 @@ func TestGetHistoryTxs(t *testing.T) {
flipedTxs = append(flipedTxs, tc.txs[len(tc.txs)-1-i])
}
assertTxs(t, flipedTxs, fetchedTxs)
// Empty array
fetchedTxs = []testTx{}
path = fmt.Sprintf("%s?batchNum=999999", endpoint)
err = doGoodReqPaginated(path, historydb.OrderDesc, &testTxsResponse{}, appendIter)
assert.NoError(t, err)
assertTxs(t, []testTx{}, fetchedTxs)
// 400
path = fmt.Sprintf(
"%s?accountIndex=%s&hezEthereumAddress=%s",
@ -430,13 +439,6 @@ func TestGetHistoryTxs(t *testing.T) {
path = fmt.Sprintf("%s?tokenId=X", endpoint)
err = doBadReq("GET", path, nil, 400)
assert.NoError(t, err)
// 404
path = fmt.Sprintf("%s?batchNum=999999", endpoint)
err = doBadReq("GET", path, nil, 404)
assert.NoError(t, err)
path = fmt.Sprintf("%s?fromItem=1000999999", endpoint)
err = doBadReq("GET", path, nil, 404)
assert.NoError(t, err)
}
func TestGetHistoryTx(t *testing.T) {

+ 8
- 9
db/historydb/historydb.go

@ -1,7 +1,6 @@
package historydb
import (
"database/sql"
"errors"
"fmt"
"math"
@ -265,7 +264,7 @@ func (hdb *HistoryDB) GetBatchesAPI(
}
batches := db.SlicePtrsToSlice(batchPtrs).([]BatchAPI)
if len(batches) == 0 {
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
return batches, 0, nil
}
return batches, batches[0].TotalItems - uint64(len(batches)), nil
}
@ -441,7 +440,7 @@ func (hdb *HistoryDB) GetBestBidsAPI(
// log.Debug(query)
bids := db.SlicePtrsToSlice(bidPtrs).([]BidAPI)
if len(bids) == 0 {
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
return bids, 0, nil
}
return bids, bids[0].TotalItems - uint64(len(bids)), nil
}
@ -512,7 +511,7 @@ func (hdb *HistoryDB) GetBidsAPI(
return nil, 0, tracerr.Wrap(err)
}
if len(bids) == 0 {
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
return []BidAPI{}, 0, nil
}
return db.SlicePtrsToSlice(bids).([]BidAPI), bids[0].TotalItems - uint64(len(bids)), nil
}
@ -739,7 +738,7 @@ func (hdb *HistoryDB) GetTokens(
return nil, 0, tracerr.Wrap(err)
}
if len(tokens) == 0 {
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
return []TokenWithUSD{}, 0, nil
}
return db.SlicePtrsToSlice(tokens).([]TokenWithUSD), uint64(len(tokens)) - tokens[0].TotalItems, nil
}
@ -1056,7 +1055,7 @@ func (hdb *HistoryDB) GetHistoryTxs(
}
txs := db.SlicePtrsToSlice(txsPtrs).([]TxAPI)
if len(txs) == 0 {
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
return txs, 0, nil
}
return txs, txs[0].TotalItems - uint64(len(txs)), nil
}
@ -1199,7 +1198,7 @@ func (hdb *HistoryDB) GetExitsAPI(
return nil, 0, tracerr.Wrap(err)
}
if len(exits) == 0 {
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
return []ExitAPI{}, 0, nil
}
return db.SlicePtrsToSlice(exits).([]ExitAPI), exits[0].TotalItems - uint64(len(exits)), nil
}
@ -1688,7 +1687,7 @@ func (hdb *HistoryDB) GetCoordinatorsAPI(
return nil, 0, tracerr.Wrap(err)
}
if len(coordinators) == 0 {
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
return []CoordinatorAPI{}, 0, nil
}
return db.SlicePtrsToSlice(coordinators).([]CoordinatorAPI),
coordinators[0].TotalItems - uint64(len(coordinators)), nil
@ -1811,7 +1810,7 @@ func (hdb *HistoryDB) GetAccountsAPI(
return nil, 0, tracerr.Wrap(err)
}
if len(accounts) == 0 {
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
return []AccountAPI{}, 0, nil
}
return db.SlicePtrsToSlice(accounts).([]AccountAPI),

Loading…
Cancel
Save