Skip to content

Commit dcc55ce

Browse files
Implement the tests of the transformers.TransformerGroup.TransformLinks() method
1 parent c9dae67 commit dcc55ce

File tree

1 file changed

+161
-1
lines changed

1 file changed

+161
-1
lines changed

extractors/transformers/transformer_group_test.go

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package transformers
22

33
import (
4+
"io/ioutil"
45
"net/http"
6+
"net/http/httptest"
7+
"strings"
58
"testing"
9+
"testing/iotest"
610

711
"github.com/stretchr/testify/assert"
812
"github.com/stretchr/testify/mock"
13+
"github.com/thewizardplusplus/go-crawler/extractors"
914
)
1015

1116
func TestTransformerGroup_TransformLinks(test *testing.T) {
@@ -22,7 +27,162 @@ func TestTransformerGroup_TransformLinks(test *testing.T) {
2227
wantLinks []string
2328
wantErr assert.ErrorAssertionFunc
2429
}{
25-
// TODO: Add test cases.
30+
{
31+
name: "success without transformers",
32+
transformers: nil,
33+
args: args{
34+
links: []string{"http://example.com/1", "http://example.com/2"},
35+
response: &http.Response{
36+
Body: ioutil.NopCloser(strings.NewReader(`
37+
<ul>
38+
<li><a href="http://example.com/1">1</a></li>
39+
<li><a href="http://example.com/2">2</a></li>
40+
</ul>
41+
`)),
42+
Request: httptest.NewRequest(http.MethodGet, "http://example.com/", nil),
43+
},
44+
responseContent: []byte(`
45+
<ul>
46+
<li><a href="http://example.com/1">1</a></li>
47+
<li><a href="http://example.com/2">2</a></li>
48+
</ul>
49+
`),
50+
},
51+
wantLinks: []string{"http://example.com/1", "http://example.com/2"},
52+
wantErr: assert.NoError,
53+
},
54+
{
55+
name: "success with transformers",
56+
transformers: TransformerGroup{
57+
func() extractors.LinkTransformer {
58+
links := []string{"http://example.com/1", "http://example.com/2"}
59+
transformedLinks := []string{
60+
"http://example.com/1/transformed/1",
61+
"http://example.com/2/transformed/1",
62+
}
63+
64+
responseContent := `
65+
<ul>
66+
<li><a href="http://example.com/1">1</a></li>
67+
<li><a href="http://example.com/2">2</a></li>
68+
</ul>
69+
`
70+
response := &http.Response{
71+
Body: ioutil.NopCloser(strings.NewReader(responseContent)),
72+
Request: httptest.NewRequest(http.MethodGet, "http://example.com/", nil),
73+
}
74+
75+
transformer := new(MockLinkTransformer)
76+
transformer.
77+
On("TransformLinks", links, response, []byte(responseContent)).
78+
Return(transformedLinks, nil)
79+
80+
return transformer
81+
}(),
82+
func() extractors.LinkTransformer {
83+
links := []string{
84+
"http://example.com/1/transformed/1",
85+
"http://example.com/2/transformed/1",
86+
}
87+
transformedLinks := []string{
88+
"http://example.com/1/transformed/2",
89+
"http://example.com/2/transformed/2",
90+
}
91+
92+
responseContent := `
93+
<ul>
94+
<li><a href="http://example.com/1">1</a></li>
95+
<li><a href="http://example.com/2">2</a></li>
96+
</ul>
97+
`
98+
response := &http.Response{
99+
Body: ioutil.NopCloser(strings.NewReader(responseContent)),
100+
Request: httptest.NewRequest(http.MethodGet, "http://example.com/", nil),
101+
}
102+
103+
transformer := new(MockLinkTransformer)
104+
transformer.
105+
On("TransformLinks", links, response, []byte(responseContent)).
106+
Return(transformedLinks, nil)
107+
108+
return transformer
109+
}(),
110+
},
111+
args: args{
112+
links: []string{"http://example.com/1", "http://example.com/2"},
113+
response: &http.Response{
114+
Body: ioutil.NopCloser(strings.NewReader(`
115+
<ul>
116+
<li><a href="http://example.com/1">1</a></li>
117+
<li><a href="http://example.com/2">2</a></li>
118+
</ul>
119+
`)),
120+
Request: httptest.NewRequest(http.MethodGet, "http://example.com/", nil),
121+
},
122+
responseContent: func() []byte {
123+
return []byte(`
124+
<ul>
125+
<li><a href="http://example.com/1">1</a></li>
126+
<li><a href="http://example.com/2">2</a></li>
127+
</ul>
128+
`)
129+
}(),
130+
},
131+
wantLinks: []string{
132+
"http://example.com/1/transformed/2",
133+
"http://example.com/2/transformed/2",
134+
},
135+
wantErr: assert.NoError,
136+
},
137+
{
138+
name: "error",
139+
transformers: TransformerGroup{
140+
func() extractors.LinkTransformer {
141+
links := []string{"http://example.com/1", "http://example.com/2"}
142+
143+
responseContent := `
144+
<ul>
145+
<li><a href="http://example.com/1">1</a></li>
146+
<li><a href="http://example.com/2">2</a></li>
147+
</ul>
148+
`
149+
response := &http.Response{
150+
Body: ioutil.NopCloser(strings.NewReader(responseContent)),
151+
Request: httptest.NewRequest(http.MethodGet, "http://example.com/", nil),
152+
}
153+
154+
transformer := new(MockLinkTransformer)
155+
transformer.
156+
On("TransformLinks", links, response, []byte(responseContent)).
157+
Return(nil, iotest.ErrTimeout)
158+
159+
return transformer
160+
}(),
161+
new(MockLinkTransformer),
162+
},
163+
args: args{
164+
links: []string{"http://example.com/1", "http://example.com/2"},
165+
response: &http.Response{
166+
Body: ioutil.NopCloser(strings.NewReader(`
167+
<ul>
168+
<li><a href="http://example.com/1">1</a></li>
169+
<li><a href="http://example.com/2">2</a></li>
170+
</ul>
171+
`)),
172+
Request: httptest.NewRequest(http.MethodGet, "http://example.com/", nil),
173+
},
174+
responseContent: func() []byte {
175+
return []byte(`
176+
<ul>
177+
<li><a href="http://example.com/1">1</a></li>
178+
<li><a href="http://example.com/2">2</a></li>
179+
</ul>
180+
`)
181+
}(),
182+
},
183+
wantLinks: nil,
184+
wantErr: assert.Error,
185+
},
26186
} {
27187
test.Run(data.name, func(test *testing.T) {
28188
gotLinks, gotErr := data.transformers.TransformLinks(

0 commit comments

Comments
 (0)