2015-07-06 16:40:43 -04:00
|
|
|
package jsonapi
|
|
|
|
|
2015-07-07 18:46:20 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-07-08 12:34:37 -04:00
|
|
|
"io"
|
2015-07-08 14:49:36 -04:00
|
|
|
"regexp"
|
2015-07-07 18:46:20 -04:00
|
|
|
"testing"
|
|
|
|
)
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-07-08 14:49:36 -04:00
|
|
|
type BadModel struct {
|
|
|
|
Id int `jsonapi:"primary"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMalformedTag(t *testing.T) {
|
|
|
|
out := new(BadModel)
|
|
|
|
err := UnmarshalJsonApiPayload(samplePayload(), out)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Did not error out with wrong number of arguments in tag")
|
|
|
|
}
|
|
|
|
|
|
|
|
r := regexp.MustCompile(`two few arguments`)
|
|
|
|
|
|
|
|
if !r.Match([]byte(err.Error())) {
|
|
|
|
t.Fatalf("The err was not due two two few arguments in a tag")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 16:11:03 -04:00
|
|
|
func TestUnmarshalSetsId(t *testing.T) {
|
|
|
|
in := samplePayloadWithId()
|
|
|
|
out := new(Blog)
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-07-08 16:11:03 -04:00
|
|
|
if err := UnmarshalJsonApiPayload(in, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-07-08 16:11:03 -04:00
|
|
|
if out.Id != 2 {
|
|
|
|
t.Fatalf("Did not set Id on dst interface")
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 17:41:53 -04:00
|
|
|
|
|
|
|
func TestUnmarshalSetsAttrs(t *testing.T) {
|
2015-07-08 14:49:36 -04:00
|
|
|
out, err := unmarshalSamplePayload()
|
|
|
|
if err != nil {
|
2015-07-06 17:41:53 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-07-08 12:34:37 -04:00
|
|
|
//o := bytes.NewBuffer(nil)
|
|
|
|
//json.NewEncoder(o).Encode(out)
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2015-07-08 12:34:37 -04:00
|
|
|
//fmt.Printf("%s\n", o.Bytes())
|
2015-07-07 18:46:20 -04:00
|
|
|
|
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-08 12:34:37 -04:00
|
|
|
func TestUnmarshalRelationships(t *testing.T) {
|
2015-07-08 14:49:36 -04:00
|
|
|
out, err := unmarshalSamplePayload()
|
|
|
|
if err != nil {
|
2015-07-08 12:34:37 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost == nil {
|
|
|
|
t.Fatalf("Current post was not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost.Title != "Bas" || out.CurrentPost.Body != "Fuubar" {
|
|
|
|
t.Fatalf("Attributes where not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Posts) != 2 {
|
|
|
|
t.Fatalf("There should have been 2 posts")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNestedRelationships(t *testing.T) {
|
2015-07-08 14:49:36 -04:00
|
|
|
out, err := unmarshalSamplePayload()
|
|
|
|
if err != nil {
|
2015-07-08 12:34:37 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost == nil {
|
|
|
|
t.Fatalf("Current post was not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost.Comments == nil {
|
|
|
|
t.Fatalf("Did not materialize nested records, comments")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.CurrentPost.Comments) != 2 {
|
|
|
|
t.Fatalf("Wrong number of comments")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 14:49:36 -04:00
|
|
|
func unmarshalSamplePayload() (*Blog, error) {
|
|
|
|
in := samplePayload()
|
|
|
|
out := new(Blog)
|
|
|
|
|
|
|
|
if err := UnmarshalJsonApiPayload(in, out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2015-07-08 12:34:37 -04:00
|
|
|
func samplePayload() io.Reader {
|
2015-07-07 18:46:20 -04:00
|
|
|
payload := &JsonApiOnePayload{
|
2015-07-06 17:41:53 -04:00
|
|
|
Data: &JsonApiNode{
|
|
|
|
Type: "blogs",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "New blog",
|
|
|
|
"created_at": 1436216820,
|
|
|
|
"view_count": 1000,
|
|
|
|
},
|
2015-07-07 18:46:20 -04:00
|
|
|
Relationships: map[string]interface{}{
|
|
|
|
"posts": &JsonApiRelationshipManyNode{
|
|
|
|
Data: []*JsonApiNode{
|
|
|
|
&JsonApiNode{
|
|
|
|
Type: "posts",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "Foo",
|
|
|
|
"body": "Bar",
|
|
|
|
},
|
|
|
|
},
|
2015-07-08 12:34:37 -04:00
|
|
|
&JsonApiNode{
|
|
|
|
Type: "posts",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "X",
|
|
|
|
"body": "Y",
|
|
|
|
},
|
|
|
|
},
|
2015-07-07 18:46:20 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"current_post": &JsonApiRelationshipOneNode{
|
|
|
|
Data: &JsonApiNode{
|
|
|
|
Type: "posts",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "Bas",
|
|
|
|
"body": "Fuubar",
|
|
|
|
},
|
2015-07-08 12:34:37 -04:00
|
|
|
Relationships: map[string]interface{}{
|
|
|
|
"comments": &JsonApiRelationshipManyNode{
|
|
|
|
Data: []*JsonApiNode{
|
|
|
|
&JsonApiNode{
|
|
|
|
Type: "comments",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"body": "Great post!",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&JsonApiNode{
|
|
|
|
Type: "comments",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"body": "Needs some work!",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-07-07 18:46:20 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-07-06 17:41:53 -04:00
|
|
|
},
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
json.NewEncoder(out).Encode(payload)
|
|
|
|
|
2015-07-08 12:34:37 -04:00
|
|
|
return out
|
2015-07-06 17:41:53 -04:00
|
|
|
}
|
2015-07-08 16:11:03 -04:00
|
|
|
|
|
|
|
func samplePayloadWithId() io.Reader {
|
|
|
|
payload := &JsonApiOnePayload{
|
|
|
|
Data: &JsonApiNode{
|
|
|
|
Id: "2",
|
|
|
|
Type: "blogs",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "New blog",
|
|
|
|
"view_count": 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
json.NewEncoder(out).Encode(payload)
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|