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.

153 lines
5.2 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "github.com/hermeznetwork/hermez-node/db"
  7. "github.com/hermeznetwork/hermez-node/db/historydb"
  8. "github.com/mitchellh/copystructure"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. type testTokensResponse struct {
  13. Tokens []historydb.TokenWithUSD `json:"tokens"`
  14. Pagination *db.Pagination `json:"pagination"`
  15. }
  16. func (t *testTokensResponse) GetPagination() *db.Pagination {
  17. if t.Tokens[0].ItemID < t.Tokens[len(t.Tokens)-1].ItemID {
  18. t.Pagination.FirstReturnedItem = t.Tokens[0].ItemID
  19. t.Pagination.LastReturnedItem = t.Tokens[len(t.Tokens)-1].ItemID
  20. } else {
  21. t.Pagination.LastReturnedItem = t.Tokens[0].ItemID
  22. t.Pagination.FirstReturnedItem = t.Tokens[len(t.Tokens)-1].ItemID
  23. }
  24. return t.Pagination
  25. }
  26. func (t *testTokensResponse) Len() int {
  27. return len(t.Tokens)
  28. }
  29. func TestGetToken(t *testing.T) {
  30. // Get all txs by their ID
  31. endpoint := apiURL + "tokens/"
  32. fetchedTokens := []historydb.TokenWithUSD{}
  33. for _, token := range tc.tokens {
  34. fetchedToken := historydb.TokenWithUSD{}
  35. assert.NoError(t, doGoodReq("GET", endpoint+strconv.Itoa(int(token.TokenID)), nil, &fetchedToken))
  36. fetchedTokens = append(fetchedTokens, fetchedToken)
  37. }
  38. assertTokensAPIs(t, tc.tokens, fetchedTokens)
  39. }
  40. func TestGetTokens(t *testing.T) {
  41. endpoint := apiURL + "tokens"
  42. fetchedTokens := []historydb.TokenWithUSD{}
  43. appendIter := func(intr interface{}) {
  44. for i := 0; i < len(intr.(*testTokensResponse).Tokens); i++ {
  45. tmp, err := copystructure.Copy(intr.(*testTokensResponse).Tokens[i])
  46. if err != nil {
  47. panic(err)
  48. }
  49. fetchedTokens = append(fetchedTokens, tmp.(historydb.TokenWithUSD))
  50. }
  51. }
  52. // Get all (no filters)
  53. limit := 8
  54. path := fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  55. err := doGoodReqPaginated(path, historydb.OrderAsc, &testTokensResponse{}, appendIter)
  56. assert.NoError(t, err)
  57. assertTokensAPIs(t, tc.tokens, fetchedTokens)
  58. // Get by tokenIds
  59. fetchedTokens = []historydb.TokenWithUSD{}
  60. limit = 7
  61. stringIds := strconv.Itoa(int(tc.tokens[2].TokenID)) + "," + strconv.Itoa(int(tc.tokens[5].TokenID)) + "," + strconv.Itoa(int(tc.tokens[6].TokenID))
  62. path = fmt.Sprintf(
  63. "%s?ids=%s&limit=%d&fromItem=",
  64. endpoint, stringIds, limit,
  65. )
  66. err = doGoodReqPaginated(path, historydb.OrderAsc, &testTokensResponse{}, appendIter)
  67. assert.NoError(t, err)
  68. var tokensFiltered []historydb.TokenWithUSD
  69. tokensFiltered = append(tokensFiltered, tc.tokens[2])
  70. tokensFiltered = append(tokensFiltered, tc.tokens[5])
  71. tokensFiltered = append(tokensFiltered, tc.tokens[6])
  72. assertTokensAPIs(t, tokensFiltered, fetchedTokens)
  73. // Get by symbols
  74. fetchedTokens = []historydb.TokenWithUSD{}
  75. limit = 7
  76. stringSymbols := tc.tokens[1].Symbol + "," + tc.tokens[3].Symbol
  77. path = fmt.Sprintf(
  78. "%s?symbols=%s&limit=%d&fromItem=",
  79. endpoint, stringSymbols, limit,
  80. )
  81. err = doGoodReqPaginated(path, historydb.OrderAsc, &testTokensResponse{}, appendIter)
  82. assert.NoError(t, err)
  83. tokensFiltered = nil
  84. tokensFiltered = append(tokensFiltered, tc.tokens[1])
  85. tokensFiltered = append(tokensFiltered, tc.tokens[3])
  86. assertTokensAPIs(t, tokensFiltered, fetchedTokens)
  87. // Get by name
  88. fetchedTokens = []historydb.TokenWithUSD{}
  89. limit = 5
  90. stringName := tc.tokens[8].Name[4:5]
  91. path = fmt.Sprintf(
  92. "%s?name=%s&limit=%d&fromItem=",
  93. endpoint, stringName, limit,
  94. )
  95. err = doGoodReqPaginated(path, historydb.OrderAsc, &testTokensResponse{}, appendIter)
  96. assert.NoError(t, err)
  97. tokensFiltered = nil
  98. tokensFiltered = append(tokensFiltered, tc.tokens[8])
  99. assertTokensAPIs(t, tokensFiltered, fetchedTokens)
  100. // Multiple filters
  101. fetchedTokens = []historydb.TokenWithUSD{}
  102. limit = 5
  103. stringSymbols = tc.tokens[2].Symbol + "," + tc.tokens[6].Symbol
  104. stringIds = strconv.Itoa(int(tc.tokens[2].TokenID)) + "," + strconv.Itoa(int(tc.tokens[5].TokenID)) + "," + strconv.Itoa(int(tc.tokens[6].TokenID))
  105. path = fmt.Sprintf(
  106. "%s?symbols=%s&ids=%s&limit=%d&fromItem=",
  107. endpoint, stringSymbols, stringIds, limit,
  108. )
  109. err = doGoodReqPaginated(path, historydb.OrderAsc, &testTokensResponse{}, appendIter)
  110. assert.NoError(t, err)
  111. tokensFiltered = nil
  112. tokensFiltered = append(tokensFiltered, tc.tokens[2])
  113. tokensFiltered = append(tokensFiltered, tc.tokens[6])
  114. assertTokensAPIs(t, tokensFiltered, fetchedTokens)
  115. // All, in reverse order
  116. fetchedTokens = []historydb.TokenWithUSD{}
  117. limit = 5
  118. path = fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  119. err = doGoodReqPaginated(path, historydb.OrderDesc, &testTokensResponse{}, appendIter)
  120. assert.NoError(t, err)
  121. flipedTokens := []historydb.TokenWithUSD{}
  122. for i := 0; i < len(tc.tokens); i++ {
  123. flipedTokens = append(flipedTokens, tc.tokens[len(tc.tokens)-1-i])
  124. }
  125. assertTokensAPIs(t, flipedTokens, fetchedTokens)
  126. }
  127. func assertTokensAPIs(t *testing.T, expected, actual []historydb.TokenWithUSD) {
  128. require.Equal(t, len(expected), len(actual))
  129. for i := 0; i < len(actual); i++ { //nolint len(actual) won't change within the loop
  130. actual[i].ItemID = 0
  131. if expected[i].USDUpdate == nil {
  132. assert.Equal(t, expected[i].USDUpdate, actual[i].USDUpdate)
  133. } else {
  134. assert.Less(t, expected[i].USDUpdate.Unix()-3, actual[i].USDUpdate.Unix())
  135. expected[i].USDUpdate = actual[i].USDUpdate
  136. }
  137. assert.Equal(t, expected[i], actual[i])
  138. }
  139. }