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.

93 lines
3.6 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/hermeznetwork/hermez-node/db"
  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. Pagination *db.Pagination `json:"pagination"`
  13. }
  14. func (t *testCoordinatorsResponse) GetPagination() *db.Pagination {
  15. if t.Coordinators[0].ItemID < t.Coordinators[len(t.Coordinators)-1].ItemID {
  16. t.Pagination.FirstReturnedItem = t.Coordinators[0].ItemID
  17. t.Pagination.LastReturnedItem = t.Coordinators[len(t.Coordinators)-1].ItemID
  18. } else {
  19. t.Pagination.LastReturnedItem = t.Coordinators[0].ItemID
  20. t.Pagination.FirstReturnedItem = t.Coordinators[len(t.Coordinators)-1].ItemID
  21. }
  22. return t.Pagination
  23. }
  24. func (t *testCoordinatorsResponse) Len() int { return len(t.Coordinators) }
  25. func TestGetCoordinators(t *testing.T) {
  26. endpoint := apiURL + "coordinators"
  27. fetchedCoordinators := []historydb.CoordinatorAPI{}
  28. appendIter := func(intr interface{}) {
  29. for i := 0; i < len(intr.(*testCoordinatorsResponse).Coordinators); i++ {
  30. tmp, err := copystructure.Copy(intr.(*testCoordinatorsResponse).Coordinators[i])
  31. if err != nil {
  32. panic(err)
  33. }
  34. fetchedCoordinators = append(fetchedCoordinators, tmp.(historydb.CoordinatorAPI))
  35. }
  36. }
  37. limit := 5
  38. path := fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  39. err := doGoodReqPaginated(path, historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter)
  40. assert.NoError(t, err)
  41. for i := 0; i < len(fetchedCoordinators); i++ {
  42. assert.Equal(t, tc.coordinators[i].ItemID, fetchedCoordinators[i].ItemID)
  43. assert.Equal(t, tc.coordinators[i].Bidder, fetchedCoordinators[i].Bidder)
  44. assert.Equal(t, tc.coordinators[i].Forger, fetchedCoordinators[i].Forger)
  45. assert.Equal(t, tc.coordinators[i].EthBlockNum, fetchedCoordinators[i].EthBlockNum)
  46. assert.Equal(t, tc.coordinators[i].URL, fetchedCoordinators[i].URL)
  47. }
  48. // Reverse Order
  49. reversedCoordinators := []historydb.CoordinatorAPI{}
  50. appendIter = func(intr interface{}) {
  51. for i := 0; i < len(intr.(*testCoordinatorsResponse).Coordinators); i++ {
  52. tmp, err := copystructure.Copy(intr.(*testCoordinatorsResponse).Coordinators[i])
  53. if err != nil {
  54. panic(err)
  55. }
  56. reversedCoordinators = append(reversedCoordinators, tmp.(historydb.CoordinatorAPI))
  57. }
  58. }
  59. err = doGoodReqPaginated(path, historydb.OrderDesc, &testCoordinatorsResponse{}, appendIter)
  60. assert.NoError(t, err)
  61. for i := 0; i < len(fetchedCoordinators); i++ {
  62. assert.Equal(t, reversedCoordinators[i].ItemID, fetchedCoordinators[len(fetchedCoordinators)-1-i].ItemID)
  63. assert.Equal(t, reversedCoordinators[i].Bidder, fetchedCoordinators[len(fetchedCoordinators)-1-i].Bidder)
  64. assert.Equal(t, reversedCoordinators[i].Forger, fetchedCoordinators[len(fetchedCoordinators)-1-i].Forger)
  65. assert.Equal(t, reversedCoordinators[i].EthBlockNum, fetchedCoordinators[len(fetchedCoordinators)-1-i].EthBlockNum)
  66. assert.Equal(t, reversedCoordinators[i].URL, fetchedCoordinators[len(fetchedCoordinators)-1-i].URL)
  67. }
  68. // Test GetCoordinator
  69. path = fmt.Sprintf("%s/%s", endpoint, fetchedCoordinators[2].Forger.String())
  70. coordinator := historydb.CoordinatorAPI{}
  71. assert.NoError(t, doGoodReq("GET", path, nil, &coordinator))
  72. assert.Equal(t, fetchedCoordinators[2], coordinator)
  73. // 400
  74. path = fmt.Sprintf("%s/0x001", endpoint)
  75. err = doBadReq("GET", path, nil, 400)
  76. assert.NoError(t, err)
  77. // 404
  78. path = fmt.Sprintf("%s/0xaa942cfcd25ad4d90a62358b0dd84f33b398262a", endpoint)
  79. err = doBadReq("GET", path, nil, 404)
  80. assert.NoError(t, err)
  81. }