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.

99 lines
3.2 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&fromItem=", 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. // Test GetCoordinator
  61. for _, coord := range tc.coordinators {
  62. path = fmt.Sprintf("%s/%s", endpoint, coord.Forger.String())
  63. fetchedCoordinator := historydb.CoordinatorAPI{}
  64. assert.NoError(t, doGoodReq("GET", path, nil, &fetchedCoordinator))
  65. assertCoordinator(t, coord, fetchedCoordinator)
  66. }
  67. // 400
  68. path = fmt.Sprintf("%s/0x001", endpoint)
  69. err = doBadReq("GET", path, nil, 400)
  70. assert.NoError(t, err)
  71. // 404
  72. path = fmt.Sprintf("%s/0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint)
  73. err = doBadReq("GET", path, nil, 404)
  74. assert.NoError(t, err)
  75. }
  76. func assertCoordinator(t *testing.T, expected, actual historydb.CoordinatorAPI) {
  77. actual.ItemID = 0
  78. assert.Equal(t, expected, actual)
  79. }
  80. func assertCoordinators(t *testing.T, expected, actual []historydb.CoordinatorAPI) {
  81. assert.Equal(t, len(expected), len(actual))
  82. for i := 0; i < len(expected); i++ {
  83. assertCoordinator(t, expected[i], actual[i])
  84. }
  85. }