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) {
//in := samplePayload()
//out := new(Blog)
func TestUnmarshalSetsId(t *testing.T) {
in := samplePayloadWithId()
out := new(Blog)
//if err := UnmarshalJsonApiPayload(in, out); err != nil {
//t.Fatal(err)
//}
if err := UnmarshalJsonApiPayload(in, out); err != nil {
t.Fatal(err)
}
//if out.Id != 0 {
//t.Fatalf("Did not set Id on dst interface")
//}
//}
if out.Id != 2 {
t.Fatalf("Did not set Id on dst interface")
}
}
func TestUnmarshalSetsAttrs(t *testing.T) {
out, err := unmarshalSamplePayload()
@ -173,3 +173,23 @@ func samplePayload() io.Reader {
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")
if tag == "" {
return false
}
args := strings.Split(tag, ",")
if len(args) != 2 {