-
Notifications
You must be signed in to change notification settings - Fork 1
/
strong_resources_test.go
153 lines (110 loc) · 3.8 KB
/
strong_resources_test.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package stretchr
/*
This file demonstrates how you might write strongly-typed resources
to make your Stretchr code easier (and compiler friendly).
*/
import (
"github.com/stretchr/sdk-go/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
// PersonResource represents an example of a strongly-typed Resource object.
//
// In your own code, you can follow this pattern:
//
// type MyStrongType struct {
// *stretchr.Resource
// }
type PersonResource struct {
/*
Embedding a Resource type pointer as an anonymous field allows
this type to act like a Resource in every necessary way.
*/
*Resource
}
// MakePersonResource makes a new person resource with the given ID.
//
// It also configures the actual Resource part of this object by assigning a
// new Resource to the .Resource variable.
func MakePersonResource(id string) *PersonResource {
p := new(PersonResource)
p.Resource = MakeResourceAt(Path("people", id))
return p
}
// Name is an example of a strongly typed and managed getter.
func (p *PersonResource) Name() string {
return p.GetString("name")
}
// SetName is an example of a srongly typed and managed setter.
func (p *PersonResource) SetName(value string) *PersonResource {
p.Set("name", value)
return p
}
// Age is an example of a strongly typed and managed getter that does some
// casting between int, and the underlying float64 number type.
func (p *PersonResource) Age() int {
return int(p.GetNumber("age"))
}
// SetAge is an exmaple of a strongly typed and managed setter that does some
// casting between int, and the underlying float64 number type.
func (p *PersonResource) SetAge(value int) *PersonResource {
p.Set("age", value)
return p
}
/*
This is how you create an instance of your strongly
typed resource.
*/
func TestStrongResources_MakingPersonResource(t *testing.T) {
p := new(PersonResource)
p.Resource = MakeResourceAt(Path("people", "123"))
assert.Equal(t, "people/123", p.ResourcePath())
}
/*
This is how you would use your strongly-typed and managed getters
and setters.
*/
func TestStrongResources_StrongGettersAndSetters(t *testing.T) {
p := new(PersonResource)
p.Resource = MakeResourceAt("people/123")
p.SetName("Mat").SetAge(30)
assert.Equal(t, "Mat", p.Name())
assert.Equal(t, int(30), p.Age())
// ensure also that the actual data is set in the resource
assert.Equal(t, "Mat", p.Resource.ResourceData().Get("name").Data())
assert.Equal(t, 30, p.Resource.ResourceData().Get("age").Data())
}
/*
This shows how you can use your strongly typed resource in place
of the normal Resource object with zero effort
The mockedTransporter stuff is just so our call to "Create" doesn't
actually try to hit any servers.
*/
func TestStrongResources_UsingTheResource(t *testing.T) {
// make a session object
session := NewSession("project", "company", "apiKey")
// don't make real requests
mockedTransporter := new(api.MockedTransporter)
session.SetTransporter(mockedTransporter)
mockedTransporter.On("MakeRequest", mock.Anything).Return(nil, assert.AnError)
// make a person resource
p := MakePersonResource("123")
//... and use it as normal
session.At(p.ResourcePath()).Create(p)
}
/*
This is how, after reading a Resource, you are able to easily cast it
to your strongly typed resource.
We recommend you wrap this kind of thing in a function, in this case:
func ReadPerson(id string) *PersonResource { ... }
*/
func TestStrongResources_ReadingAResource(t *testing.T) {
loadedResource := MakeResourceAt("people/123")
loadedResource.Set("name", "Mat")
loadedResource.Set("age", float64(30))
stronglyTypedResource := MakePersonResource(loadedResource.ID())
stronglyTypedResource.Resource = loadedResource
assert.Equal(t, "Mat", stronglyTypedResource.Name())
assert.Equal(t, 30, stronglyTypedResource.Age())
}