-
Notifications
You must be signed in to change notification settings - Fork 44
/
pages_meta_test.go
224 lines (196 loc) · 5.91 KB
/
pages_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 cleanUpPageMeta(t *testing.T, page *wordpress.Page, metaId int) {
// note: Need to pass in `force=true` param in order to delete page meta
deletedMeta, resp, body, err := page.Meta().Delete(metaId, "force=true")
if err != nil {
t.Errorf("Failed to clean up new page: %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 TestPagesMeta_InvalidCall(t *testing.T) {
// User is not allowed to call create wordpress.Page object manually to retrieve PageMetaCollection
// A proper API call would inject the right PageMetaCollection, Client and other goodies into a page,
// allowing user to call page.Meta()
invalidPage := wordpress.Page{}
invalidMeta := invalidPage.Meta()
if invalidMeta != nil {
t.Error("Expected meta to be nil, %v", invalidMeta)
}
}
func TestPagesMetaList_NoParams(t *testing.T) {
wp := initTestClient()
page := getAnyOnePage(t, wp)
meta, resp, body, err := page.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 TestPagesMetaCreate(t *testing.T) {
wp := initTestClient()
// get a page
page := getAnyOnePage(t, wp)
// create meta for retrieved page
m := wordpress.Meta{
Key: "testKey",
Value: "testValue",
}
newMeta, resp, body, err := page.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
cleanUpPageMeta(t, page, newMeta.ID)
}
func TestPagesMetaGet(t *testing.T) {
wp := initTestClient()
// get a page
page := getAnyOnePage(t, wp)
// create meta for retrieved page
m := wordpress.Meta{
Key: "testKey",
Value: "testValue",
}
newMeta, resp, body, err := page.Meta().Create(&m)
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
// get meta by id for retrieved page
metaID := newMeta.ID
meta, resp, body, err := page.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
cleanUpPageMeta(t, page, newMeta.ID)
}
func TestPagesMetaGet_Lazy(t *testing.T) {
wp := initTestClient()
// get a page so we can have a valid Page ID and we can create a test meta to get
page := getAnyOnePage(t, wp)
pageID := page.ID
// create meta for retrieved page
m := wordpress.Meta{
Key: "testKey",
Value: "testValue",
}
newMeta, resp, _, _ := page.Meta().Create(&m)
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
metaID := newMeta.ID
// Use Pages().Entity(pageID) to retrieve meta in one API call
lazyMeta, resp, body, err := wp.Pages().Entity(pageID).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 TestPagesMetaUpdate(t *testing.T) {
wp := initTestClient()
// get a page
page := getAnyOnePage(t, wp)
// create meta for retrieved page
m := wordpress.Meta{
Key: "testKey",
Value: "testValue",
}
newMeta, resp, body, err := page.Meta().Create(&m)
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
// get meta by id for retrieved page
metaID := newMeta.ID
meta, resp, body, err := page.Meta().Get(metaID, nil)
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
// update meta by id for retrieved page
meta.Value = "newTestValue"
updatedMeta, resp, body, err := page.Meta().Update(meta.ID, meta)
if err != nil {
t.Errorf("Failed to update page 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
cleanUpPageMeta(t, page, newMeta.ID)
}