Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarketDataApp committed Feb 6, 2024
1 parent f4e3a4f commit 908d4b1
Show file tree
Hide file tree
Showing 23 changed files with 829 additions and 235 deletions.
118 changes: 118 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

"github.com/go-resty/resty/v2"
_ "github.com/joho/godotenv/autoload"
)

Expand All @@ -27,6 +28,123 @@ func TestNewClient(t *testing.T) {
}
}

// TestGetEnvironment tests the getEnvironment method for various host URLs.
func TestGetEnvironment(t *testing.T) {
// Define test cases
tests := []struct {
name string
hostURL string
expected string
}{
{
name: "Production Environment",
hostURL: "https://api.marketdata.app",
expected: prodEnv,
},
{
name: "Testing Environment",
hostURL: "https://tst.marketdata.app",
expected: testEnv,
},
{
name: "Development Environment",
hostURL: "http://localhost",
expected: devEnv,
},
{
name: "Unknown Environment",
hostURL: "https://unknown.environment",
expected: "Unknown",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create a new MarketDataClient instance
client := &MarketDataClient{
Client: resty.New(),
}

// Set the HostURL to the test case's host URL
client.Client.SetHostURL(tc.hostURL)

// Call getEnvironment and check the result
result := client.getEnvironment()
if result != tc.expected {
t.Errorf("Expected environment %s, got %s", tc.expected, result)
}
})
}
}

// TestEnvironmentMethod tests the Environment method for setting the client's environment.
func TestEnvironmentMethod(t *testing.T) {
// Define test cases
tests := []struct {
name string
environment string
expectedURL string
expectError bool
}{
{
name: "Set Production Environment",
environment: prodEnv,
expectedURL: prodProtocol + "://" + prodHost,
expectError: false,
},
{
name: "Set Testing Environment",
environment: testEnv,
expectedURL: testProtocol + "://" + testHost,
expectError: false,
},
{
name: "Set Development Environment",
environment: devEnv,
expectedURL: devProtocol + "://" + devHost,
expectError: false,
},
{
name: "Set Invalid Environment",
environment: "invalidEnv",
expectedURL: "",
expectError: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create a new MarketDataClient instance
client := NewClient()

// Set the environment using the Environment method
client = client.Environment(tc.environment)

// Check if an error was expected
if tc.expectError {
if client.Error == nil {
t.Errorf("Expected an error for environment %s, but got none", tc.environment)
}
} else {
if client.Error != nil {
t.Errorf("Did not expect an error for environment %s, but got: %v", tc.environment, client.Error)
}

// Verify that the baseURL was set correctly
if client.Client.HostURL != tc.expectedURL {
t.Errorf("Expected baseURL %s, got %s", tc.expectedURL, client.Client.HostURL)
}

// Additionally, verify that getEnvironment returns the correct environment
result := client.getEnvironment()
if result != tc.environment {
t.Errorf("Expected environment %s, got %s", tc.environment, result)
}
}
})
}
}

/*
func TestRateLimit(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions indices_candles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ func ExampleIndicesCandlesRequest_get() {
for _, candle := range vix {
fmt.Println(candle)
}
// Output: Time: 2022-01-03 00:00:00 -05:00, Open: 17.6, High: 18.54, Low: 16.56, Close: 16.6
// Time: 2022-01-04 00:00:00 -05:00, Open: 16.57, High: 17.81, Low: 16.34, Close: 16.91
// Time: 2022-01-05 00:00:00 -05:00, Open: 17.07, High: 20.17, Low: 16.58, Close: 19.73
// Output: IndexCandle{Time: 2022-01-03 00:00:00 -05:00, Open: 17.6, High: 18.54, Low: 16.56, Close: 16.6}
// IndexCandle{Time: 2022-01-04 00:00:00 -05:00, Open: 16.57, High: 17.81, Low: 16.34, Close: 16.91}
// IndexCandle{Time: 2022-01-05 00:00:00 -05:00, Open: 17.07, High: 20.17, Low: 16.58, Close: 19.73}
}

func ExampleIndicesCandlesRequest_packed() {
vix, err := IndexCandles().Symbol("VIX").Resolution("D").From("2022-01-01").To("2022-01-05").Packed()
vix, err := IndexCandles().Symbol("VIX").Resolution("D").To("2022-01-05").Countback(3).Packed()
if err != nil {
println("Error retrieving VIX index candles:", err.Error())
return
}
fmt.Println(vix)
// Output: Time: [1641186000 1641272400 1641358800], Open: [17.6 16.57 17.07], High: [18.54 17.81 20.17], Low: [16.56 16.34 16.58], Close: [16.6 16.91 19.73]
// Output: IndicesCandlesResponse{Time: [1641186000 1641272400 1641358800], Open: [17.6 16.57 17.07], High: [18.54 17.81 20.17], Low: [16.56 16.34 16.58], Close: [16.6 16.91 19.73]}
}

func ExampleIndicesCandlesRequest_raw() {
Expand Down
55 changes: 55 additions & 0 deletions indices_quotes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package client

import (
"testing"
)

func TestIndexQuoteRequest_Packed(t *testing.T) {
iqPacked, err := IndexQuotes().Symbol("VIX").FiftyTwoWeek(true).Packed()
if err != nil {
t.Errorf("Failed to get index quotes: %v", err)
return
}

iq, err := iqPacked.Unpack()
if err != nil {
t.Errorf("Failed to unpack index quotes: %v", err)
return
}

if len(iq) == 0 {
t.Errorf("Unpacked index quotes slice is empty")
return
}

firstQuote := iq[0]
if firstQuote.Symbol != "VIX" {
t.Errorf("Expected symbol VIX, got %s", firstQuote.Symbol)
}

if firstQuote.Last < 0 || firstQuote.Last > 100 {
t.Errorf("Expected last value to be between 0 and 100, got %f", firstQuote.Last)
}
}

func TestIndexQuoteRequest_Get(t *testing.T) {
iq, err := IndexQuotes().Symbol("VIX").FiftyTwoWeek(false).Get()
if err != nil {
t.Errorf("Failed to get index quotes: %v", err)
return
}

if len(iq) == 0 {
t.Errorf("Index quotes slice is empty")
return
}

for _, quote := range iq {
if quote.Symbol != "VIX" {
t.Errorf("Expected symbol VIX, got %s", quote.Symbol)
}
if quote.Last < 0 || quote.Last > 100 {
t.Errorf("Expected last value to be between 0 and 100, got %f", quote.Last)
}
}
}
36 changes: 36 additions & 0 deletions markets_status_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -44,3 +45,38 @@ func TestMarketStatusRequestSetters(t *testing.T) {
t.Errorf("Countback setter failed, got: %d, want: %d.", *msr.dateParams.Countback, countback)
}
}

func ExampleMarketStatus_packed() {

msr, err := MarketStatus().From("2022-01-01").To("2022-01-10").Packed()
if err != nil {
fmt.Print(err)
return
}

fmt.Println(msr)
//Output: MarketStatusResponse{Date: [1641013200, 1641099600, 1641186000, 1641272400, 1641358800, 1641445200, 1641531600, 1641618000, 1641704400, 1641790800], Status: ["closed", "closed", "open", "open", "open", "open", "open", "closed", "closed", "open"]}
}

func ExampleMarketStatus_get() {

msr, err := MarketStatus().From("2022-01-01").To("2022-01-10").Get()
if err != nil {
fmt.Print(err)
return
}

for _, report := range msr {
fmt.Println(report)
}
// Output: MarketStatusReport{Date: 2022-01-01 00:00:00 -0500 EST, Open: false, Closed: true}
// MarketStatusReport{Date: 2022-01-02 00:00:00 -0500 EST, Open: false, Closed: true}
// MarketStatusReport{Date: 2022-01-03 00:00:00 -0500 EST, Open: true, Closed: false}
// MarketStatusReport{Date: 2022-01-04 00:00:00 -0500 EST, Open: true, Closed: false}
// MarketStatusReport{Date: 2022-01-05 00:00:00 -0500 EST, Open: true, Closed: false}
// MarketStatusReport{Date: 2022-01-06 00:00:00 -0500 EST, Open: true, Closed: false}
// MarketStatusReport{Date: 2022-01-07 00:00:00 -0500 EST, Open: true, Closed: false}
// MarketStatusReport{Date: 2022-01-08 00:00:00 -0500 EST, Open: false, Closed: true}
// MarketStatusReport{Date: 2022-01-09 00:00:00 -0500 EST, Open: false, Closed: true}
// MarketStatusReport{Date: 2022-01-10 00:00:00 -0500 EST, Open: true, Closed: false}
}
4 changes: 2 additions & 2 deletions models/indices_candles.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type IndicesCandlesResponse struct {
// Returns:
// - A formatted string containing the time, open, high, low, and close values.
func (icr *IndicesCandlesResponse) String() string {
return fmt.Sprintf("Time: %v, Open: %v, High: %v, Low: %v, Close: %v",
return fmt.Sprintf("IndicesCandlesResponse{Time: %v, Open: %v, High: %v, Low: %v, Close: %v}",
icr.Time, icr.Open, icr.High, icr.Low, icr.Close)
}

Expand Down Expand Up @@ -96,7 +96,7 @@ type IndexCandle struct {
// - A formatted string containing the time, open, high, low, and close values.
func (ic IndexCandle) String() string {
loc, _ := time.LoadLocation("America/New_York")
return fmt.Sprintf("Time: %s, Open: %v, High: %v, Low: %v, Close: %v",
return fmt.Sprintf("IndexCandle{Time: %s, Open: %v, High: %v, Low: %v, Close: %v}",
ic.Time.In(loc).Format("2006-01-02 15:04:05 Z07:00"), ic.Open, ic.High, ic.Low, ic.Close)
}

Expand Down
42 changes: 24 additions & 18 deletions models/indices_quotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,25 @@ type IndexQuote struct {
// - A string that represents the IndexQuote.
func (iq IndexQuote) String() string {
loc, _ := time.LoadLocation("America/New_York")
if iq.High52 != nil && iq.Low52 != nil && iq.Change != nil && iq.ChangePct != nil {
return fmt.Sprintf("Symbol: %s, Last: %v, Volume: %v, Updated: %s, High52: %v, Low52: %v, Change: %v, ChangePct: %v",
iq.Symbol, iq.Last, iq.Volume, iq.Updated.In(loc).Format("2006-01-02 15:04:05 Z07:00"), *iq.High52, *iq.Low52, *iq.Change, *iq.ChangePct)
} else if iq.High52 != nil && iq.Low52 != nil {
return fmt.Sprintf("Symbol: %s, Last: %v, Volume: %v, Updated: %s, High52: %v, Low52: %v",
iq.Symbol, iq.Last, iq.Volume, iq.Updated.In(loc).Format("2006-01-02 15:04:05 Z07:00"), *iq.High52, *iq.Low52)
} else if iq.Change != nil && iq.ChangePct != nil {
return fmt.Sprintf("Symbol: %s, Last: %v, Volume: %v, Updated: %s, Change: %v, ChangePct: %v",
iq.Symbol, iq.Last, iq.Volume, iq.Updated.In(loc).Format("2006-01-02 15:04:05 Z07:00"), *iq.Change, *iq.ChangePct)
} else {
return fmt.Sprintf("Symbol: %s, Last: %v, Volume: %v, Updated: %s",
iq.Symbol, iq.Last, iq.Volume, iq.Updated.In(loc).Format("2006-01-02 15:04:05 Z07:00"))
updatedFormat := iq.Updated.In(loc).Format("2006-01-02 15:04:05 Z07:00")
high52 := "nil"
if iq.High52 != nil {
high52 = fmt.Sprintf("%v", *iq.High52)
}
low52 := "nil"
if iq.Low52 != nil {
low52 = fmt.Sprintf("%v", *iq.Low52)
}
change := "nil"
if iq.Change != nil {
change = fmt.Sprintf("%v", *iq.Change)
}
changePct := "nil"
if iq.ChangePct != nil {
changePct = fmt.Sprintf("%v", *iq.ChangePct)
}
return fmt.Sprintf("IndexQuote{Symbol: %q, Last: %v, Volume: %v, Updated: %q, High52: %s, Low52: %s, Change: %s, ChangePct: %s}",
iq.Symbol, iq.Last, iq.Volume, updatedFormat, high52, low52, change, changePct)
}

// Unpack transforms the IndexQuotesResponse into a slice of IndexQuote.
Expand Down Expand Up @@ -93,22 +99,22 @@ func (iqr *IndexQuotesResponse) Unpack() ([]IndexQuote, error) {
func (iqr *IndexQuotesResponse) String() string {
var result strings.Builder

fmt.Fprintf(&result, "Symbol: [%v], Last: [%v]", strings.Join(iqr.Symbol, ", "), joinFloat64Slice(iqr.Last))
result.WriteString(fmt.Sprintf("IndexQuotesResponse{Symbol: [%v], Last: [%v]", strings.Join(iqr.Symbol, ", "), joinFloat64Slice(iqr.Last)))

if iqr.Change != nil {
fmt.Fprintf(&result, ", Change: [%v]", joinFloat64PointerSlice(iqr.Change))
result.WriteString(fmt.Sprintf(", Change: [%v]", joinFloat64PointerSlice(iqr.Change)))
}
if iqr.ChangePct != nil {
fmt.Fprintf(&result, ", ChangePct: [%v]", joinFloat64PointerSlice(iqr.ChangePct))
result.WriteString(fmt.Sprintf(", ChangePct: [%v]", joinFloat64PointerSlice(iqr.ChangePct)))
}
if iqr.High52 != nil {
fmt.Fprintf(&result, ", High52: [%v]", joinFloat64Slice(*iqr.High52))
result.WriteString(fmt.Sprintf(", High52: [%v]", joinFloat64Slice(*iqr.High52)))
}
if iqr.Low52 != nil {
fmt.Fprintf(&result, ", Low52: [%v]", joinFloat64Slice(*iqr.Low52))
result.WriteString(fmt.Sprintf(", Low52: [%v]", joinFloat64Slice(*iqr.Low52)))
}

fmt.Fprintf(&result, ", Updated: [%v]", joinInt64Slice(iqr.Updated))
result.WriteString(fmt.Sprintf(", Updated: [%v]}", joinInt64Slice(iqr.Updated)))

return result.String()
}
Expand Down
26 changes: 14 additions & 12 deletions models/markets_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@ func (msr *MarketStatusResponse) IsValid() bool {
// Returns:
// - string: A string representation of the MarketStatusResponse.
func (msr *MarketStatusResponse) String() string {
var parts []string
if msr.Status != nil && len(msr.Date) == len(*msr.Status) {
for i, date := range msr.Date {
t := time.Unix(date, 0)
dateStr := t.Format("2006-01-02")
status := (*msr.Status)[i]
part := fmt.Sprintf("Date: %s, Status: %s", dateStr, status)
parts = append(parts, part)
}
}
return "MarketStatusResponse{\n" + strings.Join(parts, ",\n") + "\n}"
dateParts := make([]string, len(msr.Date))
for i, date := range msr.Date {
dateParts[i] = fmt.Sprintf("%v", date)
}
statusParts := "nil"
if msr.Status != nil {
statusSlice := make([]string, len(*msr.Status))
for i, status := range *msr.Status {
statusSlice[i] = fmt.Sprintf("%q", status)
}
statusParts = "[" + strings.Join(statusSlice, ", ") + "]"
}
return fmt.Sprintf("MarketStatusResponse{Date: [%s], Status: %s}", strings.Join(dateParts, ", "), statusParts)
}

// String returns a string representation of the MarketStatusReport.
//
// Returns:
// - string: A string representation of the MarketStatusReport.
func (ms MarketStatusReport) String() string {
return fmt.Sprintf("MarketStatus{Date: %v, Open: %v, Closed: %v}", ms.Date, ms.Open, ms.Closed)
return fmt.Sprintf("MarketStatusReport{Date: %v, Open: %v, Closed: %v}", ms.Date, ms.Open, ms.Closed)
}

// Unpack unpacks the MarketStatusResponse into a slice of MarketStatusReport.
Expand Down
2 changes: 1 addition & 1 deletion models/options_expirations.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (oer *OptionsExpirationsResponse) IsValid() bool {
// Returns:
// - A string that represents the OptionsExpirationsResponse object.
func (oer *OptionsExpirationsResponse) String() string {
return fmt.Sprintf("Expirations: %v, Updated: %v", oer.Expirations, oer.Updated)
return fmt.Sprintf("OptionsExpirationsResponse{Expirations: %v, Updated: %d}", oer.Expirations, oer.Updated)
}

// Unpack converts the expiration date strings in the OptionsExpirationsResponse to a slice of time.Time objects.
Expand Down
Loading

0 comments on commit 908d4b1

Please sign in to comment.