-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikidump_test.go
115 lines (101 loc) · 2.83 KB
/
wikidump_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
package wikidump
import (
"context"
"crypto/sha1"
"encoding/base64"
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"testing"
"time"
)
func TestUnit(t *testing.T) {
testingFilename := "usergroupstable"
w, err := Latest("", "en", testingFilename)
if err != nil {
t.Error("Latest returns ", err)
}
if d := w.Date(); d.Before(time.Now().Add(-time.Hour * 24 * 365)) {
t.Error("Date is invalid", d)
}
next := w.Open(testingFilename)
r, err := next(context.Background())
if err != nil {
t.Error("Open iterator returns ", err)
}
defer r.Close()
_, err = csv.NewReader(SQL2CSV(r)).ReadAll()
if err != nil {
t.Error("ReadAll on csv returns ", err)
}
if err = r.Close(); err != nil {
t.Error("Closing returns ", err)
}
if _, err = next(context.Background()); err != io.EOF {
t.Error("Open should return only one file for "+testingFilename+" while it returns ", err)
}
}
func TestOpen(t *testing.T) {
ffi := make([]fileInfo, 0, len(name2MyInfo))
for name, info := range name2MyInfo {
ffi = append(ffi, fileInfo{"http://" + address + name, info.SHA1})
}
tDump := Wikidump{map[string][]fileInfo{"helloword": ffi}, "", time.Now()}
next := tDump.Open("helloword")
r, err := next(context.Background())
for ; err == nil; r, err = next(context.Background()) {
defer r.Close()
data, err := ioutil.ReadAll(r)
closeErr := r.Close()
switch {
case err != nil:
t.Error("Open iterator returns ", err)
case string(data) != helloword:
t.Error("Data should be " + helloword + " but it's " + string(data))
case closeErr != nil:
t.Error("Closing returns ", closeErr)
}
}
if err != nil && err != io.EOF {
t.Error("Open iterator returns ", err)
}
next = tDump.Open("nothing")
if _, err := next(context.Background()); err == nil {
t.Error("Error should be not null")
}
}
const helloword = "Hello, World!"
const address = ":8080"
var name2MyInfo = map[string]myInfo{
"/helloword.7z": base642MyInfo("N3q8ryccAAT5z0JlEQAAAAAAAABqAAAAAAAAACkoIPIBAAxIZWxsbywgV29ybGQhAAEEBgABCREA" +
"BwsBAAEhIQEADA0ACAoB0MNK7AAABQEZDAAAAAAAAAAAAAAAABEfAGgAZQBsAGwAbwB3AG8AcgBs" +
"AGQALgB0AHgAdAAAABkEAAAAABQKAQCAOPxYCNPTARUGAQAggKSBAAA="),
"/helloword.bz2": base642MyInfo("QlpoOTFBWSZTWebY/t8AAAGXgGAEAEAAgAYEkAAgACIDIyEAMLKAWt5D7xdyRThQkObY/t8="),
"/helloword.gz": base642MyInfo("H4sICNV10FoAA2hlbGxvd29ybGQudHh0APNIzcnJ11EIzy/KSVEEANDDSuwNAAAA"),
}
type myInfo struct {
Data []byte
SHA1 string
}
func base642MyInfo(s string) myInfo {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return myInfo{data, fmt.Sprintf("%x", sha1.Sum(data))}
}
func TestMain(m *testing.M) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(name2MyInfo[r.URL.Path].Data)
})
go func() {
err := http.ListenAndServe(address, nil)
if err != nil {
panic(err)
}
}()
os.Exit(m.Run())
}