-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawl_test.go
More file actions
220 lines (212 loc) · 5.62 KB
/
Copy pathcrawl_test.go
File metadata and controls
220 lines (212 loc) · 5.62 KB
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
package main
import (
"flag"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
type test struct {
url string
words_want map[string]uint
paths_want map[string]struct{}
data []byte
}
func TestCrawl(t *testing.T) {
tests := map[string]test{
"/test-data/project01/home.html": {
url: "/test-data/project01/home.html",
words_want: map[string]uint{"simpl": 1, "href": 1, "style": 1},
paths_want: map[string]struct{}{
"/test-data/project01/home.html": {},
"/test-data/project01/simple.html": {},
"/test-data/project01/href.html": {},
"/test-data/project01/style.html": {},
"/test-data/project01/deep.html": {},
},
data: []byte(`
<html>
<body>
<ul>
<li>
<a href="/test-data/project01/simple.html">simple.html</a>
</li>
<li>
<a href="/test-data/project01/href.html">href.html</a>
</li>
<li>
<a href="/test-data/project01/style.html">style.html</a>
</ul>
</body>
</html>
`),
},
"/test-data/project01/simple.html": {
url: "/test-data/project01/simple.html",
words_want: map[string]uint{"link": 1},
paths_want: map[string]struct{}{
"/test-data/project01/simple.html": {},
},
data: []byte(`
<html>
<body>
Hello CS 272, there are no links here.
</body>
</html>
`),
},
"/test-data/project01/href.html": {
url: "/test-data/project01/href.html",
words_want: map[string]uint{"simpl": 2},
paths_want: map[string]struct{}{
"/test-data/project01/simple.html": {},
"/test-data/project01/href.html": {},
},
data: []byte(`
<html>
<body>
For a simple example, see <a href="/test-data/project01/simple.html">simple.html</a>
</body>
</html>
`),
},
"/test-data/project01/style.html": {
url: "/test-data/project01/style.html",
words_want: map[string]uint{"style": 1, "red": 1, "link": 3, "green": 1, "blue": 1, "simpl": 1, "deep": 1, "href": 1},
paths_want: map[string]struct{}{
"/test-data/project01/simple.html": {},
"/test-data/project01/href.html": {},
"/test-data/project01/style.html": {},
"/test-data/project01/deep.html": {},
"/test-data/project01/home.html": {},
},
data: []byte(`
<html>
<head>
<title>Style</title>
<style>
a.red {
color: red;
}
a.green {
color: green;
}
a.blue {
color: blue;
}
</style>
<body>
<p>
Here is a red link to <a class="red" href="/test-data/project01/href.html">href.html</a>
</p>
<p>
And a green link to <a class="green" href="/test-data/project01/simple.html">simple.html</a>
</p>
<p>
And lastly a blue link to <a class="blue" href="/test-data/project01/deep.html">deep.html</a>
</p>
</body>
</html>
`),
},
"/test-data/project01/deep.html": {
url: "/test-data/project01/deep.html",
words_want: map[string]uint{"deep": 1, "circular": 1, "link": 1},
paths_want: map[string]struct{}{
"/test-data/project01/deep.html": {},
"/test-data/project01/home.html": {},
"/test-data/project01/simple.html": {},
"/test-data/project01/href.html": {},
"/test-data/project01/style.html": {},
},
data: []byte(`
<html>
<body>
This deep page contains a circular link to <a href="/test-data/project01/home.html">home.html</a>
</body>
</html>
`),
},
}
// Set up server to deliver correct html for each url path
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, ok := tests[r.URL.Path]
if ok {
fmt.Fprint(w, string(data.data))
return
}
http.NotFound(w, r)
}))
defer ts.Close()
for _, tc := range tests {
t.Run(tc.url, func(t *testing.T) {
// Create stop words list
stop_words, err := CreateStopWordSet("stopwords-en.json")
if err != nil {
t.Fatal("Could not create stop words list")
return
}
// Parse flags to get data_handler type
flag.Parse()
args := flag.Args()
var data_handler DataHandler
if len(args) >= 2 && args[0] == "ondisk" {
var err error
data_handler, err = NewSQLDataBase(args[1])
if err != nil {
data_handler = NewInvIdx()
}
} else {
data_handler = NewInvIdx()
}
t.Cleanup(func() {
data_handler.DeleteDataBase()
})
crawler := NewCrawler(data_handler, stop_words, 0.5)
_, err = crawler.Crawl(
ts.URL+tc.url,
struct {
clean_href int
download int
extract int
}{16, 16, 16},
)
if err != nil {
t.Errorf("Crawl failed: %v", err)
}
hrefs_got, err := data_handler.GetAllDocs()
if err != nil {
t.Fatal("Data handler broke retrieving href map")
}
// Check return map lengths
size, err := data_handler.Size()
if err != nil {
t.Fatal("Data handler broke retieving the number of hrefs")
}
if int(size) != len(tc.paths_want) {
t.Fatalf("Crawl starting from %s got %d hrefs, want %d", tc.url, size, len(tc.paths_want))
}
// Check each expected href
for path := range tc.paths_want {
full_url := ts.URL + path
if _, ok := hrefs_got[full_url]; !ok {
t.Fatalf("Crawl starting from %s missing href: %s", tc.url, full_url)
}
words_got, err := data_handler.GetAllFreq(full_url)
if err != nil {
t.Fatalf("Data handler broke attempting to retrieve the words in %s", full_url)
}
if !reflect.DeepEqual(words_got, tests[path].words_want) {
t.Fatalf(
"Crawl starting from %s: for href %s got:\n%s\nwant:\n%s",
tc.url,
full_url,
MapToString(words_got),
MapToString(tests[path].words_want),
)
}
}
})
}
}