-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_test.go
More file actions
503 lines (458 loc) · 13.4 KB
/
Copy pathencode_test.go
File metadata and controls
503 lines (458 loc) · 13.4 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package jsonstat
import (
"bytes"
"encoding/json"
"errors"
"testing"
)
// This file tests the JSON-stat 2.0 wire-format encoder. Round-trip fidelity
// is the central guarantee: decode → encode → decode must produce a Dataset
// that is semantically equivalent to the original. We verify equivalence by
// re-walking the cube and comparing cell-by-cell, not by byte-equality (real
// fixtures vary in whitespace and key order).
//
// Beyond round-trip, each value form (dense/sparse), each status form
// (uniform/array/object), and the standalone-dimension and collection classes
// are exercised individually.
// ---------- Round-trip across canonical fixtures ----------
func TestEncodeRoundTripDatasets(t *testing.T) {
// Every fixture in here must be a dataset-class document. (Collections
// and standalone dimensions have their own tests below.)
fixtures := []string{
"oecd.json",
"canada.json",
"galicia.json",
"hierarchy.json",
"us-gsp.json",
"us-unr.json",
"order.json",
"sparse.json",
"status-array.json",
}
for _, name := range fixtures {
t.Run(name, func(t *testing.T) {
orig := mustDecode(t, loadFixture(t, name))
origDS, err := orig.SingleDataset()
if err != nil {
t.Fatalf("fixture %s is not a single-dataset document: %v", name, err)
}
encoded, err := MarshalDataset(origDS)
if err != nil {
t.Fatalf("MarshalDataset(%s): %v", name, err)
}
// Sanity: encoded output must be valid JSON.
if !json.Valid(encoded) {
t.Fatalf("MarshalDataset(%s) produced invalid JSON: %s", name, encoded)
}
// Re-decode the encoded output.
doc2, err := DecodeBytes(encoded)
if err != nil {
t.Fatalf("re-decode of encoded %s: %v\nencoded=%s", name, err, encoded)
}
ds2, err := doc2.SingleDataset()
if err != nil {
t.Fatalf("re-decoded doc has no dataset: %v", err)
}
assertDatasetsEquivalent(t, name, origDS, ds2)
})
}
}
// assertDatasetsEquivalent compares two Datasets by walking every cell. This
// is the strongest semantic check we can make without a generated equality
// method: the value/status stores, dimensions, and roles must all agree.
func assertDatasetsEquivalent(t *testing.T, label string, a, b *Dataset) {
t.Helper()
// Structural fields.
if len(a.ID) != len(b.ID) {
t.Fatalf("%s: ID length mismatch: %v vs %v", label, a.ID, b.ID)
}
for i := range a.ID {
if a.ID[i] != b.ID[i] {
t.Errorf("%s: ID[%d]: %q vs %q", label, i, a.ID[i], b.ID[i])
}
}
if len(a.Size) != len(b.Size) {
t.Fatalf("%s: Size length mismatch: %v vs %v", label, a.Size, b.Size)
}
for i := range a.Size {
if a.Size[i] != b.Size[i] {
t.Errorf("%s: Size[%d]: %d vs %d", label, i, a.Size[i], b.Size[i])
}
}
if a.N() != b.N() {
t.Fatalf("%s: N mismatch: %d vs %d", label, a.N(), b.N())
}
// Value form must round-trip (dense stays dense, sparse stays sparse).
if a.ValueFormat() != b.ValueFormat() {
t.Errorf("%s: ValueFormat changed: %s vs %s", label, a.ValueFormat(), b.ValueFormat())
}
// Status form must round-trip.
if a.StatusFormat() != b.StatusFormat() {
t.Errorf("%s: StatusFormat changed: %s vs %s", label, a.StatusFormat(), b.StatusFormat())
}
// Walk every cell and compare Value, Status, Missing.
if a.N() == 0 {
return
}
cellsA, _ := a.CellsSlice()
cellsB, _ := b.CellsSlice()
if len(cellsA) != len(cellsB) {
t.Fatalf("%s: cell counts differ: %d vs %d", label, len(cellsA), len(cellsB))
}
mismatches := 0
for i := range cellsA {
if !cellsEqual(cellsA[i], cellsB[i]) {
if mismatches < 5 {
t.Errorf("%s: cell %d differs:\n a=%#v\n b=%#v", label, i, cellsA[i], cellsB[i])
}
mismatches++
}
}
if mismatches > 0 {
t.Errorf("%s: %d/%d cells differ", label, mismatches, len(cellsA))
}
}
// ---------- Dense value round-trip ----------
func TestEncodeDenseValues(t *testing.T) {
// oecd.json: dense 432-cell value array, status is a sparse object.
ds := mustDataset(t, "oecd.json")
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
// Spot-check a value in the encoded JSON to confirm it's a dense array.
var probe map[string]json.RawMessage
if err := json.Unmarshal(encoded, &probe); err != nil {
t.Fatal(err)
}
valRaw := probe["value"]
if len(valRaw) == 0 || valRaw[0] != '[' {
t.Errorf("dense value should encode as array, got %s", valRaw)
}
// Check a specific known value.
want := `5.943826289`
if !bytes.Contains(valRaw, []byte(want)) {
t.Errorf("encoded value missing %s: %s", want, valRaw)
}
}
// ---------- Sparse value round-trip ----------
func TestEncodeSparseValues(t *testing.T) {
// sparse.json: 4 keys {0,5,13,20}.
ds := mustDataset(t, "sparse.json")
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
var probe map[string]json.RawMessage
if err := json.Unmarshal(encoded, &probe); err != nil {
t.Fatal(err)
}
valRaw := probe["value"]
if len(valRaw) == 0 || valRaw[0] != '{' {
t.Errorf("sparse value should encode as object, got %s", valRaw)
}
// Re-decode and confirm the four values survive.
doc2, err := DecodeBytes(encoded)
if err != nil {
t.Fatal(err)
}
ds2 := doc2.Dataset
c, _ := ds2.CellByFlat(13)
if c.Value != 110 {
t.Errorf("flat 13 after round-trip: got %v, want 110", c.Value)
}
}
// ---------- Status forms ----------
func TestEncodeStatusUniform(t *testing.T) {
// canada.json has a string status. After round-trip, status form must
// still be "uniform".
ds := mustDataset(t, "canada.json")
if ds.StatusFormat() != "uniform" {
t.Fatalf("canada status form pre-encode: got %s, want uniform", ds.StatusFormat())
}
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
doc2, err := DecodeBytes(encoded)
if err != nil {
t.Fatal(err)
}
if got := doc2.Dataset.StatusFormat(); got != "uniform" {
t.Errorf("canada status form post-encode: got %s, want uniform", got)
}
}
func TestEncodeStatusArray(t *testing.T) {
ds := mustDataset(t, "status-array.json")
if ds.StatusFormat() != "array" {
t.Fatalf("status-array form pre-encode: got %s, want array", ds.StatusFormat())
}
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
doc2, err := DecodeBytes(encoded)
if err != nil {
t.Fatal(err)
}
if got := doc2.Dataset.StatusFormat(); got != "array" {
t.Errorf("status-array form post-encode: got %s, want array", got)
}
// Spot-check a status value at flat 4 ("p").
c, _ := doc2.Dataset.CellByFlat(4)
if c.Status != "p" {
t.Errorf("flat 4 status after round-trip: got %q, want 'p'", c.Status)
}
}
func TestEncodeStatusObject(t *testing.T) {
// oecd.json has object-form status.
ds := mustDataset(t, "oecd.json")
if ds.StatusFormat() != "object" {
t.Fatalf("oecd status form pre-encode: got %s, want object", ds.StatusFormat())
}
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
doc2, err := DecodeBytes(encoded)
if err != nil {
t.Fatal(err)
}
if got := doc2.Dataset.StatusFormat(); got != "object" {
t.Errorf("oecd status form post-encode: got %s, want object", got)
}
}
func TestEncodeStatusAbsent(t *testing.T) {
// us-gsp.json has no status property.
ds := mustDataset(t, "us-gsp.json")
if ds.StatusFormat() != "none" {
t.Fatalf("us-gsp status form: got %s, want none", ds.StatusFormat())
}
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
var probe map[string]json.RawMessage
if err := json.Unmarshal(encoded, &probe); err != nil {
t.Fatal(err)
}
if _, present := probe["status"]; present {
t.Errorf("us-gsp encoded a status property, want none")
}
}
// ---------- Hierarchy ----------
func TestEncodeHierarchy(t *testing.T) {
ds := mustDataset(t, "hierarchy.json")
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
doc2, err := DecodeBytes(encoded)
if err != nil {
t.Fatal(err)
}
ds2 := doc2.Dataset
// Find the dimension with a hierarchy and confirm it survived.
anyHierarchy := false
for _, id := range ds.ID {
if ds.Dimensions[id].HasHierarchy() {
anyHierarchy = true
if !ds2.Dimensions[id].HasHierarchy() {
t.Errorf("dim %s: hierarchy lost on round-trip", id)
}
}
}
if !anyHierarchy {
t.Errorf("no hierarchy found in source dataset")
}
}
// ---------- Roles ----------
func TestEncodeRoles(t *testing.T) {
// oecd.json has role {time:[year], geo:[area], metric:[concept]}.
ds := mustDataset(t, "oecd.json")
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
doc2, err := DecodeBytes(encoded)
if err != nil {
t.Fatal(err)
}
ds2 := doc2.Dataset
for _, r := range []Role{RoleTime, RoleGeo, RoleMetric} {
if !ds2.HasRole(r) {
t.Errorf("role %s lost on round-trip", r)
}
}
// Verify role-to-dim mapping.
for _, r := range []Role{RoleTime, RoleGeo, RoleMetric} {
got := ds2.Role[r]
want := ds.Role[r]
if len(got) != len(want) {
t.Errorf("role %s mapping len: got %d, want %d", r, len(got), len(want))
continue
}
for i := range got {
if got[i] != want[i] {
t.Errorf("role %s[%d]: got %q, want %q", r, i, got[i], want[i])
}
}
}
}
// ---------- Collection ----------
func TestEncodeCollection(t *testing.T) {
doc := mustDecode(t, loadFixture(t, "collection.json"))
if !doc.IsCollection() {
t.Fatalf("collection.json is not a collection")
}
encoded, err := MarshalCollection(doc.Collection)
if err != nil {
t.Fatal(err)
}
if !json.Valid(encoded) {
t.Fatalf("encoded collection is invalid JSON: %s", encoded)
}
doc2, err := DecodeBytes(encoded)
if err != nil {
t.Fatalf("re-decode: %v", err)
}
if !doc2.IsCollection() {
t.Fatalf("re-decoded doc is not a collection")
}
if got := len(doc2.Collection.Items); got != len(doc.Collection.Items) {
t.Errorf("items count: got %d, want %d", got, len(doc.Collection.Items))
}
}
// ---------- Standalone dimension ----------
func TestEncodeStandaloneDimension(t *testing.T) {
doc := mustDecode(t, loadFixture(t, "dimension-only.json"))
if !doc.IsDimension() {
t.Fatalf("dimension-only.json is not a standalone dimension")
}
encoded, err := MarshalDimension(doc.Dimension)
if err != nil {
t.Fatal(err)
}
if !json.Valid(encoded) {
t.Fatalf("encoded dimension is invalid JSON: %s", encoded)
}
doc2, err := DecodeBytes(encoded)
if err != nil {
t.Fatalf("re-decode: %v", err)
}
if !doc2.IsDimension() {
t.Fatalf("re-decoded doc is not a standalone dimension")
}
// Verify the role survived.
if doc2.Dimension.Role != doc.Dimension.Role {
t.Errorf("dim role: got %q, want %q", doc2.Dimension.Role, doc.Dimension.Role)
}
}
// ---------- Pre-2.0 bundle rejection ----------
func TestEncodeBundleRejected(t *testing.T) {
doc := mustDecode(t, loadFixture(t, "bundle-pre2.0.json"))
if !doc.IsBundle {
t.Fatalf("bundle-pre2.0.json is not a bundle")
}
_, err := MarshalDocument(doc)
if err == nil {
t.Fatalf("MarshalDocument on bundle: want error, got nil")
}
if !errors.Is(err, ErrUnsupportedVersion) {
t.Errorf("MarshalDocument on bundle: got %v, want ErrUnsupportedVersion", err)
}
}
// ---------- MarshalJSON interfaces ----------
func TestDatasetMarshalJSON(t *testing.T) {
ds := mustDataset(t, "oecd.json")
b, err := ds.MarshalJSON()
if err != nil {
t.Fatal(err)
}
if !json.Valid(b) {
t.Fatalf("Dataset.MarshalJSON produced invalid JSON")
}
}
func TestEncodeBytes(t *testing.T) {
doc := mustDecode(t, loadFixture(t, "oecd.json"))
b, err := EncodeBytes(doc)
if err != nil {
t.Fatal(err)
}
if !json.Valid(b) {
t.Fatalf("EncodeBytes produced invalid JSON")
}
}
func TestEncodeToBuffer(t *testing.T) {
doc := mustDecode(t, loadFixture(t, "status-array.json"))
var buf bytes.Buffer
if err := Encode(&buf, doc); err != nil {
t.Fatal(err)
}
if !json.Valid(buf.Bytes()) {
t.Fatalf("Encode produced invalid JSON")
}
}
// ---------- Nil handling ----------
func TestEncodeNilDataset(t *testing.T) {
_, err := MarshalDataset(nil)
if err == nil {
t.Fatalf("MarshalDataset(nil): want error")
}
}
func TestEncodeNilDocument(t *testing.T) {
_, err := MarshalDocument(nil)
if err == nil {
t.Fatalf("MarshalDocument(nil): want error")
}
}
func TestEncodeEmptyDocument(t *testing.T) {
doc := &Document{}
_, err := MarshalDocument(doc)
if err == nil {
t.Fatalf("MarshalDocument(empty): want error")
}
}
// ---------- Property order check ----------
func TestEncodeKeyOrder(t *testing.T) {
// The canonical JSON-stat property order for a dataset starts with
// "class". This is a smoke check that the ordered writer works.
ds := mustDataset(t, "oecd.json")
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
// The first key after the opening brace must be "class".
prefix := `{"class":`
if !bytes.HasPrefix(encoded, []byte(prefix)) {
t.Errorf("encoded dataset does not start with %q: got %s", prefix, encoded[:40])
}
}
// ---------- Category index form: array ----------
func TestEncodeCategoryIndexArray(t *testing.T) {
// The encoder always emits category.index as an array. Re-decoding must
// work because decodeCategoryIndex accepts arrays.
ds := mustDataset(t, "oecd.json")
encoded, err := MarshalDataset(ds)
if err != nil {
t.Fatal(err)
}
var probe struct {
Dimension map[string]struct {
Category struct {
Index []string `json:"index"`
} `json:"category"`
} `json:"dimension"`
}
if err := json.Unmarshal(encoded, &probe); err != nil {
t.Fatal(err)
}
areaDim := probe.Dimension["area"]
if len(areaDim.Category.Index) == 0 {
t.Errorf("area dimension category.index is not an array")
}
// The area dimension in oecd.json has size 36.
if len(areaDim.Category.Index) != 36 {
t.Errorf("area category.index length: got %d, want 36", len(areaDim.Category.Index))
}
}