Skip to content

Commit

Permalink
Add unit tests #1
Browse files Browse the repository at this point in the history
* accounts
* addresses
* buys
  • Loading branch information
Lukas Matt committed Jan 31, 2017
1 parent d2004a5 commit f3729a9
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 8 deletions.
9 changes: 6 additions & 3 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

package coinbase

import "bytes"
import (
"bytes"
"time"
)

/*
Expand Down Expand Up @@ -54,8 +57,8 @@ type APIAccountData struct {
Currency string
Balance APIBalance
Native_balance APIBalance
Created_at string
Updated_at string
Created_at *time.Time
Updated_at *time.Time
Resource string
Resource_path string
}
Expand Down
9 changes: 6 additions & 3 deletions addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

package coinbase

import "bytes"
import (
"bytes"
"time"
)

/*
Expand All @@ -41,8 +44,8 @@ type APIAddressData struct {
Id string
Address string
Name string
Created_at string
Updated_at string
Created_at *time.Time
Updated_at *time.Time
Network string
Resource string
Resource_path string
Expand Down
4 changes: 2 additions & 2 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ type APIWalletTransferData struct {
Amount APIBalance
Total APIBalance
Subtotal APIBalance
Created_at string
Updated_at string
Created_at *time.Time
Updated_at *time.Time
Resource string
Resource_path string
Committed bool
Expand Down
59 changes: 59 additions & 0 deletions test/accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package coinbase_test

import (
"testing"
"zauberstuhl/coinbase"
)

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"
}
}
`)
defer s.Close()

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

acc, err := a.Account("2bbf394c-193b-5b2a-9155-3b4732659ede")
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)
}
if acc.Data.Balance.Amount != 39.59 {
t.Error("Expected 39.59, got ", acc.Data.Balance.Amount)
}
if acc.Data.Native_balance.Amount != 395.90 {
t.Error("Expected 395.90, got ", acc.Data.Native_balance.Amount)
}
if acc.Data.Created_at.Year() != 2015 {
t.Error("Expected 2015, got ", acc.Data.Created_at.Year())
}
if acc.Data.Updated_at.Year() != 2015 {
t.Error("Expected 2015, got ", acc.Data.Created_at.Year())
}
}
52 changes: 52 additions & 0 deletions test/addresses_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package coinbase_test

import (
"testing"
"zauberstuhl/coinbase"
)

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"
}
}
`)
defer s.Close()

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

address, err := a.GetAddress(
"dd3183eb-af1d-5f5d-a90d-cbff946435ff",
"mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa",
)
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)
}
if address.Data.Address != "mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa" {
t.Error("Expected mswUGcPHp1YnkLCgF1TtoryqSc5E9Q8xFa, got ",
address.Data.Address)
}
if address.Data.Created_at.Year() != 2015 {
t.Error("Expected 2015, got ", address.Data.Created_at.Year())
}
if address.Data.Updated_at.Year() != 2015 {
t.Error("Expected 2015, got ", address.Data.Created_at.Year())
}
}
87 changes: 87 additions & 0 deletions test/buys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package coinbase_test

import (
"testing"
"zauberstuhl/coinbase"
)

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"
}
}
`)
defer s.Close()

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

buys, err := a.ShowBuy("123", "456")
if err != nil {
t.Error("Expected nil, got ", err.Error())
}
if buys.Data.Id != "9e14d574-30fa-5d85-b02c-6be0d851d61d" {
t.Error("Expected 9e14d574-30fa-5d85-b02c-6be0d851d61d, got ",
buys.Data.Id)
}

wrapper := func(data interface{}) {
switch r := data.(type) {
case coinbase.APIResource:
case coinbase.APIBalance:
default:
t.Error("Expected APIBalance or APIResource, got ", r)
}
}
wrapper(buys.Data.Payment_method)
wrapper(buys.Data.Transaction)
wrapper(buys.Data.Amount)
wrapper(buys.Data.Total)
wrapper(buys.Data.Subtotal)

if buys.Data.Created_at.Year() != 2015 {
t.Error("Expected 2015, got ", buys.Data.Created_at.Year())
}
if buys.Data.Updated_at.Year() != 2015 {
t.Error("Expected 2015, got ", buys.Data.Created_at.Year())
}
}
14 changes: 14 additions & 0 deletions test/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package coinbase_test

import (
"net/http"
"net/http/httptest"
"fmt"
)

func serverHelper(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)
}))
}

0 comments on commit f3729a9

Please sign in to comment.