-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
121 lines (114 loc) · 2.65 KB
/
search_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
package main
import (
"github.com/go-test/deep"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestSearch(t *testing.T) {
tests := []struct {
name, path string
pathFrequency Frequency
indexType IndexType
expected SearchResult
}{
{
"Verona",
"/tests/rnj/sceneI_30.0.html",
Frequency{
"/tests/rnj/sceneI_30.0.html": 1,
},
0,
SearchResult{
TotalDocsSearched: 1,
Found: true,
},
},
{
"Benvolio",
"/tests/rnj/sceneI_30.1.html",
Frequency{
"/tests/rnj/sceneI_30.1.html": 26,
},
0,
SearchResult{
TotalDocsSearched: 1,
Found: true,
},
},
{
"Romeo",
"/tests/rnj/",
Frequency{
"/tests/rnj/sceneI_30.0.html": 2,
"/tests/rnj/sceneI_30.1.html": 22,
"/tests/rnj/sceneI_30.3.html": 2,
"/tests/rnj/sceneI_30.4.html": 17,
"/tests/rnj/sceneI_30.5.html": 15,
"/tests/rnj/sceneII_30.2.html": 42,
"/tests/rnj/": 200,
"/tests/rnj/sceneI_30.2.html": 15,
"/tests/rnj/sceneII_30.0.html": 3,
"/tests/rnj/sceneII_30.1.html": 10,
"/tests/rnj/sceneII_30.3.html": 13,
},
0,
SearchResult{
TotalDocsSearched: 11,
Found: true,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
urlPath := r.URL.Path
if urlPath == "/tests/rnj/" {
urlPath += "/index.html"
}
filePath := "./documents" + urlPath
reader, err := os.Open(filePath)
if err != nil {
t.Logf("Could not open file %q\n", filePath)
w.WriteHeader(http.StatusNotFound)
return
}
bytes, err := io.ReadAll(reader)
_, err = w.Write(bytes)
if err != nil {
log.Fatalf("Error writing response: %v", err.Error())
}
}))
defer ts.Close()
testURL, err := clean(ts.URL, test.path)
if err != nil {
t.Fatalf("Could not clean URL: %v\n", test.path)
}
var index Index
if test.indexType == Memory {
index = newMemoryIndex()
} else {
index = newDBIndex("test.db", true, nil)
}
crawl(&index, testURL, true)
got := index.search(test.name)
expectedTermFrequency := make(Frequency)
for path, freq := range test.pathFrequency {
cleanedUrl, err := clean(ts.URL, path)
if err != nil {
t.Fatalf("Could not clean URL: %v\n", path)
}
expectedTermFrequency[cleanedUrl] += freq
}
test.expected.TermFrequency = expectedTermFrequency
test.expected.UrlMap = got.UrlMap
dropDatabase("test.db")
if diff := deep.Equal(got, &test.expected); diff != nil {
t.Error(diff)
}
})
}
}