forked from sogko/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 2
/
posts_meta_test.go
224 lines (196 loc) · 5.91 KB
/
posts_meta_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package wordpress_test
import (
"github.com/sogko/go-wordpress"
"net/http"
"testing"
)
func cleanUpPostMeta(t *testing.T, post *wordpress.Post, metaId int) {
// note: Need to pass in `force=true` param in order to delete post meta
deletedMeta, resp, body, err := post.Meta().Delete(metaId, "force=true")
if err != nil {
t.Errorf("Failed to clean up new post: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if deletedMeta.Message != "Deleted meta" {
t.Errorf("Unexpected response to deleted meta: %v", deletedMeta.Message)
}
}
func TestPostsMeta_InvalidCall(t *testing.T) {
// User is not allowed to call create wordpress.Post object manually to retrieve PostMetaCollection
// A proper API call would inject the right PostMetaCollection, Client and other goodies into a post,
// allowing user to call post.Meta()
invalidPost := wordpress.Post{}
invalidMeta := invalidPost.Meta()
if invalidMeta != nil {
t.Error("Expected meta to be nil, %v", invalidMeta)
}
}
func TestPostsMetaList_NoParams(t *testing.T) {
wp := initTestClient()
post := getAnyOnePost(t, wp)
meta, resp, body, err := post.Meta().List(nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("Should not return nil body")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if meta == nil {
t.Errorf("Should not return nil meta")
}
}
func TestPostsMetaCreate(t *testing.T) {
wp := initTestClient()
// get a post
post := getAnyOnePost(t, wp)
// create meta for retrieved post
m := wordpress.Meta{
Key: "testKey",
Value: "testValue",
}
newMeta, resp, body, err := post.Meta().Create(&m)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
if newMeta == nil {
t.Errorf("newMeta should not be nil")
}
if newMeta.Key != m.Key {
t.Errorf("newMeta.Key should be the same, %v != %v", newMeta.Key, m.Key)
}
if newMeta.Value != m.Value {
t.Errorf("newMeta.Value should be the same, %v != %v", newMeta.Value, m.Key)
}
// clean up
cleanUpPostMeta(t, post, newMeta.ID)
}
func TestPostsMetaGet(t *testing.T) {
wp := initTestClient()
// get a post
post := getAnyOnePost(t, wp)
// create meta for retrieved post
m := wordpress.Meta{
Key: "testKey",
Value: "testValue",
}
newMeta, resp, body, err := post.Meta().Create(&m)
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
// get meta by id for retrieved post
metaID := newMeta.ID
meta, resp, body, err := post.Meta().Get(metaID, nil)
if err != nil {
t.Errorf("Failed to get meta: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("meta.Value should be the same, %v != %v", meta.Value, m.Value)
}
if meta.ID != metaID {
t.Errorf("meta.ID should be the same, %v != %v", meta.ID, metaID)
}
if newMeta.Key != m.Key {
t.Errorf("meta.Key should be the same, %v != %v", meta.Key, m.Key)
}
if newMeta.Value != m.Value {
t.Errorf("meta.Value should be the same, %v != %v", meta.Value, m.Value)
}
// clean up
cleanUpPostMeta(t, post, newMeta.ID)
}
func TestPostsMetaGet_Lazy(t *testing.T) {
wp := initTestClient()
// get a post so we can have a valid Post ID and we can create a test meta to get
post := getAnyOnePost(t, wp)
postID := post.ID
// create meta for retrieved post
m := wordpress.Meta{
Key: "testKey",
Value: "testValue",
}
newMeta, resp, _, _ := post.Meta().Create(&m)
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
metaID := newMeta.ID
// Use Posts().Entity(postID) to retrieve meta in one API call
lazyMeta, resp, body, err := wp.Posts().Entity(postID).Meta().Get(metaID, nil)
if err != nil {
t.Errorf("Failed to lazy-get meta: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("meta.Value should be the same, %v != %v", lazyMeta.Value, m.Value)
}
if lazyMeta.ID != metaID {
t.Errorf("meta.ID should be the same, %v != %v", lazyMeta.ID, metaID)
}
if lazyMeta.Key != m.Key {
t.Errorf("meta.Key should be the same, %v != %v", lazyMeta.Key, m.Key)
}
if lazyMeta.Value != m.Value {
t.Errorf("meta.Value should be the same, %v != %v", lazyMeta.Value, m.Value)
}
}
func TestPostsMetaUpdate(t *testing.T) {
wp := initTestClient()
// get a post
post := getAnyOnePost(t, wp)
// create meta for retrieved post
m := wordpress.Meta{
Key: "testKey",
Value: "testValue",
}
newMeta, resp, body, err := post.Meta().Create(&m)
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
// get meta by id for retrieved post
metaID := newMeta.ID
meta, resp, body, err := post.Meta().Get(metaID, nil)
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
// update meta by id for retrieved post
meta.Value = "newTestValue"
updatedMeta, resp, body, err := post.Meta().Update(meta.ID, meta)
if err != nil {
t.Errorf("Failed to update post meta: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if updatedMeta.ID != meta.ID {
t.Errorf("updatedMeta.ID should be the same, %v != %v", updatedMeta.ID, meta.ID)
}
if updatedMeta.Key != meta.Key {
t.Errorf("updatedMeta.Key should be the same, %v != %v", updatedMeta.Key, meta.Key)
}
if updatedMeta.Value != meta.Value {
t.Errorf("updatedMeta.Value should be the same, %v != %v", updatedMeta.Value, meta.Value)
}
// clean up
cleanUpPostMeta(t, post, newMeta.ID)
}