You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.7 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/hermeznetwork/hermez-node/db/historydb"
  7. "github.com/mitchellh/copystructure"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. type testCoordinatorsResponse struct {
  11. Coordinators []historydb.CoordinatorAPI `json:"coordinators"`
  12. PendingItems uint64 `json:"pendingItems"`
  13. }
  14. func (t testCoordinatorsResponse) GetPending() (pendingItems, lastItemID uint64) {
  15. pendingItems = t.PendingItems
  16. lastItemID = t.Coordinators[len(t.Coordinators)-1].ItemID
  17. return pendingItems, lastItemID
  18. }
  19. func (t *testCoordinatorsResponse) Len() int { return len(t.Coordinators) }
  20. func (t testCoordinatorsResponse) New() Pendinger { return &testCoordinatorsResponse{} }
  21. func genTestCoordinators(coordinators []common.Coordinator) []historydb.CoordinatorAPI {
  22. testCoords := []historydb.CoordinatorAPI{}
  23. for i := 0; i < len(coordinators); i++ {
  24. testCoords = append(testCoords, historydb.CoordinatorAPI{
  25. Bidder: coordinators[i].Bidder,
  26. Forger: coordinators[i].Forger,
  27. EthBlockNum: coordinators[i].EthBlockNum,
  28. URL: coordinators[i].URL,
  29. })
  30. }
  31. return testCoords
  32. }
  33. func TestGetCoordinators(t *testing.T) {
  34. endpoint := apiURL + "coordinators"
  35. fetchedCoordinators := []historydb.CoordinatorAPI{}
  36. appendIter := func(intr interface{}) {
  37. for i := 0; i < len(intr.(*testCoordinatorsResponse).Coordinators); i++ {
  38. tmp, err := copystructure.Copy(intr.(*testCoordinatorsResponse).Coordinators[i])
  39. if err != nil {
  40. panic(err)
  41. }
  42. fetchedCoordinators = append(fetchedCoordinators, tmp.(historydb.CoordinatorAPI))
  43. }
  44. }
  45. // All
  46. limit := 5
  47. path := fmt.Sprintf("%s?limit=%d", endpoint, limit)
  48. err := doGoodReqPaginated(path, historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter)
  49. assert.NoError(t, err)
  50. assertCoordinators(t, tc.coordinators, fetchedCoordinators)
  51. // All in reverse order
  52. fetchedCoordinators = []historydb.CoordinatorAPI{}
  53. err = doGoodReqPaginated(path, historydb.OrderDesc, &testCoordinatorsResponse{}, appendIter)
  54. assert.NoError(t, err)
  55. reversedCoordinators := []historydb.CoordinatorAPI{}
  56. for i := 0; i < len(tc.coordinators); i++ {
  57. reversedCoordinators = append(reversedCoordinators, tc.coordinators[len(tc.coordinators)-1-i])
  58. }
  59. assertCoordinators(t, reversedCoordinators, fetchedCoordinators)
  60. for _, filteredCoord := range tc.coordinators {
  61. // By bidder
  62. fetchedCoordinators = []historydb.CoordinatorAPI{}
  63. err = doGoodReqPaginated(
  64. fmt.Sprintf(path+"&bidderAddr=%s", filteredCoord.Bidder.String()),
  65. historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter,
  66. )
  67. assert.NoError(t, err)
  68. assertCoordinators(t, []historydb.CoordinatorAPI{filteredCoord}, fetchedCoordinators)
  69. // By forger
  70. fetchedCoordinators = []historydb.CoordinatorAPI{}
  71. err = doGoodReqPaginated(
  72. fmt.Sprintf(path+"&forgerAddr=%s", filteredCoord.Forger.String()),
  73. historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter,
  74. )
  75. assert.NoError(t, err)
  76. assertCoordinators(t, []historydb.CoordinatorAPI{filteredCoord}, fetchedCoordinators)
  77. }
  78. // 400
  79. path = fmt.Sprintf("%s?bidderAddr=0x001", endpoint)
  80. err = doBadReq("GET", path, nil, 400)
  81. assert.NoError(t, err)
  82. // 404
  83. path = fmt.Sprintf("%s?bidderAddr=0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint)
  84. err = doBadReq("GET", path, nil, 404)
  85. assert.NoError(t, err)
  86. }
  87. func assertCoordinator(t *testing.T, expected, actual historydb.CoordinatorAPI) {
  88. actual.ItemID = 0
  89. assert.Equal(t, expected, actual)
  90. }
  91. func assertCoordinators(t *testing.T, expected, actual []historydb.CoordinatorAPI) {
  92. assert.Equal(t, len(expected), len(actual))
  93. for i := 0; i < len(expected); i++ {
  94. assertCoordinator(t, expected[i], actual[i])
  95. }
  96. }