-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-wp_test.go
324 lines (276 loc) · 8.54 KB
/
export-wp_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main // _test
import (
"bytes"
"encoding/xml"
"io/ioutil"
"log"
"os"
"strings"
"testing"
)
var (
testXML string
)
func TestMain(m *testing.M) {
fileReader, err := os.OpenFile("testWpExport.xml", os.O_RDONLY, os.ModeCharDevice)
if err != nil {
log.Fatalf("could not read test payload: %v", err)
}
bts, err := ioutil.ReadAll(fileReader)
if err != nil {
log.Fatalf("could not read xml: %v", err)
}
testXML = string(bts)
err = fileReader.Close()
if err != nil {
log.Fatalf("could not read xml: %v", err)
}
os.Exit(m.Run())
}
func Test_parseXML(t *testing.T) {
var doc rss
err := xml.Unmarshal([]byte(testXML), &doc)
if err != nil {
log.Fatalf("could not parse xml: %v", err)
}
if len(doc.Items) != 4 {
t.Errorf("expected to find 4 items, found %d", len(doc.Items))
t.FailNow()
}
if !strings.Contains(doc.Items[0].Encodeds[0].Data, "http://plazamoyua.files.wordpress.com/2007/01/moyua6.jpg") {
t.Errorf("missing content for item1: %s", doc.Items[0].Encodeds[0].Data)
}
if doc.Items[0].Author != "soil" {
t.Errorf("unexpected author for item1: %s", doc.Items[0].Author)
}
if doc.Items[0].Title != "moyua6.jpg" {
t.Errorf("unexpected title for item1: %s", doc.Items[0].Title)
}
if doc.Items[0].PubDate != "Sat, 20 Jan 2007 06:36:31 +0000" {
t.Errorf("unexpected PubDate for item1: %s", doc.Items[0].PubDate)
}
if doc.Items[0].PostType != "attachment" {
t.Errorf("unexpected PubDate for item1: %s", doc.Items[0].PubDate)
}
}
func Test_cleanContent(t *testing.T) {
var doc rss
err := xml.Unmarshal([]byte(testXML), &doc)
if err != nil {
log.Fatalf("could not parse xml: %v", err)
}
if len(doc.Items) != 4 {
t.Errorf("expected to find 4 items, found %d", len(doc.Items))
t.FailNow()
}
renderer := contentRenderer{
transformContent: cleanContent,
}
var buff bytes.Buffer
err = renderer.toMarkdown(doc.Items[0], &buff)
if err != nil {
t.Errorf("could not convert post to markdown: %v", err)
}
md := buff.String()
if strings.Contains(md, "plazamoyua.files.wordpress.com") {
t.Errorf("should have eliminated media url reference")
}
if strings.Contains(md, "http://plazamoyua.files.wordpress.com/2007/01/moyua6.jpg") ||
!strings.Contains(md, "/media/2007/01/moyua6.jpg") {
t.Errorf("should have eliminated media url reference")
}
if strings.Contains(md, "https://plazamoyua.com/category/fertilizacion-co2/") ||
!strings.Contains(md, "/categories/fertilizacion-co2/") {
t.Errorf("should have converted category reference")
}
if strings.Contains(md, "https://plazamoyua.com/tag/1010/") ||
!strings.Contains(md, "/tags/1010/") {
t.Errorf("should have converted tag reference")
}
t.Log(md)
}
func Test_generateMD(t *testing.T) {
var doc rss
err := xml.Unmarshal([]byte(testXML), &doc)
if err != nil {
log.Fatalf("could not parse xml: %v", err)
}
if len(doc.Items) != 4 {
t.Errorf("expected to find 4 items, found %d", len(doc.Items))
t.FailNow()
}
renderer := contentRenderer{
transformContent: func(in string) string { return in },
}
var buff bytes.Buffer
err = renderer.toMarkdown(doc.Items[2], &buff)
if err != nil {
t.Errorf("could not convert post to markdown: %v", err)
}
md := buff.String()
expectedWords := []string{"---", "date:", "title:", "author:", "slug:", doc.Items[2].Encodeds[0].Data}
for _, word := range expectedWords {
t.Run("testing-for-"+word, func(t *testing.T) {
if !strings.Contains(md, word) {
t.Errorf("missing %s from markdown", word)
}
})
}
}
func Test_generateHTMLinMD(t *testing.T) {
var doc rss
err := xml.Unmarshal([]byte(testXML), &doc)
if err != nil {
log.Fatalf("could not parse xml: %v", err)
}
if len(doc.Items) != 4 {
t.Errorf("expected to find 4 items, found %d", len(doc.Items))
t.FailNow()
}
renderer := contentRenderer{
transformContent: func(in string) string { return in },
}
var buff bytes.Buffer
err = renderer.toMarkdown(doc.Items[3], &buff)
if err != nil {
t.Errorf("could not convert post to markdown: %v", err)
}
md := buff.String()
extracts := []string{
`title: "Las plataformas de hielo de la Antártida, estables. Lástima por los \"fans\" de Wilkins."`,
`author: "plazaeme"`,
`slug: "las-plataformas-de-hielo-de-la-antartida-estables-lo-siento-por-fans-de-wilkins"`,
`url: "/2009/06/16/las-plataformas-de-hielo-de-la-antartida-estables-lo-siento-por-fans-de-wilkins/"`,
`categories: ["algoreros", "calentamiento-global", "cambio-climatico"]`,
`Científicos de la <em><strong>Western Australia's Curtin University of Technology</strong></em> están usando sensores acústicos,`,
}
for _, extract := range extracts {
if !strings.Contains(md, extract) {
t.Errorf("missing content: %s", extract)
}
}
t.Log(md)
}
func Test_threadComments(t *testing.T) {
var doc rss
err := xml.Unmarshal([]byte(testXML), &doc)
if err != nil {
log.Fatalf("could not parse xml: %v", err)
}
if len(doc.Items) != 4 {
t.Errorf("expected to find 4 items, found %d", len(doc.Items))
t.FailNow()
}
comments := doc.Items[2].Comments
if 3 != len(comments) {
t.Errorf("unexpected number of comments: %d. Wanted 3", len(comments))
}
commentThreads := threadComments(comments)
if 1 != len(commentThreads) {
t.Errorf("expected 1 thread, got %d", len(commentThreads))
t.FailNow()
}
if commentThreads[0].ID != commentThreads[0].Children[0].ParentID ||
commentThreads[0].Children[0].ID != commentThreads[0].Children[0].Children[0].ParentID {
t.Errorf("unexpected thread %v", commentThreads[0])
}
}
func Test_renderComments(t *testing.T) {
var doc rss
err := xml.Unmarshal([]byte(testXML), &doc)
if err != nil {
t.Fatal(err)
}
if len(doc.Items) != 4 {
t.Fatal("expected to find 4 items")
}
comments := doc.Items[2].Comments
if 3 != len(comments) {
t.Fatal("expected 3 comments")
}
commentThreads := threadComments(comments)
renderer := contentRenderer{
transformContent: func(in string) string { return in },
}
html, err := renderer.threadToHTML(commentThreads[0])
if err != nil {
t.Fatal(err)
}
expectedFragments := []string{
`<div class="comment">`,
`<span class="author">MOY</span>`,
`paco <i>pecho</i> chico rico`,
`insultaba como loco`,
`<span class="author">Foo</span>`,
}
for _, frag := range expectedFragments {
if !strings.Contains(string(html), frag) {
t.Errorf("expected to find string %s", frag)
}
}
}
func Test_parseCategoriesTags(t *testing.T) {
var doc rss
err := xml.Unmarshal([]byte(testXML), &doc)
if err != nil {
log.Fatalf("could not parse xml: %v", err)
}
if len(doc.Items) != 4 {
t.Errorf("expected to find 4 items, found %d", len(doc.Items))
t.FailNow()
}
if len(doc.Items[3].Categories) != 4 {
t.Errorf("expected 4 categories, found %d", len(doc.Items[3].Categories))
}
var tags, cats int
for _, ct := range doc.Items[3].Categories {
switch ct.Domain {
case "category":
cats++
case "post_tag":
tags++
default:
t.Errorf("unknown domain: %s", ct.Domain)
}
}
if tags != 1 {
t.Errorf("expected 1 tag, found %d", tags)
}
if cats != 3 {
t.Errorf("expected 3 categories, found %d", tags)
}
expected := category{
XMLName: xml.Name{Space: "", Local: "category"},
Domain: "post_tag",
NiceName: "cambio-climatico",
Data: "Cambio Climático",
}
if expected != doc.Items[3].Categories[3] {
t.Errorf("unexpected tag: %#v", doc.Items[3].Categories[3])
}
}
func TestLinkify(t *testing.T) {
in := `Y luego pasó a ser esta dirección. Hace poco funcionaba:
http://reode.blogspot.com/ y http://spreadsheets.google.com/pub?key=tl4jqSPuZ3CE5wxyDVwjvMA&single=true&gid=0&output=html también,
Pero ya no <a href="http://amazon.com">amazon</a>.
Creo que era:
http://ciudadanos-nada-mas.blogspot.com/
Ahora se pueden ver los resultados provisionales con gran comodidad en una hoja de cálculo que ha preparado "Producciones Críticas":
http://spreadsheets.google.com/pub?key=tl4jqSPuZ3CE5wxyDVwjvMA&single=true&gid=0&output=html`
out, err := linkifyText(in)
if err != nil {
t.Fatalf("could not linkify: %v", err)
}
frags := []string{
`<a href="http://reode.blogspot.com/">http://reode.blogspot.com/</a> y `,
`Pero ya no <a href="http://amazon.com">amazon</a>.`,
`<a href="http://ciudadanos-nada-mas.blogspot.com/">http://ciudadanos-nada-mas.blogspot.com/</a>`,
`<a href="http://spreadsheets.google.com/pub?key=tl4jqSPuZ3CE5wxyDVwjvMA&single=true&gid=0&output=html">http://spreadsheets.google.com/pub?key=tl4jqSPuZ3CE5wxyDVwjvMA&single=true&gid=0&output=html</a>`,
}
for _, frag := range frags {
if !strings.Contains(out, frag) {
t.Errorf("expected to find string %s", frag)
}
}
t.Log(string(out))
}