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.

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