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.

150 lines
5.1 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. tokenNameLen := len(tc.tokens[8].Name)
  86. stringName := tc.tokens[8].Name[tokenNameLen-1:]
  87. path = fmt.Sprintf(
  88. "%s?name=%s&limit=%d&fromItem=",
  89. endpoint, stringName, limit,
  90. )
  91. err = doGoodReqPaginated(path, historydb.OrderAsc, &testTokensResponse{}, appendIter)
  92. assert.NoError(t, err)
  93. tokensFiltered = nil
  94. tokensFiltered = append(tokensFiltered, tc.tokens[8])
  95. assertTokensAPIs(t, tokensFiltered, fetchedTokens)
  96. // Multiple filters
  97. fetchedTokens = []historydb.TokenWithUSD{}
  98. limit = 5
  99. stringSymbols = tc.tokens[2].Symbol + "," + tc.tokens[6].Symbol
  100. stringIds = strconv.Itoa(int(tc.tokens[2].TokenID)) + "," + strconv.Itoa(int(tc.tokens[5].TokenID)) + "," + strconv.Itoa(int(tc.tokens[6].TokenID))
  101. path = fmt.Sprintf(
  102. "%s?symbols=%s&ids=%s&limit=%d&fromItem=",
  103. endpoint, stringSymbols, stringIds, limit,
  104. )
  105. err = doGoodReqPaginated(path, historydb.OrderAsc, &testTokensResponse{}, appendIter)
  106. assert.NoError(t, err)
  107. tokensFiltered = nil
  108. tokensFiltered = append(tokensFiltered, tc.tokens[2])
  109. tokensFiltered = append(tokensFiltered, tc.tokens[6])
  110. assertTokensAPIs(t, tokensFiltered, fetchedTokens)
  111. // All, in reverse order
  112. fetchedTokens = []historydb.TokenWithUSD{}
  113. limit = 5
  114. path = fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
  115. err = doGoodReqPaginated(path, historydb.OrderDesc, &testTokensResponse{}, appendIter)
  116. assert.NoError(t, err)
  117. flipedTokens := []historydb.TokenWithUSD{}
  118. for i := 0; i < len(tc.tokens); i++ {
  119. flipedTokens = append(flipedTokens, tc.tokens[len(tc.tokens)-1-i])
  120. }
  121. assertTokensAPIs(t, flipedTokens, fetchedTokens)
  122. }
  123. func assertTokensAPIs(t *testing.T, expected, actual []historydb.TokenWithUSD) {
  124. require.Equal(t, len(expected), len(actual))
  125. for i := 0; i < len(actual); i++ { //nolint len(actual) won't change within the loop
  126. actual[i].ItemID = 0
  127. if expected[i].USDUpdate == nil {
  128. assert.Equal(t, expected[i].USDUpdate, actual[i].USDUpdate)
  129. } else {
  130. assert.Less(t, expected[i].USDUpdate.Unix()-3, actual[i].USDUpdate.Unix())
  131. expected[i].USDUpdate = actual[i].USDUpdate
  132. }
  133. assert.Equal(t, expected[i], actual[i])
  134. }
  135. }