2015-07-06 16:40:43 -04:00
|
|
|
package jsonapi
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestUnmarshalSetsId(t *testing.T) {
|
2015-07-06 17:41:53 -04:00
|
|
|
in := samplePayload()
|
2015-07-06 16:40:43 -04:00
|
|
|
out := new(Blog)
|
|
|
|
|
|
|
|
if err := UnmarshalJsonApiPayload(in, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Id != 5 {
|
|
|
|
t.Fatalf("Did not set Id on dst interface")
|
|
|
|
}
|
2015-07-06 17:41:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalSetsAttrs(t *testing.T) {
|
|
|
|
in := samplePayload()
|
|
|
|
out := new(Blog)
|
|
|
|
|
|
|
|
if err := UnmarshalJsonApiPayload(in, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
|
|
|
if out.CreatedAt.IsZero() {
|
|
|
|
t.Fatalf("Did not parse time")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.ViewCount != 1000 {
|
|
|
|
t.Fatalf("View count not properly serialized")
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 17:41:53 -04:00
|
|
|
|
2015-07-07 12:52:38 -04:00
|
|
|
func samplePayload() *JsonApiOnePayload {
|
|
|
|
return &JsonApiOnePayload{
|
2015-07-06 17:41:53 -04:00
|
|
|
Data: &JsonApiNode{
|
|
|
|
Id: "5",
|
|
|
|
Type: "blogs",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "New blog",
|
|
|
|
"created_at": 1436216820,
|
|
|
|
"view_count": 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|