Skip to content

Commit

Permalink
test: improved test coverage and fixed tests that were executing netw…
Browse files Browse the repository at this point in the history
…ork requests
  • Loading branch information
achannarasappa committed Jun 1, 2024
1 parent f1ec587 commit 633dbc5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 20 deletions.
19 changes: 14 additions & 5 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ func GetDependencies() c.Dependencies {
return c.Dependencies{
Fs: afero.NewOsFs(),
HttpClients: c.DependenciesHttpClients{
Default: resty.New(),
Yahoo: yahooClient.New(resty.New(), resty.New()),
Default: resty.New(),
Yahoo: yahooClient.New(resty.New(), resty.New()),
YahooSession: resty.New(),
},
}

Expand All @@ -85,7 +86,7 @@ func GetContext(d c.Dependencies, config c.Config) (c.Context, error) {
err error
)

err = yahooClient.RefreshSession(d.HttpClients.Yahoo, resty.New())
err = yahooClient.RefreshSession(d.HttpClients.Yahoo, d.HttpClients.YahooSession)

if err != nil {
return c.Context{}, err
Expand All @@ -97,7 +98,11 @@ func GetContext(d c.Dependencies, config c.Config) (c.Context, error) {
return c.Context{}, err
}

reference, err = getReference(config, groups, *d.HttpClients.Yahoo)
reference, err = getReference(config, groups, d.HttpClients.Yahoo)

if err != nil {
return c.Context{}, err
}

context := c.Context{
Reference: reference,
Expand Down Expand Up @@ -131,11 +136,15 @@ func readConfig(fs afero.Fs, configPathOption string) (c.Config, error) {
return config, nil
}

func getReference(config c.Config, assetGroups []c.AssetGroup, client resty.Client) (c.Reference, error) {
func getReference(config c.Config, assetGroups []c.AssetGroup, client *resty.Client) (c.Reference, error) {

currencyRates, err := quote.GetAssetGroupsCurrencyRates(client, assetGroups, config.Currency)
styles := util.GetColorScheme(config.ColorScheme)

if err != nil {
return c.Reference{}, err
}

return c.Reference{
CurrencyRates: currencyRates,
Styles: styles,
Expand Down
51 changes: 43 additions & 8 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/achannarasappa/ticker/internal/cli"
. "github.com/achannarasappa/ticker/internal/cli"
c "github.com/achannarasappa/ticker/internal/common"
"github.com/achannarasappa/ticker/test/http"
httpMocks "github.com/achannarasappa/ticker/test/http"
)

func getStdout(fn func()) string {
Expand Down Expand Up @@ -67,13 +67,16 @@ var _ = Describe("Cli", func() {
dep = c.Dependencies{
Fs: afero.NewMemMapFs(),
HttpClients: c.DependenciesHttpClients{
Default: client,
Yahoo: client,
Default: client,
Yahoo: client,
YahooSession: client,
},
}

http.MockTickerSymbols()
http.MockResponseCurrency()
httpMocks.MockTickerSymbols()
httpMocks.MockResponseCurrency()
httpMocks.MockResponseForRefreshSessionSuccess()

//nolint:errcheck
dep.Fs.MkdirAll("./", 0755)
})
Expand Down Expand Up @@ -293,7 +296,7 @@ var _ = Describe("Cli", func() {

It("returns the error", func() {

http.MockTickerSymbolsError()
httpMocks.MockTickerSymbolsError()

_, outputErr := GetContext(dep, c.Config{})

Expand All @@ -305,9 +308,25 @@ var _ = Describe("Cli", func() {

When("there is an error getting reference data", func() {

PIt("returns the error", func() {
It("returns the error", func() {

httpMocks.MockResponseCurrencyError()

_, outputErr := GetContext(dep, c.Config{
Watchlist: []string{"TSLA"},
})

Expect(outputErr).ToNot(BeNil())

})

http.MockResponseCurrencyError()
})

When("there is an error refreshing the yahoo session", func() {

It("returns the error", func() {

httpMocks.MockResponseForRefreshSessionError()

_, outputErr := GetContext(dep, c.Config{})

Expand Down Expand Up @@ -543,6 +562,22 @@ var _ = Describe("Cli", func() {
})
})

Describe("GetDependencies", func() {

It("should dependencies", func() {

output := GetDependencies()
expected := g.MatchAllFields(g.Fields{
"Fs": BeAssignableToTypeOf(afero.NewOsFs()),
"HttpClients": BeAssignableToTypeOf(c.DependenciesHttpClients{}),
})

Expect(output).To(expected)

})

})

Describe("Validate", func() {

var (
Expand Down
5 changes: 3 additions & 2 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ type Dependencies struct {
}

type DependenciesHttpClients struct { //nolint:golint,stylecheck,revive
Default *resty.Client
Yahoo *resty.Client
Default *resty.Client
Yahoo *resty.Client
YahooSession *resty.Client
}

// Lot represents a cost basis lot
Expand Down
4 changes: 2 additions & 2 deletions internal/quote/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func getUniqueSymbolsBySource(assetGroups []c.AssetGroup) []c.AssetGroupSymbolsB
}

// GetAssetGroupsCurrencyRates gets the currency rates by source across all asset groups
func GetAssetGroupsCurrencyRates(client resty.Client, assetGroups []c.AssetGroup, targetCurrency string) (c.CurrencyRates, error) {
func GetAssetGroupsCurrencyRates(client *resty.Client, assetGroups []c.AssetGroup, targetCurrency string) (c.CurrencyRates, error) {

var err error
var currencyRates c.CurrencyRates
Expand All @@ -89,7 +89,7 @@ func GetAssetGroupsCurrencyRates(client resty.Client, assetGroups []c.AssetGroup
for _, source := range uniqueSymbolsBySource {

if source.Source == c.QuoteSourceYahoo && err == nil {
currencyRates, err = quoteYahoo.GetCurrencyRates(client, source.Symbols, targetCurrency)
currencyRates, err = quoteYahoo.GetCurrencyRates(*client, source.Symbols, targetCurrency)
}

}
Expand Down
2 changes: 1 addition & 1 deletion internal/quote/quote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ var _ = Describe("Quote", func() {
},
},
}
output, _ := GetAssetGroupsCurrencyRates(*client, input, "EUR")
output, _ := GetAssetGroupsCurrencyRates(client, input, "EUR")
Expect(output).To(g.MatchAllKeys(g.Keys{
"USD": g.MatchFields(g.IgnoreExtras, g.Fields{
"FromCurrency": Equal("USD"),
Expand Down
33 changes: 31 additions & 2 deletions test/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func MockResponseCurrency() {
func MockResponseCurrencyError() {
responseUrl := `=~.*\/finance\/quote.*` //nolint:golint,stylecheck,revive
httpmock.RegisterResponder("GET", responseUrl, func(_ *http.Request) (*http.Response, error) {
return &http.Response{}, errors.New("error getting currencies") //nolint:goerr113

return httpmock.NewStringResponse(http.StatusInternalServerError, "server error"), errors.New("error getting currencies") //nolint:goerr113
})
}

Expand Down Expand Up @@ -155,6 +156,34 @@ func MockTickerSymbols() {
func MockTickerSymbolsError() {
responseUrl := "https://raw.githubusercontent.com/achannarasappa/ticker-static/master/symbols.csv" //nolint:golint,stylecheck,revive
httpmock.RegisterResponder("GET", responseUrl, func(_ *http.Request) (*http.Response, error) {
return &http.Response{}, errors.New("error getting ticker symbols") //nolint:goerr113

return httpmock.NewStringResponse(http.StatusInternalServerError, "server error"), errors.New("error getting ticker symbols") //nolint:goerr113
})
}

// Mocks for Yahoo session refresh
func MockResponseForRefreshSessionSuccess() {
httpmock.RegisterResponder("GET", "https://finance.yahoo.com/", func(request *http.Request) (*http.Response, error) {

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

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'request' seems to be unused, consider removing or renaming it as _ (revive)
response := httpmock.NewStringResponse(http.StatusOK, "")
response.Header.Set("Set-Cookie", "A3=d=AQABBPMJfWQCWPnJSAFIwq1PtsjJQ_yNsJ8FEgEBAQFbfmSGZNxN0iMA_eMAAA&S=AQAAAk_fgKYu72Cro5IHlbBd6yg; Expires=Tue, 4 Jun 2024 04:02:28 GMT; Max-Age=31557600; Domain=.yahoo.com; Path=/; SameSite=None; Secure; HttpOnly")

return response, nil
})

httpmock.RegisterResponder("GET", "https://query2.finance.yahoo.com/v1/test/getcrumb", func(request *http.Request) (*http.Response, error) {

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

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'request' seems to be unused, consider removing or renaming it as _ (revive)
return httpmock.NewStringResponse(http.StatusOK, "MrBKM4QQ"), nil
})
}

func MockResponseForRefreshSessionError() {
httpmock.RegisterResponder("GET", "https://finance.yahoo.com/", func(request *http.Request) (*http.Response, error) {

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

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'request' seems to be unused, consider removing or renaming it as _ (revive)
response := httpmock.NewStringResponse(http.StatusOK, "")
response.Header.Set("Set-Cookie", "A3=d=AQABBPMJfWQCWPnJSAFIwq1PtsjJQ_yNsJ8FEgEBAQFbfmSGZNxN0iMA_eMAAA&S=AQAAAk_fgKYu72Cro5IHlbBd6yg; Expires=Tue, 4 Jun 2024 04:02:28 GMT; Max-Age=31557600; Domain=.yahoo.com; Path=/; SameSite=None; Secure; HttpOnly")

return response, nil
})

httpmock.RegisterResponder("GET", "https://query2.finance.yahoo.com/v1/test/getcrumb", func(request *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusBadRequest, "tests"), nil
})
}

0 comments on commit 633dbc5

Please sign in to comment.