Skip to content

Commit

Permalink
fix: support for coincap symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Jun 1, 2024
1 parent 7efe139 commit 53ec475
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 70 deletions.
33 changes: 4 additions & 29 deletions internal/quote/coincap/coincap_test.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,22 @@
package coincap_test

import (
"net/http"

"github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

c "github.com/achannarasappa/ticker/internal/common"
. "github.com/achannarasappa/ticker/internal/quote/coincap"
. "github.com/achannarasappa/ticker/test/http"
g "github.com/onsi/gomega/gstruct"
)

var _ = Describe("CoinCap Quote", func() {
Describe("GetAssetQuotes", func() {
It("should make a request to get stock quotes and transform the response", func() {
responseFixture := `{
"data": [
{
"id": "bitcoin",
"rank": "1",
"symbol": "BTC",
"name": "Bitcoin",
"supply": "19685775.0000000000000000",
"maxSupply": "21000000.0000000000000000",
"marketCapUsd": "1248489381324.9592799671502700",
"volumeUsd24Hr": "7744198446.5431034815177485",
"priceUsd": "63420.8905326287270868",
"changePercent24Hr": "1.3622077494913284",
"vwap24Hr": "62988.1090433238215198",
"explorer": "https://blockchain.info/"
}
],
"timestamp": 1714453771801
}`
responseUrl := `=~\/v2\/assets.*ids\=bitcoin.*`
httpmock.RegisterResponder("GET", responseUrl, func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(200, responseFixture)
resp.Header.Set("Content-Type", "application/json")
return resp, nil
})

output := GetAssetQuotes(*client, []string{"bitcoin"})
MockResponseCoincapQuotes()

output := GetAssetQuotes(*client, []string{"elrond"})
Expect(output).To(g.MatchAllElementsWithIndex(g.IndexIdentity, g.Elements{
"0": g.MatchFields(g.IgnoreExtras, g.Fields{
"QuotePrice": g.MatchFields(g.IgnoreExtras, g.Fields{
Expand Down
41 changes: 2 additions & 39 deletions internal/quote/coingecko/coingecko_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package coingecko_test

import (
"net/http"

"github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

c "github.com/achannarasappa/ticker/internal/common"
. "github.com/achannarasappa/ticker/internal/quote/coingecko"
. "github.com/achannarasappa/ticker/test/http"
g "github.com/onsi/gomega/gstruct"
)

Expand All @@ -17,42 +15,7 @@ var _ = Describe("Coingecko", func() {
Describe("GetAssetQuotes", func() {

It("should make a request to get crypto quotes and transform the response", func() {
responseFixture := `[
{
"ath": 69045,
"ath_change_percentage": -43.4461,
"ath_date": "2021-11-10T14:24:11.849Z",
"atl": 67.81,
"atl_change_percentage": 57484.55501,
"atl_date": "2013-07-06T00:00:00.000Z",
"circulating_supply": 18964093.0,
"current_price": 39045,
"fully_diluted_valuation": 819997729028,
"high_24h": 40090,
"id": "bitcoin",
"image": "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
"last_updated": "2022-02-21T01:24:23.221Z",
"low_24h": 38195,
"market_cap": 740500628242,
"market_cap_change_24h": -17241577635.956177,
"market_cap_change_percentage_24h": -2.27539,
"market_cap_rank": 1,
"max_supply": 21000000.0,
"name": "Bitcoin",
"price_change_24h": -978.048909591315,
"price_change_percentage_24h": -2.44373,
"roi": null,
"symbol": "btc",
"total_supply": 21000000.0,
"total_volume": 16659222262
}
]`
responseUrl := "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin&order=market_cap_desc&per_page=250&page=1&sparkline=false"
httpmock.RegisterResponder("GET", responseUrl, func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(200, responseFixture)
resp.Header.Set("Content-Type", "application/json")
return resp, nil
})
MockResponseCoingeckoQuotes()

output := GetAssetQuotes(*client, []string{"bitcoin"})
Expect(output).To(g.MatchAllElementsWithIndex(g.IndexIdentity, g.Elements{
Expand Down
5 changes: 5 additions & 0 deletions internal/quote/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package quote

import (
c "github.com/achannarasappa/ticker/internal/common"
quoteCoincap "github.com/achannarasappa/ticker/internal/quote/coincap"
quoteCoingecko "github.com/achannarasappa/ticker/internal/quote/coingecko"
quoteYahoo "github.com/achannarasappa/ticker/internal/quote/yahoo"
"github.com/go-resty/resty/v2"
Expand All @@ -17,6 +18,10 @@ func getQuoteBySource(dep c.Dependencies, symbolBySource c.AssetGroupSymbolsBySo
return quoteCoingecko.GetAssetQuotes(*dep.HttpClients.Default, symbolBySource.Symbols)
}

if symbolBySource.Source == c.QuoteSourceCoinCap {
return quoteCoincap.GetAssetQuotes(*dep.HttpClients.Default, symbolBySource.Symbols)
}

return []c.AssetQuote{}
}

Expand Down
23 changes: 21 additions & 2 deletions internal/quote/quote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ var _ = Describe("Quote", func() {
},
}
MockResponseYahooQuotes()
MockResponseCoingeckoQuotes()
MockResponseCoincapQuotes()
})

Describe("GetAssetGroupQuote", func() {
Expand All @@ -96,7 +98,12 @@ var _ = Describe("Quote", func() {
Source: c.QuoteSourceCoingecko,
Symbols: []string{
"bitcoin",
"solana",
},
},
{
Source: c.QuoteSourceCoinCap,
Symbols: []string{
"elrond",
},
},
{
Expand All @@ -110,8 +117,20 @@ var _ = Describe("Quote", func() {
}
output := GetAssetGroupQuote(dep)(input)

idFn := func(e interface{}) string { return e.(c.AssetQuote).Symbol }

Expect(output).To(g.MatchFields(g.IgnoreExtras, g.Fields{
"AssetQuotes": HaveLen(2),
"AssetQuotes": g.MatchElements(idFn, g.IgnoreExtras, g.Elements{
"GOOG": g.MatchFields(g.IgnoreExtras, g.Fields{
"QuoteSource": Equal(c.QuoteSourceYahoo),
}),
"BTC": g.MatchFields(g.IgnoreExtras, g.Fields{
"QuoteSource": Equal(c.QuoteSourceCoingecko),
}),
"EGLD": g.MatchFields(g.IgnoreExtras, g.Fields{
"QuoteSource": Equal(c.QuoteSourceCoinCap),
}),
}),
}))

})
Expand Down
71 changes: 71 additions & 0 deletions test/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,77 @@ func MockResponseYahooQuotes() {
})
}

func MockResponseCoingeckoQuotes() {

responseFixture := `[
{
"ath": 69045,
"ath_change_percentage": -43.4461,
"ath_date": "2021-11-10T14:24:11.849Z",
"atl": 67.81,
"atl_change_percentage": 57484.55501,
"atl_date": "2013-07-06T00:00:00.000Z",
"circulating_supply": 18964093.0,
"current_price": 39045,
"fully_diluted_valuation": 819997729028,
"high_24h": 40090,
"id": "bitcoin",
"image": "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
"last_updated": "2022-02-21T01:24:23.221Z",
"low_24h": 38195,
"market_cap": 740500628242,
"market_cap_change_24h": -17241577635.956177,
"market_cap_change_percentage_24h": -2.27539,
"market_cap_rank": 1,
"max_supply": 21000000.0,
"name": "Bitcoin",
"price_change_24h": -978.048909591315,
"price_change_percentage_24h": -2.44373,
"roi": null,
"symbol": "btc",
"total_supply": 21000000.0,
"total_volume": 16659222262
}
]`
responseUrl := "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin&order=market_cap_desc&per_page=250&page=1&sparkline=false"

Check warning on line 168 in test/http/http.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var responseUrl should be responseURL (revive)
httpmock.RegisterResponder("GET", responseUrl, func(req *http.Request) (*http.Response, error) {

Check warning on line 169 in test/http/http.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'req' seems to be unused, consider removing or renaming it as _ (revive)
resp := httpmock.NewStringResponse(200, responseFixture)
resp.Header.Set("Content-Type", "application/json")

return resp, nil
})
}

func MockResponseCoincapQuotes() {

responseFixture := `{
"data": [
{
"id": "elrond",
"rank": "1",
"symbol": "EGLD",
"name": "MultiversX",
"supply": "19685775.0000000000000000",
"maxSupply": "21000000.0000000000000000",
"marketCapUsd": "1248489381324.9592799671502700",
"volumeUsd24Hr": "7744198446.5431034815177485",
"priceUsd": "63420.8905326287270868",
"changePercent24Hr": "1.3622077494913284",
"vwap24Hr": "62988.1090433238215198",
"explorer": "https://blockchain.info/"
}
],
"timestamp": 1714453771801
}`
responseUrl := `=~\/v2\/assets.*ids\=elrond.*`

Check warning on line 198 in test/http/http.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var responseUrl should be responseURL (revive)
httpmock.RegisterResponder("GET", responseUrl, func(req *http.Request) (*http.Response, error) {

Check warning on line 199 in test/http/http.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'req' seems to be unused, consider removing or renaming it as _ (revive)
resp := httpmock.NewStringResponse(200, responseFixture)
resp.Header.Set("Content-Type", "application/json")
return resp, nil

Check failure on line 202 in test/http/http.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
})

}

func MockTickerSymbols() {
responseFixture := `"ADA.X","cardano","cg"
"ALGO.X","algorand","cg"
Expand Down

0 comments on commit 53ec475

Please sign in to comment.