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.

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