forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
133 lines (107 loc) · 3.41 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Package mock provides mocks attached to the Recurly client for testing
code using the Recurly API client.
This package makes it easy to test code using the Recurly API by focusing on
the arguments passed into each function and returning the expected result as
structs from the Recurly package. There is no need to deal with XML or make any HTTP requests.
Simple Setup
The basic setup for a test involves creating a new mock client, attaching mocks,
and returning expected results.
func TestFoo(t *testing.T) {
client := mock.NewClient("subdomain", "key")
client.Billing.OnGet = func(ctx context.Context, accountCode string) (*recurly.Billing, error) {
if accountCode != "10" {
t.Fatalf("unexpected account code: %s", accountCode)
}
return &recurly.Billing{
FirstName: "Foo",
LastName: "Bar",
}
}
if b, err := client.Billing.Get(context.Background(), "10"); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(b, &recurly.Billing{
FirstName: "Foo",
LastName: "Bar",
}); diff != "" {
t.Fatal(diff)
} else if !client.Billing.GetInvoked {
t.Fatal("expected Get() to be invoked")
}
}
More Common Setup
If you created your own wrapper type to the library, let's call it PaymentsProvider:
// MyBillingInfo is a custom billing holder. *recurly.Billing is converted to
// *MyBillingInfo by combining FirstName and LastName into a single Name field.
type MyBillingInfo{
Name string
}
type PaymentsProvider interface {
GetBilling(ctx context.Context, id int) (*MyBillingInfo, error)
}
// Provider implements PaymentsProvider.
type Provider struct {
Client *recurly.Client
}
func New(subdomain, key string) *Provider {
return &Provider{Client: recurly.NewClient(subdomain, key)}
}
// GetBilling calls Recurly and converts *recurly.BillingInfo to *MyBillingInfo.
func (p *Provider) GetBilling(ctx context.Context, id int) (*MyBillingInfo, error) {
// Retrieve billing info from Recurly.
b, err := p.Client.Billing.Get(ctx, strconv.Atoi(id))
if err != nil {
return nil, err
} else if b == nil {
return nil, nil
}
// Convert to MyBillingInfo.
return &MyBillingInfo{
Name: fmt.Sprintf("%s %s", b.FirstName, b.LastName),
}
}
Then in your test suite you might configure your own wrapper similar to mock.Client
package foo_test
import (
"context"
"github.com/your-project/foo"
"github.com/blacklightcms/recurly/mock"
)
// Provider is a test wrapper for foo.Provider.
type Provider struct {
*foo.Provider
Billing mock.Billing
}
// NewProvider returns a new test provider.
func NewProvider() *Provider {
// Init Provider.
p := &Provider{Provider: foo.New("foo", "bar")}
// Point Recurly Client's Billing Service to your mock
// attached to p.
p.Provider.Client.Billing = &p.Billing
return p
}
func TestBilling(t *testing.T) {
// Init test Provider.
p := NewProvider()
// Mock Recurly's response
p.Billing.OnGet = func(ctx context.Context, id int) (*recurly.Billing, error) {
return &recurly.Billing{
FirstName: "Verena",
LastName: "Example",
}
}
// Call your provider and assert *MyBillingInfo
if b, err := p.GetBilling(context.Background(), 10); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(b, &foo.MyBillingInfo{
Name: "Verena Example",
}); diff != "" {
t.Fatal(diff)
} else if !p.Billing.GetInvoked {
t.Fatal("expected Get() to be invoked")
}
}
See examples for more.
*/
package mock