-
Notifications
You must be signed in to change notification settings - Fork 8
/
meta_test.go
357 lines (315 loc) · 10.3 KB
/
meta_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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package dataset
import (
"bytes"
"encoding/json"
"io/ioutil"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func compareMetas(a, b *Meta) string {
return cmp.Diff(a, b, cmpopts.IgnoreUnexported(Meta{}))
}
func TestMetaDropDerivedValues(t *testing.T) {
md := &Meta{
Path: "/ipfs/QmHash",
Qri: "oh you know it's qri",
}
md.DropDerivedValues()
if !cmp.Equal(md, &Meta{}, cmp.AllowUnexported(Meta{})) {
t.Errorf("expected dropping a commit of only derived values to be empty")
}
}
func TestMetaAssign(t *testing.T) {
// TODO - expand test to check all fields
cases := []struct {
in *Meta
}{
{&Meta{Path: "/a"}},
{&Meta{AccessURL: "foo"}},
{&Meta{DownloadURL: "foo"}},
{&Meta{ReadmeURL: "foo"}},
{&Meta{AccrualPeriodicity: "1W"}},
{&Meta{Citations: []*Citation{{Email: "foo"}}}},
{&Meta{Description: "foo"}},
{&Meta{HomeURL: "foo"}},
{&Meta{Identifier: "foo"}},
{&Meta{License: &License{Type: "foo"}}},
{&Meta{Version: "foo"}},
{&Meta{Keywords: []string{"foo"}}},
{&Meta{Contributors: []*User{{Email: "foo"}}}},
{&Meta{Language: []string{"stuff"}}},
{&Meta{Theme: []string{"stuff"}}},
{&Meta{meta: map[string]interface{}{"foo": "bar"}}},
}
for i, c := range cases {
got := &Meta{}
got.Assign(c.in)
if diff := compareMetas(c.in, got); diff != "" {
t.Errorf("case %d result mismatch: (-want +got):\n%s", i, diff)
continue
}
}
expect := &Meta{
Title: "Final Title",
Description: "Final Description",
AccessURL: "AccessURL",
}
got := &Meta{
Title: "Overwrite Me",
Description: "Nope",
}
got.Assign(&Meta{
Title: "Final Title",
Description: "Final Description",
AccessURL: "AccessURL",
})
if diff := compareMetas(expect, got); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
got.Assign(nil, nil)
if diff := compareMetas(expect, got); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
emptyDs := &Meta{}
emptyDs.Assign(expect)
if diff := compareMetas(expect, emptyDs); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
}
func TestMetaSet(t *testing.T) {
cases := []struct {
key string
val interface{}
err string
meta *Meta
}{
{" TITLE ", 0, "type must be a string", nil},
{" TITLE", "title", "", &Meta{Title: "title"}},
{" TITLE", nil, "", &Meta{}},
{"accessurl", 0, "type must be a string", nil},
{"accessurl", "foo", "", &Meta{AccessURL: "foo"}},
{"accrualperiodicity", 0, "type must be a string", nil},
{"accrualperiodicity", "foo", "", &Meta{AccrualPeriodicity: "foo"}},
{"description", 0, "type must be a string", nil},
{"description", "foo", "", &Meta{Description: "foo"}},
{"downloadurl", 0, "type must be a string", nil},
{"downloadurl", "foo", "", &Meta{DownloadURL: "foo"}},
{"homeurl", 0, "type must be a string", nil},
{"homeurl", "foo", "", &Meta{HomeURL: "foo"}},
{"identifier", 0, "type must be a string", nil},
{"identifier", "foo", "", &Meta{Identifier: "foo"}},
{"readmeurl", 0, "type must be a string", nil},
{"readmeurl", "foo", "", &Meta{ReadmeURL: "foo"}},
{"title", 0, "type must be a string", nil},
{"title", "foo", "", &Meta{Title: "foo"}},
{"version", 0, "type must be a string", nil},
{"version", "foo", "", &Meta{Version: "foo"}},
{"keywords", 0, "type must be a set of strings", nil},
{"keywords", nil, "", &Meta{}},
{"keywords", []interface{}{0}, "index 0: type must be a string", nil},
{"keywords", []interface{}{"foo"}, "", &Meta{Keywords: []string{"foo"}}},
{"language", 0, "type must be a set of strings", nil},
{"language", []interface{}{"foo"}, "", &Meta{Language: []string{"foo"}}},
{"theme", 0, "type must be a set of strings", nil},
{"theme", []interface{}{"foo"}, "", &Meta{Theme: []string{"foo"}}},
{"citations", 0, "citation: expected interface slice", nil},
{"citations", []interface{}{0}, "parsing citations index 0: expected map[string]interface{}", nil},
{"citations", []interface{}{
map[string]interface{}{
"name": "foo",
"url": "bar",
},
}, "", &Meta{Citations: []*Citation{&Citation{Name: "foo", URL: "bar"}}}},
{"contributors", 0, "contributors: expected interface slice", nil},
{"contributors", []interface{}{0}, "parsing contributors index 0: expected map[string]interface{}", nil},
{"contributors", []interface{}{
map[string]interface{}{
"id": "steve",
"email": "email@steve.com",
}}, "", &Meta{Contributors: []*User{&User{ID: "steve", Email: "email@steve.com"}}}},
{"license", 0, "expected map[string]interface{}", nil},
{"license", map[string]interface{}{
"type": "foo",
"url": "bar",
}, "", &Meta{License: &License{Type: "foo", URL: "bar"}}},
{"@id", "foo", "", &Meta{meta: map[string]interface{}{"@id": "foo"}}},
}
for i, c := range cases {
m := &Meta{}
err := m.Set(c.key, c.val)
if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
t.Errorf("case %d (%s) error mismatch. expected: '%s', got: '%s'", i, c.key, c.err, err)
continue
}
if c.meta != nil {
if diff := compareMetas(m, c.meta); diff != "" {
t.Errorf("case %d (%s) meta mismatch (-want +got):\n%s", i, c.key, diff)
continue
}
}
}
}
func TestMetaMarshalJSON(t *testing.T) {
cases := []struct {
in *Meta
out []byte
err error
}{
{&Meta{}, []byte(`{"qri":"md:0"}`), nil},
{AirportCodes.Meta, []byte(`{"citations":[{"name":"Our Airports","url":"http://ourairports.com/data/"}],"homeURL":"http://www.ourairports.com/","license":{"type":"PDDL-1.0"},"qri":"md:0","title":"Airport Codes"}`), nil},
{Hours.Meta, []byte(`{"accessURL":"https://example.com/not/a/url","downloadURL":"https://example.com/not/a/url","qri":"md:0","readmeURL":"/ipfs/notahash","title":"hours"}`), nil},
}
for i, c := range cases {
got, err := c.in.MarshalJSON()
if err != c.err {
t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
continue
}
if !bytes.Equal(c.out, got) {
t.Errorf("case %d mismatch. expected != got: %s != %s", i, string(c.out), string(got))
continue
}
}
data, err := ioutil.ReadFile("testdata/datasets/complete.json")
if err != nil {
t.Errorf("error reading dataset file: %s", err.Error())
return
}
ds := &Meta{}
if err := json.Unmarshal(data, &ds); err != nil {
t.Errorf("error unmarshaling json: %s", err.Error())
return
}
if _, err := ds.MarshalJSON(); err != nil {
t.Errorf("error marshaling back to json: %s", err.Error())
return
}
strbytes, err := json.Marshal(&Meta{Path: "/path/to/dataset"})
if err != nil {
t.Errorf("unexpected string marshal error: %s", err.Error())
return
}
if !bytes.Equal(strbytes, []byte("\"/path/to/dataset\"")) {
t.Errorf("marshal strbyte interface byte mismatch: %s != %s", string(strbytes), "\"/path/to/dataset\"")
}
}
func TestMetaUnmarshalJSON(t *testing.T) {
cases := []struct {
FileName string
result *Meta
err error
}{
{"testdata/metadata/airport-codes.json", AirportCodes.Meta, nil},
{"testdata/metadata/continent-codes.json", ContinentCodes.Meta, nil},
{"testdata/metadata/hours.json", Hours.Meta, nil},
}
for i, c := range cases {
data, err := ioutil.ReadFile(c.FileName)
if err != nil {
t.Errorf("case %d couldn't read file: %s", i, err.Error())
}
ds := &Meta{}
if err := json.Unmarshal(data, ds); err != c.err {
t.Errorf("case %d error mismatch (expected: '%s', got): '%s'", i, c.err, err)
continue
}
if diff := compareMetas(ds, c.result); diff != "" {
t.Errorf("case %d resource comparison error (-want +got):\n%s", i, diff)
continue
}
}
strds := &Meta{}
path := "/path/to/dataset"
if err := json.Unmarshal([]byte(`"`+path+`"`), strds); err != nil {
t.Errorf("unmarshal string path error: %s", err.Error())
return
}
if strds.Path != path {
t.Errorf("unmarshal didn't set proper path: %s != %s", path, strds.Path)
return
}
// confirm direct marshalling empty meta component to-and-from JSON doesn't
// have side effects
md := &Meta{}
data, err := md.MarshalJSON()
if err != nil {
t.Fatal(err)
}
md = &Meta{}
if err := json.Unmarshal(data, md); err != nil {
t.Fatal(err)
}
if md.meta != nil {
t.Errorf("expected deserialized meta to be nil")
}
}
func TestCitationDecode(t *testing.T) {
c := &Citation{}
if err := c.Decode(map[string]interface{}{"name": 0}); err == nil {
t.Errorf("expected error")
}
if err := c.Decode(map[string]interface{}{"url": 0}); err == nil {
t.Errorf("expected error")
}
if err := c.Decode(map[string]interface{}{"email": 0}); err == nil {
t.Errorf("expected error")
}
}
func TestLicenseDecode(t *testing.T) {
l := &License{}
if err := l.Decode(map[string]interface{}{"type": 0}); err == nil {
t.Errorf("expected error")
}
if err := l.Decode(map[string]interface{}{"url": 0}); err == nil {
t.Errorf("expected error")
}
}
func TestUserDecode(t *testing.T) {
u := &User{}
if err := u.Decode(map[string]interface{}{"id": 0}); err == nil {
t.Errorf("expected error")
}
if err := u.Decode(map[string]interface{}{"name": 0}); err == nil {
t.Errorf("expected error")
}
if err := u.Decode(map[string]interface{}{"email": 0}); err == nil {
t.Errorf("expected error")
}
}
func TestLicense(t *testing.T) {
}
func TestAccrualDuration(t *testing.T) {
cases := []struct {
in string
expect time.Duration
}{
{"", time.Duration(0)},
{"R/P10Y", time.Duration(315360000000000000)},
{"R/P4Y", time.Duration(126144000000000000)},
{"R/P1Y", time.Duration(31536000000000000)},
{"R/P2M", time.Duration(25920000000000000)},
{"R/P3.5D", time.Duration(345600000000000)},
{"R/P1D", time.Duration(86400000000000)},
{"R/P2W", time.Duration(1209600000000000)},
{"R/P6M", time.Duration(15552000000000000)},
{"R/P2Y", time.Duration(63072000000000000)},
{"R/P3Y", time.Duration(94608000000000000)},
{"R/P0.33W", time.Duration(201600000000000)},
{"R/P0.33M", time.Duration(864000000000000)},
{"R/PT1S", time.Duration(1000000000)},
{"R/P1M", time.Duration(2592000000000000)},
{"R/P3M", time.Duration(4505142857142857)},
{"R/P0.5M", time.Duration(1296000000000000)},
{"R/P4M", time.Duration(7884000000000000)},
{"R/P1W", time.Duration(604800000000000)},
{"R/PT1H", time.Duration(3600000000000)},
}
for i, c := range cases {
got := AccuralDuration(c.in)
if got != c.expect {
t.Errorf("case %d error. expected: %d, got: %d", i, c.expect, got)
}
}
}