This repository was archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcdxj_test.go
More file actions
147 lines (124 loc) · 3.35 KB
/
cdxj_test.go
File metadata and controls
147 lines (124 loc) · 3.35 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
package cdxj
import (
"fmt"
"testing"
"time"
"github.com/datatogether/warc"
)
func TestSurtURL(t *testing.T) {
cases := []struct {
in, out string
err error
}{
// {"cnn.com/world", "(com,cnn,)/world", nil},
{"http://cnn.com/world", "(com,cnn,)/world", nil},
{"https://cnn.com/world", "(com,cnn,)/world", nil},
{"ftp://cnn.co.uk/world?foo=bar", "(uk,co,cnn,)/world?foo=bar", nil},
}
for i, c := range cases {
got, err := SurtURL(c.in)
if err != c.err {
t.Errorf("case %d error mismatch: %s != %s", i, c.err, err)
continue
}
if c.out != got {
t.Errorf("case %d mismatch. expected: '%s', got: '%s'", i, c.out, got)
continue
}
}
}
func TestUnSurtURL(t *testing.T) {
cases := []struct {
in, out string
err error
}{
{"(com,cnn,)/world", "cnn.com/world", nil},
{"com,cnn,)/world>", "cnn.com/world", nil},
{"com,cnn)/world", "cnn.com/world", nil},
{"(uk,co,cnn,)/world?foo=bar", "cnn.co.uk/world?foo=bar", nil},
}
for i, c := range cases {
got, err := UnSurtURL(c.in)
if err != c.err {
t.Errorf("case %d error mismatch: %s != %s", i, c.err, err)
continue
}
if c.out != got {
t.Errorf("case %d mismatch. expected: '%s', got: '%s'", i, c.out, got)
continue
}
}
}
func TestUnSurtPath(t *testing.T) {
cases := []struct {
in, out string
err error
}{
{"(com,cnn,)/world", "/world", nil},
{"com,cnn,)/world>", "/world", nil},
{"com,cnn)/world", "/world", nil},
{"com,cnn)", "/", nil},
{"(uk,co,cnn,)/world?foo=bar", "/world?foo=bar", nil},
}
for i, c := range cases {
got, err := UnSurtPath(c.in)
if err != c.err {
t.Errorf("case %d error mismatch: %s != %s", i, c.err, err)
continue
}
if c.out != got {
t.Errorf("case %d mismatch. expected: '%s', got: '%s'", i, c.out, got)
continue
}
}
}
func TestRecordUnmarshalCDXJ(t *testing.T) {
cases := []struct {
data []byte
out *Record
err error
}{
{[]byte(`(com,cnn,)/world 2015-09-03T13:27:52Z response {"a" : 0, "b" : "b", "c" : false }`), &Record{"cnn.com/world", time.Date(2015, time.September, 3, 13, 27, 52, 0, time.UTC), warc.RecordTypeResponse, map[string]interface{}{"a": 0, "b": "b", "c": false}}, nil},
}
for i, c := range cases {
r := &Record{}
if err := r.UnmarshalCDXJ(c.data); err != c.err {
t.Errorf("case %d error mismatch: %s != %s", i, c.err, err)
continue
}
if err := CompareRecords(c.out, r); err != nil {
t.Errorf("case %d record mismatch: %s", i, err.Error())
continue
}
}
}
func CompareRecordSlices(a, b []*Record) error {
if len(a) != len(b) {
return fmt.Errorf("record slice length mismatch: %d != %d", len(a), len(b))
}
for i, ar := range a {
br := b[i]
if err := CompareRecords(ar, br); err != nil {
return fmt.Errorf("record %d mismatch: %s", i, err.Error())
}
}
return nil
}
func CompareRecords(a, b *Record) error {
if a == nil && b != nil || b == nil && a != nil {
return fmt.Errorf("nil mistmatch: %s,%s", a, b)
} else if a == nil && b == nil {
return nil
}
if a.URI != b.URI {
return fmt.Errorf("record uri mismatch: %s != %s", a.URI, b.URI)
}
if !a.Timestamp.Equal(b.Timestamp) {
return fmt.Errorf("timestamp mismatch: %s != %s", a.Timestamp.String(), a.Timestamp.String())
}
if a.RecordType != b.RecordType {
return fmt.Errorf("record type mismatch: %s != %s", a.RecordType, b.RecordType)
}
// TODO - compare json field
return nil
}