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.

87 lines
3.3 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/hermeznetwork/hermez-node/db/historydb"
  6. "github.com/mitchellh/copystructure"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. type testCoordinatorsResponse struct {
  10. Coordinators []historydb.CoordinatorAPI `json:"coordinators"`
  11. PendingItems uint64 `json:"pendingItems"`
  12. }
  13. func (t testCoordinatorsResponse) GetPending() (pendingItems, lastItemID uint64) {
  14. pendingItems = t.PendingItems
  15. lastItemID = t.Coordinators[len(t.Coordinators)-1].ItemID
  16. return pendingItems, lastItemID
  17. }
  18. func (t *testCoordinatorsResponse) Len() int { return len(t.Coordinators) }
  19. func TestGetCoordinators(t *testing.T) {
  20. endpoint := apiURL + "coordinators"
  21. fetchedCoordinators := []historydb.CoordinatorAPI{}
  22. appendIter := func(intr interface{}) {
  23. for i := 0; i < len(intr.(*testCoordinatorsResponse).Coordinators); i++ {
  24. tmp, err := copystructure.Copy(intr.(*testCoordinatorsResponse).Coordinators[i])
  25. if err != nil {
  26. panic(err)
  27. }
  28. fetchedCoordinators = append(fetchedCoordinators, tmp.(historydb.CoordinatorAPI))
  29. }
  30. }
  31. limit := 5
  32. path := fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  33. err := doGoodReqPaginated(path, historydb.OrderAsc, &testCoordinatorsResponse{}, appendIter)
  34. assert.NoError(t, err)
  35. for i := 0; i < len(fetchedCoordinators); i++ {
  36. assert.Equal(t, tc.coordinators[i].ItemID, fetchedCoordinators[i].ItemID)
  37. assert.Equal(t, tc.coordinators[i].Bidder, fetchedCoordinators[i].Bidder)
  38. assert.Equal(t, tc.coordinators[i].Forger, fetchedCoordinators[i].Forger)
  39. assert.Equal(t, tc.coordinators[i].EthBlockNum, fetchedCoordinators[i].EthBlockNum)
  40. assert.Equal(t, tc.coordinators[i].URL, fetchedCoordinators[i].URL)
  41. }
  42. // Reverse Order
  43. reversedCoordinators := []historydb.CoordinatorAPI{}
  44. appendIter = func(intr interface{}) {
  45. for i := 0; i < len(intr.(*testCoordinatorsResponse).Coordinators); i++ {
  46. tmp, err := copystructure.Copy(intr.(*testCoordinatorsResponse).Coordinators[i])
  47. if err != nil {
  48. panic(err)
  49. }
  50. reversedCoordinators = append(reversedCoordinators, tmp.(historydb.CoordinatorAPI))
  51. }
  52. }
  53. err = doGoodReqPaginated(path, historydb.OrderDesc, &testCoordinatorsResponse{}, appendIter)
  54. assert.NoError(t, err)
  55. for i := 0; i < len(fetchedCoordinators); i++ {
  56. assert.Equal(t, reversedCoordinators[i].ItemID, fetchedCoordinators[len(fetchedCoordinators)-1-i].ItemID)
  57. assert.Equal(t, reversedCoordinators[i].Bidder, fetchedCoordinators[len(fetchedCoordinators)-1-i].Bidder)
  58. assert.Equal(t, reversedCoordinators[i].Forger, fetchedCoordinators[len(fetchedCoordinators)-1-i].Forger)
  59. assert.Equal(t, reversedCoordinators[i].EthBlockNum, fetchedCoordinators[len(fetchedCoordinators)-1-i].EthBlockNum)
  60. assert.Equal(t, reversedCoordinators[i].URL, fetchedCoordinators[len(fetchedCoordinators)-1-i].URL)
  61. }
  62. // Test GetCoordinator
  63. path = fmt.Sprintf("%s/%s", endpoint, fetchedCoordinators[2].Forger.String())
  64. coordinator := historydb.CoordinatorAPI{}
  65. assert.NoError(t, doGoodReq("GET", path, nil, &coordinator))
  66. assert.Equal(t, fetchedCoordinators[2], coordinator)
  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. }