Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more unit tests #5

Merged
merged 2 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 127 additions & 34 deletions accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@ import (
"testing"
)

func TestAccounts(t *testing.T) {
s := serverHelper(`
{
"data": {
"id": "2bbf394c-193b-5b2a-9155-3b4732659ede",
"name": "My Wallet",
"primary": true,
"type": "wallet",
"currency": "BTC",
"balance": {
"amount": "39.59000000",
"currency": "BTC"
},
"native_balance": {
"amount": "395.90",
"currency": "USD"
},
"created_at": "2015-01-31T20:49:02Z",
"updated_at": "2015-01-31T20:49:02Z",
"resource": "account",
"resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede"
}
}
`)
func TestAccount(t *testing.T) {
method := "GET"
uri := "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede"
body := `{
"data": {
"id": "2bbf394c-193b-5b2a-9155-3b4732659ede",
"name": "My Wallet",
"primary": true,
"type": "wallet",
"currency": "BTC",
"balance": {
"amount": "39.59000000",
"currency": "BTC"
},
"native_balance": {
"amount": "395.90",
"currency": "USD"
},
"created_at": "2015-01-31T20:49:02Z",
"updated_at": "2015-01-31T20:49:02Z",
"resource": "account",
"resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede"
}
}`
s := serverHelper(t, &method, &uri, &body)
defer s.Close()

a := APIClient{
Expand All @@ -40,19 +41,111 @@ func TestAccounts(t *testing.T) {
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
if acc.Data.Id != "2bbf394c-193b-5b2a-9155-3b4732659ede" {
t.Error("Expected 2bbf394c-193b-5b2a-9155-3b4732659ede, got ", acc.Data.Id)
account(t, acc.Data)

method = "POST"
uri = "/v2/accounts"
acc, err = a.CreateAccount("My Wallet")
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
account(t, acc.Data)

uri = "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/primary"
acc, err = a.SetPrimaryAccount("2bbf394c-193b-5b2a-9155-3b4732659ede")
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
account(t, acc.Data)

method = "PUT"
uri = "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede"
acc, err = a.UpdateAccount("2bbf394c-193b-5b2a-9155-3b4732659ede", "My Wallet")
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
account(t, acc.Data)

method = "DELETE"
acc, err = a.DeleteAccount("2bbf394c-193b-5b2a-9155-3b4732659ede")
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
account(t, acc.Data)
}

func TestAccounts(t *testing.T) {
method := "GET"
uri := "/v2/accounts"
body := `{
"pagination": {
"ending_before": null,
"starting_after": null,
"limit": 25,
"order": "desc",
"previous_uri": null,
"next_uri": null
},
"data": [
{
"id": "2bbf394c-193b-5b2a-9155-3b4732659ede",
"name": "My Vault",
"primary": false,
"type": "vault",
"currency": "BTC",
"balance": {
"amount": "39.59000000",
"currency": "BTC"
},
"native_balance": {
"amount": "395.90",
"currency": "USD"
},
"created_at": "2015-01-31T20:49:02Z",
"updated_at": "2015-01-31T20:49:02Z",
"resource": "account",
"resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede",
"ready": true
}
]
}`
s := serverHelper(t, &method, &uri, &body)
defer s.Close()

a := APIClient{
Key: "123",
Secret: "123456",
Endpoint: s.URL,
}

accs, err := a.Accounts()
if err != nil {
t.Error("Expected nil, got ", err.Error())
}

if len(accs.Data) != 1 {
t.Errorf("Expected len to be 1, got %d", len(accs.Data))
}

for _, data := range accs.Data {
account(t, data)
}
}

func account(t *testing.T, acc APIAccountData) {
if acc.Id != "2bbf394c-193b-5b2a-9155-3b4732659ede" {
t.Error("Expected 2bbf394c-193b-5b2a-9155-3b4732659ede, got ", acc.Id)
}
if acc.Data.Balance.Amount != 39.59 {
t.Error("Expected 39.59, got ", acc.Data.Balance.Amount)
if acc.Balance.Amount != 39.59 {
t.Error("Expected 39.59, got ", acc.Balance.Amount)
}
if acc.Data.Native_balance.Amount != 395.90 {
t.Error("Expected 395.90, got ", acc.Data.Native_balance.Amount)
if acc.Native_balance.Amount != 395.90 {
t.Error("Expected 395.90, got ", acc.Native_balance.Amount)
}
if acc.Data.Created_at.Year() != 2015 {
t.Error("Expected 2015, got ", acc.Data.Created_at.Year())
if acc.Created_at.Year() != 2015 {
t.Error("Expected 2015, got ", acc.Created_at.Year())
}
if acc.Data.Updated_at.Year() != 2015 {
t.Error("Expected 2015, got ", acc.Data.Created_at.Year())
if acc.Updated_at.Year() != 2015 {
t.Error("Expected 2015, got ", acc.Created_at.Year())
}
}
66 changes: 39 additions & 27 deletions addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import (
"testing"
)

func TestAddresses(t *testing.T) {
s := serverHelper(`
{
"data": {
"id": "dd3183eb-af1d-5f5d-a90d-cbff946435ff",
"address": "mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa",
"name": "One off payment",
"created_at": "2015-01-31T20:49:02Z",
"updated_at": "2015-03-31T17:25:29-07:00",
"network": "bitcoin",
"resource": "address",
"resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/addresses/dd3183eb-af1d-5f5d-a90d-cbff946435ff"
}
}
`)
func TestAddress(t *testing.T) {
method := "GET"
uri := "/v2/accounts/dd3183eb-af1d-5f5d-a90d-cbff946435ff/addresses/mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa"
body := `{
"data": {
"id": "dd3183eb-af1d-5f5d-a90d-cbff946435ff",
"address": "mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa",
"name": "One off payment",
"created_at": "2015-01-31T20:49:02Z",
"updated_at": "2015-03-31T17:25:29-07:00",
"network": "bitcoin",
"resource": "address",
"resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/addresses/dd3183eb-af1d-5f5d-a90d-cbff946435ff"
}
}`
s := serverHelper(t, &method, &uri, &body)
defer s.Close()

a := APIClient{
Expand All @@ -27,25 +28,36 @@ func TestAddresses(t *testing.T) {
Endpoint: s.URL,
}

address, err := a.GetAddress(
add, err := a.GetAddress(
"dd3183eb-af1d-5f5d-a90d-cbff946435ff",
"mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa")
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
address(t, add.Data)

method = "POST"
uri = "/v2/accounts/mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa/addresses"
add, err = a.CreateAddress(
"mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa",
)
"One off payment")
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
if address.Data.Id != "dd3183eb-af1d-5f5d-a90d-cbff946435ff" {
t.Error("Expected dd3183eb-af1d-5f5d-a90d-cbff946435ff, got ",
address.Data.Id)
address(t, add.Data)
}

func address(t *testing.T, address APIAddressData) {
if address.Id != "dd3183eb-af1d-5f5d-a90d-cbff946435ff" {
t.Error("Expected dd3183eb-af1d-5f5d-a90d-cbff946435ff, got ", address.Id)
}
if address.Data.Address != "mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa" {
t.Error("Expected mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa, got ",
address.Data.Address)
if address.Address != "mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa" {
t.Error("Expected mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa, got ", address.Address)
}
if address.Data.Created_at.Year() != 2015 {
t.Error("Expected 2015, got ", address.Data.Created_at.Year())
if address.Created_at.Year() != 2015 {
t.Error("Expected 2015, got ", address.Created_at.Year())
}
if address.Data.Updated_at.Year() != 2015 {
t.Error("Expected 2015, got ", address.Data.Created_at.Year())
if address.Updated_at.Year() != 2015 {
t.Error("Expected 2015, got ", address.Created_at.Year())
}
}
83 changes: 42 additions & 41 deletions buys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,48 @@ import (
)

func TestBuys(t *testing.T) {
s := serverHelper(`
{
"data": {
"id": "9e14d574-30fa-5d85-b02c-6be0d851d61d",
"status": "created",
"payment_method": {
"id": "83562370-3e5c-51db-87da-752af5ab9559",
"resource": "payment_method",
"resource_path": "/v2/payment-methods/83562370-3e5c-51db-87da-752af5ab9559"
},
"transaction": {
"id": "4117f7d6-5694-5b36-bc8f-847509850ea4",
"resource": "transaction",
"resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/transactions/441b9494-b3f0-5b98-b9b0-4d82c21c252a"
},
"amount": {
"amount": "10.00000000",
"currency": "BTC"
},
"total": {
"amount": "102.01",
"currency": "USD"
},
"subtotal": {
"amount": "101.00",
"currency": "USD"
},
"created_at": "2015-03-26T23:43:59-07:00",
"updated_at": "2015-03-26T23:44:09-07:00",
"resource": "buy",
"resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/buys/9e14d574-30fa-5d85-b02c-6be0d851d61d",
"committed": true,
"instant": false,
"fee": {
"amount": "1.01",
"currency": "USD"
},
"payout_at": "2015-04-01T23:43:59-07:00"
}
}
`)
method := "GET"
uri := "/v2/accounts/123/buys/456"
body := `{
"data": {
"id": "9e14d574-30fa-5d85-b02c-6be0d851d61d",
"status": "created",
"payment_method": {
"id": "83562370-3e5c-51db-87da-752af5ab9559",
"resource": "payment_method",
"resource_path": "/v2/payment-methods/83562370-3e5c-51db-87da-752af5ab9559"
},
"transaction": {
"id": "4117f7d6-5694-5b36-bc8f-847509850ea4",
"resource": "transaction",
"resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/transactions/441b9494-b3f0-5b98-b9b0-4d82c21c252a"
},
"amount": {
"amount": "10.00000000",
"currency": "BTC"
},
"total": {
"amount": "102.01",
"currency": "USD"
},
"subtotal": {
"amount": "101.00",
"currency": "USD"
},
"created_at": "2015-03-26T23:43:59-07:00",
"updated_at": "2015-03-26T23:44:09-07:00",
"resource": "buy",
"resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/buys/9e14d574-30fa-5d85-b02c-6be0d851d61d",
"committed": true,
"instant": false,
"fee": {
"amount": "1.01",
"currency": "USD"
},
"payout_at": "2015-04-01T23:43:59-07:00"
}
}`
s := serverHelper(t, &method, &uri, &body)
defer s.Close()

a := APIClient{
Expand Down
2 changes: 1 addition & 1 deletion exchange_rates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGetExchangeRates(t *testing.T) {
t.Error("Expected currency length equal three")
}
for key, value := range exch.Data.Rates {
if len(key) != 3 {
if len(key) < 3 {
t.Error("Expected ", key, " length equal three")
}
rate, err := value.Float64()
Expand Down
25 changes: 23 additions & 2 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@ import (
"net/http"
"net/http/httptest"
"fmt"
"testing"
)

func serverHelper(body string) *httptest.Server {
func serverHelper(t *testing.T, method, uri, body *string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, body)

if r.RequestURI == "/v2/time" {
// NOTE this is used on a signing request
fmt.Fprintln(w, `{
"data": {
"iso": "2015-06-23T18:02:51Z",
"epoch": 1435082571
}
}`)
return
}

if *method != r.Method {
t.Errorf("Expected http method %s, got %s", *method, r.Method)
}

if *uri != r.RequestURI {
t.Errorf("Expected request uri %s, got %s", *uri, r.RequestURI)
}

fmt.Fprintln(w, *body)
}))
}