Skip to content

Commit

Permalink
Added remaining quote tests
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Jan 13, 2021
1 parent c2e1db3 commit d5f3d3b
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/quote/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func transformResponseQuote(responseQuote ResponseQuote) Quote {
if responseQuote.MarketState == "POST" {
return Quote{
ResponseQuote: responseQuote,
Price: responseQuote.PostMarketPrice + responseQuote.RegularMarketPrice,
Price: responseQuote.PostMarketPrice,
Change: responseQuote.PostMarketChange + responseQuote.RegularMarketChange,
ChangePercent: responseQuote.PostMarketChangePercent + responseQuote.RegularMarketChangePercent,
IsActive: true,
Expand Down
14 changes: 8 additions & 6 deletions internal/quote/quote_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ package quote_test
import (
"testing"

"github.com/go-resty/resty/v2"
"github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

// var client = resty.New()
var client = resty.New()

var _ = BeforeSuite(func() {
// httpmock.ActivateNonDefault(client.GetClient())
httpmock.ActivateNonDefault(client.GetClient())
})

// var _ = BeforeEach(func() {
// httpmock.Reset()
// })
var _ = BeforeEach(func() {
httpmock.Reset()
})

var _ = AfterSuite(func() {
// httpmock.DeactivateAndReset()
httpmock.DeactivateAndReset()
})

func TestQuote(t *testing.T) {
Expand Down
175 changes: 165 additions & 10 deletions internal/quote/quote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package quote_test
import (
"net/http"

"github.com/go-resty/resty/v2"
"github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -14,17 +13,12 @@ import (
var _ = Describe("Quote", func() {
Describe("GetQuotes", func() {
It("should make a request to get stock quotes and transform the response", func() {
var client = resty.New()
httpmock.ActivateNonDefault(client.GetClient())
responseFixture := `{
"quoteResponse": {
"result": [
{
"marketState": "REGULAR",
"shortName": "Cloudflare, Inc.",
"preMarketChange": 1.0399933,
"preMarketChangePercent": 1.2238094,
"preMarketPrice": 86.02,
"regularMarketChange": 3.0800018,
"regularMarketChangePercent": 3.7606857,
"regularMarketTime": 1608832801,
Expand Down Expand Up @@ -54,9 +48,6 @@ var _ = Describe("Quote", func() {
RegularMarketChangePercent: 3.7606857,
RegularMarketPrice: 84.98,
RegularMarketPreviousClose: 81.9,
PreMarketChange: 1.0399933,
PreMarketChangePercent: 1.2238094,
PreMarketPrice: 86.02,
},
Price: 84.98,
Change: 3.0800018,
Expand All @@ -66,7 +57,171 @@ var _ = Describe("Quote", func() {
},
}
Expect(output).To(Equal(expected))
httpmock.DeactivateAndReset()
})

Context("when the market is in a pre-market trading session", func() {
It("should return the pre-market price", func() {
responseFixture := `{
"quoteResponse": {
"result": [
{
"marketState": "PRE",
"shortName": "Cloudflare, Inc.",
"preMarketChange": 1.0399933,
"preMarketChangePercent": 1.2238094,
"preMarketPrice": 86.02,
"regularMarketChange": 3.0800018,
"regularMarketChangePercent": 3.7606857,
"regularMarketTime": 1608832801,
"regularMarketPrice": 84.98,
"regularMarketPreviousClose": 81.9,
"symbol": "NET"
}
],
"error": null
}
}`
responseUrl := "https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&symbols=NET"
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 := GetQuotes(*client)([]string{"NET"})
expected := []Quote{
{
ResponseQuote: ResponseQuote{
ShortName: "Cloudflare, Inc.",
Symbol: "NET",
MarketState: "PRE",
RegularMarketChange: 3.0800018,
RegularMarketChangePercent: 3.7606857,
RegularMarketPrice: 84.98,
RegularMarketPreviousClose: 81.9,
PreMarketChange: 1.0399933,
PreMarketChangePercent: 1.2238094,
PreMarketPrice: 86.02,
},
Price: 86.02,
Change: 1.0399933,
ChangePercent: 1.2238094,
IsActive: true,
IsRegularTradingSession: false,
},
}
Expect(output).To(Equal(expected))
})
})

Context("when the market is in a post-market trading session", func() {
It("should return the post-market price added to the regular market price", func() {
responseFixture := `{
"quoteResponse": {
"result": [
{
"marketState": "POST",
"shortName": "Cloudflare, Inc.",
"postMarketChange": 1.0399933,
"postMarketChangePercent": 1.2238094,
"postMarketPrice": 86.02,
"regularMarketChange": 3.0800018,
"regularMarketChangePercent": 3.7606857,
"regularMarketTime": 1608832801,
"regularMarketPrice": 84.98,
"regularMarketPreviousClose": 81.9,
"symbol": "NET"
}
],
"error": null
}
}`
responseUrl := "https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&symbols=NET"
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 := GetQuotes(*client)([]string{"NET"})
expected := []Quote{
{
ResponseQuote: ResponseQuote{
ShortName: "Cloudflare, Inc.",
Symbol: "NET",
MarketState: "POST",
RegularMarketChange: 3.0800018,
RegularMarketChangePercent: 3.7606857,
RegularMarketPrice: 84.98,
RegularMarketPreviousClose: 81.9,
PostMarketChange: 1.0399933,
PostMarketChangePercent: 1.2238094,
PostMarketPrice: 86.02,
},
Price: 86.02,
Change: 4.1199951,
ChangePercent: 4.9844951,
IsActive: true,
IsRegularTradingSession: false,
},
}
Expect(output).To(Equal(expected))
})
})

Context("when the market is CLOSED", func() {
It("should return the post-market price added to the regular market price", func() {
responseFixture := `{
"quoteResponse": {
"result": [
{
"marketState": "CLOSED",
"shortName": "Cloudflare, Inc.",
"postMarketChange": 1.0399933,
"postMarketChangePercent": 1.2238094,
"postMarketPrice": 86.02,
"regularMarketChange": 3.0800018,
"regularMarketChangePercent": 3.7606857,
"regularMarketTime": 1608832801,
"regularMarketPrice": 84.98,
"regularMarketPreviousClose": 81.9,
"symbol": "NET"
}
],
"error": null
}
}`
responseUrl := "https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&symbols=NET"
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 := GetQuotes(*client)([]string{"NET"})
expected := []Quote{
{
ResponseQuote: ResponseQuote{
ShortName: "Cloudflare, Inc.",
Symbol: "NET",
MarketState: "CLOSED",
RegularMarketChange: 3.0800018,
RegularMarketChangePercent: 3.7606857,
RegularMarketPrice: 84.98,
RegularMarketPreviousClose: 81.9,
PostMarketChange: 1.0399933,
PostMarketChangePercent: 1.2238094,
PostMarketPrice: 86.02,
},
Price: 84.98,
Change: 0.0,
ChangePercent: 0.0,
IsActive: false,
IsRegularTradingSession: false,
},
}
Expect(output).To(Equal(expected))
})
})
})
})

0 comments on commit d5f3d3b

Please sign in to comment.