@@ -3,6 +3,7 @@ package jsonapi
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
8
"io"
8
9
"reflect"
@@ -341,75 +342,175 @@ func TestUnmarshalSetsAttrs(t *testing.T) {
341
342
}
342
343
}
343
344
344
- func TestUnmarshalParsesISO8601 (t * testing.T ) {
345
- payload := & OnePayload {
346
- Data : & Node {
347
- Type : "timestamps" ,
348
- Attributes : map [string ]interface {}{
349
- "timestamp" : "2016-08-17T08:27:12Z" ,
345
+ func TestUnmarshal_Times (t * testing.T ) {
346
+ aTime := time .Date (2016 , 8 , 17 , 8 , 27 , 12 , 0 , time .UTC )
347
+
348
+ for _ , tc := range []struct {
349
+ desc string
350
+ inputPayload * OnePayload
351
+ wantErr bool
352
+ verification func (tm * TimestampModel ) error
353
+ }{
354
+ // Default:
355
+ {
356
+ desc : "default_byValue" ,
357
+ inputPayload : & OnePayload {
358
+ Data : & Node {
359
+ Type : "timestamps" ,
360
+ Attributes : map [string ]interface {}{
361
+ "defaultv" : aTime .Unix (),
362
+ },
363
+ },
364
+ },
365
+ verification : func (tm * TimestampModel ) error {
366
+ if ! tm .DefaultV .Equal (aTime ) {
367
+ return errors .New ("times not equal!" )
368
+ }
369
+ return nil
350
370
},
351
371
},
352
- }
353
-
354
- in := bytes .NewBuffer (nil )
355
- json .NewEncoder (in ).Encode (payload )
356
-
357
- out := new (Timestamp )
358
-
359
- if err := UnmarshalPayload (in , out ); err != nil {
360
- t .Fatal (err )
361
- }
362
-
363
- expected := time .Date (2016 , 8 , 17 , 8 , 27 , 12 , 0 , time .UTC )
364
-
365
- if ! out .Time .Equal (expected ) {
366
- t .Fatal ("Parsing the ISO8601 timestamp failed" )
367
- }
368
- }
369
-
370
- func TestUnmarshalParsesISO8601TimePointer (t * testing.T ) {
371
- payload := & OnePayload {
372
- Data : & Node {
373
- Type : "timestamps" ,
374
- Attributes : map [string ]interface {}{
375
- "next" : "2016-08-17T08:27:12Z" ,
372
+ {
373
+ desc : "default_byPointer" ,
374
+ inputPayload : & OnePayload {
375
+ Data : & Node {
376
+ Type : "timestamps" ,
377
+ Attributes : map [string ]interface {}{
378
+ "defaultp" : aTime .Unix (),
379
+ },
380
+ },
381
+ },
382
+ verification : func (tm * TimestampModel ) error {
383
+ if ! tm .DefaultP .Equal (aTime ) {
384
+ return errors .New ("times not equal!" )
385
+ }
386
+ return nil
376
387
},
377
388
},
378
- }
379
-
380
- in := bytes .NewBuffer (nil )
381
- json .NewEncoder (in ).Encode (payload )
382
-
383
- out := new (Timestamp )
384
-
385
- if err := UnmarshalPayload (in , out ); err != nil {
386
- t .Fatal (err )
387
- }
388
-
389
- expected := time .Date (2016 , 8 , 17 , 8 , 27 , 12 , 0 , time .UTC )
390
-
391
- if ! out .Next .Equal (expected ) {
392
- t .Fatal ("Parsing the ISO8601 timestamp failed" )
393
- }
394
- }
395
-
396
- func TestUnmarshalInvalidISO8601 (t * testing.T ) {
397
- payload := & OnePayload {
398
- Data : & Node {
399
- Type : "timestamps" ,
400
- Attributes : map [string ]interface {}{
401
- "timestamp" : "17 Aug 16 08:027 MST" ,
389
+ {
390
+ desc : "default_invalid" ,
391
+ inputPayload : & OnePayload {
392
+ Data : & Node {
393
+ Type : "timestamps" ,
394
+ Attributes : map [string ]interface {}{
395
+ "defaultv" : "not a timestamp!" ,
396
+ },
397
+ },
402
398
},
399
+ wantErr : true ,
403
400
},
404
- }
405
-
406
- in := bytes .NewBuffer (nil )
407
- json .NewEncoder (in ).Encode (payload )
408
-
409
- out := new (Timestamp )
401
+ // ISO 8601:
402
+ {
403
+ desc : "iso8601_byValue" ,
404
+ inputPayload : & OnePayload {
405
+ Data : & Node {
406
+ Type : "timestamps" ,
407
+ Attributes : map [string ]interface {}{
408
+ "iso8601v" : "2016-08-17T08:27:12Z" ,
409
+ },
410
+ },
411
+ },
412
+ verification : func (tm * TimestampModel ) error {
413
+ if ! tm .ISO8601V .Equal (aTime ) {
414
+ return errors .New ("times not equal!" )
415
+ }
416
+ return nil
417
+ },
418
+ },
419
+ {
420
+ desc : "iso8601_byPointer" ,
421
+ inputPayload : & OnePayload {
422
+ Data : & Node {
423
+ Type : "timestamps" ,
424
+ Attributes : map [string ]interface {}{
425
+ "iso8601p" : "2016-08-17T08:27:12Z" ,
426
+ },
427
+ },
428
+ },
429
+ verification : func (tm * TimestampModel ) error {
430
+ if ! tm .ISO8601P .Equal (aTime ) {
431
+ return errors .New ("times not equal!" )
432
+ }
433
+ return nil
434
+ },
435
+ },
436
+ {
437
+ desc : "iso8601_invalid" ,
438
+ inputPayload : & OnePayload {
439
+ Data : & Node {
440
+ Type : "timestamps" ,
441
+ Attributes : map [string ]interface {}{
442
+ "iso8601v" : "not a timestamp" ,
443
+ },
444
+ },
445
+ },
446
+ wantErr : true ,
447
+ },
448
+ // RFC 3339
449
+ {
450
+ desc : "rfc3339_byValue" ,
451
+ inputPayload : & OnePayload {
452
+ Data : & Node {
453
+ Type : "timestamps" ,
454
+ Attributes : map [string ]interface {}{
455
+ "rfc3339v" : "2016-08-17T08:27:12Z" ,
456
+ },
457
+ },
458
+ },
459
+ verification : func (tm * TimestampModel ) error {
460
+ if got , want := tm .RFC3339V , aTime ; got != want {
461
+ return fmt .Errorf ("got %v, want %v" , got , want )
462
+ }
463
+ return nil
464
+ },
465
+ },
466
+ {
467
+ desc : "rfc3339_byPointer" ,
468
+ inputPayload : & OnePayload {
469
+ Data : & Node {
470
+ Type : "timestamps" ,
471
+ Attributes : map [string ]interface {}{
472
+ "rfc3339p" : "2016-08-17T08:27:12Z" ,
473
+ },
474
+ },
475
+ },
476
+ verification : func (tm * TimestampModel ) error {
477
+ if got , want := * tm .RFC3339P , aTime ; got != want {
478
+ return fmt .Errorf ("got %v, want %v" , got , want )
479
+ }
480
+ return nil
481
+ },
482
+ },
483
+ {
484
+ desc : "rfc3339_invalid" ,
485
+ inputPayload : & OnePayload {
486
+ Data : & Node {
487
+ Type : "timestamps" ,
488
+ Attributes : map [string ]interface {}{
489
+ "rfc3339v" : "not a timestamp" ,
490
+ },
491
+ },
492
+ },
493
+ wantErr : true ,
494
+ },
495
+ } {
496
+ t .Run (tc .desc , func (t * testing.T ) {
497
+ // Serialize the OnePayload using the standard JSON library.
498
+ in := bytes .NewBuffer (nil )
499
+ if err := json .NewEncoder (in ).Encode (tc .inputPayload ); err != nil {
500
+ t .Fatal (err )
501
+ }
410
502
411
- if err := UnmarshalPayload (in , out ); err != ErrInvalidISO8601 {
412
- t .Fatalf ("Expected ErrInvalidISO8601, got %v" , err )
503
+ out := & TimestampModel {}
504
+ err := UnmarshalPayload (in , out )
505
+ if got , want := (err != nil ), tc .wantErr ; got != want {
506
+ t .Fatalf ("UnmarshalPayload error: got %v, want %v" , got , want )
507
+ }
508
+ if tc .verification != nil {
509
+ if err := tc .verification (out ); err != nil {
510
+ t .Fatal (err )
511
+ }
512
+ }
513
+ })
413
514
}
414
515
}
415
516
0 commit comments