Imp api get full-batches

This commit is contained in:
Arnau B
2020-10-30 16:26:33 +01:00
parent 8c15a16dea
commit e0e3299d9f
4 changed files with 70 additions and 19 deletions

View File

@@ -49,7 +49,16 @@ func (t testBatchesResponse) Len() int {
return len(t.Batches)
}
func genTestBatches(blocks []common.Block, cBatches []common.Batch) []testBatch {
type testFullBatch struct {
Batch testBatch `json:"batch"`
Txs []testTx `json:"transactions"`
}
func genTestBatches(
blocks []common.Block,
cBatches []common.Batch,
txs []testTx,
) ([]testBatch, []testFullBatch) {
tBatches := []testBatch{}
for _, cBatch := range cBatches {
block := common.Block{}
@@ -84,7 +93,20 @@ func genTestBatches(blocks []common.Block, cBatches []common.Batch) []testBatch
}
tBatches = append(tBatches, tBatch)
}
return tBatches
fullBatches := []testFullBatch{}
for i := 0; i < len(tBatches); i++ {
forgedTxs := []testTx{}
for j := 0; j < len(txs); j++ {
if txs[j].BatchNum != nil && *txs[j].BatchNum == tBatches[i].BatchNum {
forgedTxs = append(forgedTxs, txs[j])
}
}
fullBatches = append(fullBatches, testFullBatch{
Batch: tBatches[i],
Txs: forgedTxs,
})
}
return tBatches, fullBatches
}
func TestGetBatches(t *testing.T) {
@@ -228,6 +250,26 @@ func TestGetBatch(t *testing.T) {
assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404))
}
func TestGetFullBatch(t *testing.T) {
endpoint := apiURL + "full-batches/"
for _, fullBatch := range tc.fullBatches {
fetchedFullBatch := testFullBatch{}
assert.NoError(
t, doGoodReq(
"GET",
endpoint+strconv.Itoa(int(fullBatch.Batch.BatchNum)),
nil, &fetchedFullBatch,
),
)
assertBatch(t, fullBatch.Batch, fetchedFullBatch.Batch)
assertTxs(t, fullBatch.Txs, fetchedFullBatch.Txs)
}
// 400
assert.NoError(t, doBadReq("GET", endpoint+"foo", nil, 400))
// 404
assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404))
}
func assertBatches(t *testing.T, expected, actual []testBatch) {
assert.Equal(t, len(expected), len(actual))
for i := 0; i < len(expected); i++ {