Skip to content

Commit

Permalink
Add test, cleanup (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanglie committed May 19, 2023
1 parent 44bc928 commit 4cdfeda
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
7 changes: 7 additions & 0 deletions br_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br

import (
"math"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -84,4 +85,10 @@ func TestRates_String(t *testing.T) {
if got != want {
t.Errorf("got= %v, want= %v", got, want)
}

// Negative case
r.Branches[0].Sell = math.NaN()
if got = r.String(); len(got) > 0 {
t.Errorf("got = %v, want \"\" (emtpy)", got)
}
}
39 changes: 16 additions & 23 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,28 @@ const (
)

var (
Debug bool // Debug mode. Default: false.
Crnc Currency = USD // Default currency.
Ct City = Moscow // Default city.
// Debug mode. Default: false.
Debug bool
)

// URL type.
type URL struct {
buildFunc func() string
}

// build returns a URL.
func (u *URL) build() string {
return u.buildFunc()
}

// Client.
type Client struct {
currency Currency
city City
buildURL func() string
collector *colly.Collector
url *URL
}

// NewClient creates a new client.
func NewClient() *Client {
c := &Client{
collector: colly.NewCollector(colly.AllowURLRevisit()),
url: &URL{buildFunc: func() string {
return fmt.Sprintf(baseURL, strings.ToLower(string(Crnc)), Ct)
}},
c := &Client{}

c.currency = USD
c.city = Moscow
c.buildURL = func() string {
return fmt.Sprintf(baseURL, strings.ToLower(string(c.currency)), c.city)
}
c.collector = colly.NewCollector(colly.AllowURLRevisit())

t := &http.Transport{}
t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
Expand All @@ -101,15 +94,15 @@ func NewClient() *Client {
// Rates by currency (USD, if empty) and city (Moscow, if empty).
func (c *Client) Rates(crnc Currency, ct City) (*Rates, error) {
if len(crnc) > 0 {
Crnc = crnc
c.currency = crnc
}

if len(ct) > 0 {
Ct = ct
c.city = ct
}

if Debug {
log.Printf("Fetching the currency rate from %s", c.url.build())
log.Printf("Fetching the currency rate from %s", c.buildURL())
}

r := &Rates{Currency: crnc, City: ct}
Expand Down Expand Up @@ -190,7 +183,7 @@ func (c *Client) parseBranches() ([]Branch, error) {
log.Println(err)
})

err = c.collector.Visit(c.url.build())
err = c.collector.Visit(c.buildURL())

return b, err
}
14 changes: 7 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestClient_Rates(t *testing.T) {
c := NewClient()
c.url.buildFunc = func() string {
c.buildURL = func() string {
dir, _ := os.Getwd()
return "file:" + filepath.Join(dir, "/test/bankiru")
}
Expand All @@ -34,7 +34,7 @@ func TestClient_RatesError(t *testing.T) {
Debug = true

c := NewClient()
c.url.buildFunc = func() string {
c.buildURL = func() string {
dir, _ := os.Getwd()
return "file:" + filepath.Join(dir, "/test/invalid-bankiru")
}
Expand All @@ -45,13 +45,13 @@ func TestClient_RatesError(t *testing.T) {
}

func TestURL_build(t *testing.T) {
url := &URL{buildFunc: func() string {
return fmt.Sprintf(baseURL, strings.ToLower(string(Crnc)), Ct)
}}
buildURL := func() string {
return fmt.Sprintf(baseURL, strings.ToLower(string(USD)), Moscow)
}

want := (NewClient()).url.build()
want := (NewClient()).buildURL()

if got := url.build(); got != want {
if got := buildURL(); got != want {
t.Errorf("URL.build() = %v, want %v", got, want)
}
}

0 comments on commit 4cdfeda

Please sign in to comment.