Skip to content

Commit

Permalink
Test coverage for rates
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jun 4, 2021
1 parent 4b0b9fa commit c5b9113
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 7 deletions.
29 changes: 29 additions & 0 deletions examples/rates/current_rate/current_rate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"log"
"os"

"github.com/tonicpow/go-tonicpow"
)

func main() {

// Load the api client
client, err := tonicpow.NewClient(
tonicpow.WithAPIKey(os.Getenv("TONICPOW_API_KEY")),
tonicpow.WithEnvironmentString(os.Getenv("TONICPOW_ENVIRONMENT")),
)
if err != nil {
log.Fatalf("error in NewClient: %s", err.Error())
}

// Get current rate
var rate *tonicpow.Rate
rate, err = client.GetCurrentRate("usd", 1.00)
if err != nil {
log.Fatalf("error in GetCurrentRate: %s", err.Error())
}

log.Printf("rate: %s %f is %d sats", rate.Currency, rate.CurrencyAmount, rate.PriceInSatoshis)
}
10 changes: 3 additions & 7 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ type Goal struct {
//
// For more information: https://docs.tonicpow.com/#fb00736e-61b9-4ec9-acaf-e3f9bb046c89
type Rate struct {
Currency string `json:"currency"`
CurrencyAmount float64 `json:"currency_amount"`
CurrencyLastUpdated string `json:"currency_last_updated,omitempty"`
CurrencyName string `json:"currency_name,omitempty"`
Price float64 `json:"price"`
PriceInSatoshis int64 `json:"price_in_satoshis"`
RateLastUpdated string `json:"rate_last_updated,omitempty"`
Currency string `json:"currency"`
CurrencyAmount float64 `json:"currency_amount"`
PriceInSatoshis int64 `json:"price_in_satoshis"`
}
178 changes: 178 additions & 0 deletions rates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package tonicpow

import (
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

// newTestRate creates a dummy profile for testing
func newTestRate() *Rate {
return &Rate{
Currency: testRateCurrency,
CurrencyAmount: 0.01,
PriceInSatoshis: 4200,
}
}

// TestClient_GetCurrentRate will test the method GetCurrentRate()
func TestClient_GetCurrentRate(t *testing.T) {
// t.Parallel() (Cannot run in parallel - issues with overriding the mock client)

t.Run("get current rate (success)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)

rates := newTestRate()

endpoint := fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
)

err = mockResponseData(http.MethodGet, endpoint, http.StatusOK, rates)
assert.NoError(t, err)

var currentRate *Rate
currentRate, err = client.GetCurrentRate(testRateCurrency, 0.00)
assert.NoError(t, err)
assert.NotNil(t, currentRate)
assert.Equal(t, testRateCurrency, currentRate.Currency)
})

t.Run("missing currency", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)

rates := newTestRate()

endpoint := fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
)

err = mockResponseData(http.MethodGet, endpoint, http.StatusOK, rates)
assert.NoError(t, err)

var currentRate *Rate
currentRate, err = client.GetCurrentRate("", 0.00)
assert.Error(t, err)
assert.Nil(t, currentRate)
})

t.Run("error from api (status code)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)

rates := newTestRate()

endpoint := fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
)

err = mockResponseData(http.MethodGet, endpoint, http.StatusBadRequest, rates)
assert.NoError(t, err)

var currentRate *Rate
currentRate, err = client.GetCurrentRate(testRateCurrency, 0.00)
assert.Error(t, err)
assert.Nil(t, currentRate)
})

t.Run("error from api (status code)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)

endpoint := fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
)

apiError := &Error{
Code: 400,
Data: "field_name",
IPAddress: "127.0.0.1",
Message: "some error message",
Method: http.MethodPut,
RequestGUID: "7f3d97a8fd67ff57861904df6118dcc8",
StatusCode: http.StatusBadRequest,
URL: endpoint,
}

err = mockResponseData(http.MethodGet, endpoint, http.StatusBadRequest, apiError)
assert.NoError(t, err)

var currentRate *Rate
currentRate, err = client.GetCurrentRate(testRateCurrency, 0.00)
assert.Error(t, err)
assert.Nil(t, currentRate)
assert.Equal(t, apiError.Message, err.Error())
})
}

// ExampleClient_GetCurrentRate example using GetCurrentRate()
//
// See more examples in /examples/
func ExampleClient_GetCurrentRate() {

// Load the client (using test client for example only)
client, err := newTestClient()
if err != nil {
fmt.Printf("error loading client: %s", err.Error())
return
}

// For mocking
rates := newTestRate()

// Mock response (for example only)
_ = mockResponseData(
http.MethodGet,
fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
),
http.StatusOK,
rates,
)

// Get profile (using mocking response)
var currentRate *Rate
if currentRate, err = client.GetCurrentRate(testRateCurrency, 0.00); err != nil {
fmt.Printf("error getting profile: " + err.Error())
return
}
fmt.Printf("current rate: %s %f usd is %d sats", currentRate.Currency, currentRate.CurrencyAmount, currentRate.PriceInSatoshis)
// Output:current rate: usd 0.010000 usd is 4200 sats
}

// BenchmarkClient_GetCurrentRate benchmarks the method GetCurrentRate()
func BenchmarkClient_GetCurrentRate(b *testing.B) {
client, _ := newTestClient()
rate := newTestRate()
_ = mockResponseData(
http.MethodGet,
fmt.Sprintf(
"%s/%s/%s?%s=%f", EnvironmentDevelopment.apiURL,
modelRates, testRateCurrency,
fieldAmount, 0.0,
),
http.StatusOK,
rate,
)
for i := 0; i < b.N; i++ {
_, _ = client.GetCurrentRate(testRateCurrency, 0.00)
}
}
1 change: 1 addition & 0 deletions tonicpow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
testCampaignID uint64 = 23
testCampaignTargetURL string = "https://tonicpow.com"
testGoalID uint64 = 13
testRateCurrency string = "usd"
testUserID uint64 = 43
)

Expand Down

0 comments on commit c5b9113

Please sign in to comment.