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.

147 lines
4.9 KiB

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