Add test for set id

This commit is contained in:
Sam Woodard 2015-07-08 13:11:03 -07:00
parent 7430fd0b0c
commit c887cfc585
2 changed files with 34 additions and 10 deletions

View File

@ -26,18 +26,18 @@ func TestMalformedTag(t *testing.T) {
} }
} }
//func TestUnmarshalSetsId(t *testing.T) { func TestUnmarshalSetsId(t *testing.T) {
//in := samplePayload() in := samplePayloadWithId()
//out := new(Blog) out := new(Blog)
//if err := UnmarshalJsonApiPayload(in, out); err != nil { if err := UnmarshalJsonApiPayload(in, out); err != nil {
//t.Fatal(err) t.Fatal(err)
//} }
//if out.Id != 0 { if out.Id != 2 {
//t.Fatalf("Did not set Id on dst interface") t.Fatalf("Did not set Id on dst interface")
//} }
//} }
func TestUnmarshalSetsAttrs(t *testing.T) { func TestUnmarshalSetsAttrs(t *testing.T) {
out, err := unmarshalSamplePayload() out, err := unmarshalSamplePayload()
@ -173,3 +173,23 @@ func samplePayload() io.Reader {
return out return out
} }
func samplePayloadWithId() io.Reader {
payload := &JsonApiOnePayload{
Data: &JsonApiNode{
Id: "2",
Type: "blogs",
Attributes: map[string]interface{}{
"title": "New blog",
"created_at": 1436216820,
"view_count": 1000,
},
},
}
out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
return out
}

View File

@ -82,6 +82,10 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) {
tag := structField.Tag.Get("jsonapi") tag := structField.Tag.Get("jsonapi")
if tag == "" {
return false
}
args := strings.Split(tag, ",") args := strings.Split(tag, ",")
if len(args) != 2 { if len(args) != 2 {