2015-07-05 11:59:30 -04:00
|
|
|
package jsonapi
|
|
|
|
|
2015-07-05 13:59:35 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-07-08 14:49:36 -04:00
|
|
|
"regexp"
|
2015-07-05 13:59:35 -04:00
|
|
|
"testing"
|
2015-07-06 16:40:43 -04:00
|
|
|
"time"
|
2015-07-05 13:59:35 -04:00
|
|
|
)
|
|
|
|
|
2015-07-10 16:50:51 -04:00
|
|
|
type Blog struct {
|
|
|
|
Id int `jsonapi:"primary,blogs"`
|
|
|
|
Title string `jsonapi:"attr,title"`
|
|
|
|
Posts []*Post `jsonapi:"relation,posts"`
|
|
|
|
CurrentPost *Post `jsonapi:"relation,current_post"`
|
|
|
|
CurrentPostId int `jsonapi:"attr,current_post_id"`
|
|
|
|
CreatedAt time.Time `jsonapi:"attr,created_at"`
|
|
|
|
ViewCount int `jsonapi:"attr,view_count"`
|
2015-07-08 12:34:37 -04:00
|
|
|
}
|
|
|
|
|
2015-07-05 13:59:35 -04:00
|
|
|
type Post struct {
|
2015-07-12 10:42:52 -04:00
|
|
|
Id int `jsonapi:"primary,posts"`
|
|
|
|
BlogId int `jsonapi:"attr,blog_id"`
|
|
|
|
Title string `jsonapi:"attr,title"`
|
|
|
|
Body string `jsonapi:"attr,body"`
|
|
|
|
Comments []*Comment `jsonapi:"relation,comments"`
|
|
|
|
LatestComment *Comment `jsonapi:"relation,latest_comment"`
|
2015-07-05 13:59:35 -04:00
|
|
|
}
|
2015-07-05 11:59:30 -04:00
|
|
|
|
2015-07-10 16:50:51 -04:00
|
|
|
type Comment struct {
|
|
|
|
Id int `jsonapi:"primary,comments"`
|
|
|
|
PostId int `jsonapi:"attr,post_id"`
|
|
|
|
Body string `jsonapi:"attr,body"`
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
|
|
|
|
2015-07-07 12:52:38 -04:00
|
|
|
type Blogs []*Blog
|
|
|
|
|
|
|
|
func (b Blogs) GetData() []interface{} {
|
|
|
|
d := make([]interface{}, len(b))
|
|
|
|
for i, blog := range b {
|
|
|
|
d[i] = blog
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2015-07-08 14:49:36 -04:00
|
|
|
func TestMalformedTagResposne(t *testing.T) {
|
|
|
|
testModel := &BadModel{}
|
2015-07-10 11:57:27 -04:00
|
|
|
out := bytes.NewBuffer(nil)
|
2015-07-10 12:07:12 -04:00
|
|
|
err := MarshalOnePayload(out, testModel)
|
2015-07-08 14:49:36 -04:00
|
|
|
|
|
|
|
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-05 11:59:30 -04:00
|
|
|
func TestHasPrimaryAnnotation(t *testing.T) {
|
2015-07-06 13:38:42 -04:00
|
|
|
testModel := &Blog{
|
2015-07-06 17:35:17 -04:00
|
|
|
Id: 5,
|
|
|
|
Title: "Title 1",
|
|
|
|
CreatedAt: time.Now(),
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
out := bytes.NewBuffer(nil)
|
2015-07-10 12:07:12 -04:00
|
|
|
if err := MarshalOnePayload(out, testModel); err != nil {
|
2015-07-10 11:57:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-10 12:07:12 -04:00
|
|
|
resp := new(OnePayload)
|
2015-07-10 11:57:27 -04:00
|
|
|
|
|
|
|
if err := json.NewDecoder(out).Decode(resp); err != nil {
|
2015-07-05 11:59:30 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
data := resp.Data
|
2015-07-05 13:59:35 -04:00
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
if data.Type != "blogs" {
|
|
|
|
t.Fatalf("type should have been blogs, got %s", data.Type)
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
if data.Id != "5" {
|
2015-07-05 11:59:30 -04:00
|
|
|
t.Fatalf("Id not transfered")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSupportsAttributes(t *testing.T) {
|
2015-07-06 13:38:42 -04:00
|
|
|
testModel := &Blog{
|
2015-07-06 17:35:17 -04:00
|
|
|
Id: 5,
|
|
|
|
Title: "Title 1",
|
|
|
|
CreatedAt: time.Now(),
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
out := bytes.NewBuffer(nil)
|
2015-07-10 12:07:12 -04:00
|
|
|
if err := MarshalOnePayload(out, testModel); err != nil {
|
2015-07-10 11:57:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-10 12:07:12 -04:00
|
|
|
resp := new(OnePayload)
|
2015-07-10 11:57:27 -04:00
|
|
|
if err := json.NewDecoder(out).Decode(resp); err != nil {
|
2015-07-05 11:59:30 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
data := resp.Data
|
2015-07-05 13:59:35 -04:00
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
if data.Attributes == nil {
|
2015-07-06 16:40:43 -04:00
|
|
|
t.Fatalf("Expected attributes")
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
if data.Attributes["title"] != "Title 1" {
|
2015-07-05 11:59:30 -04:00
|
|
|
t.Fatalf("Attributes hash not populated using tags correctly")
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 14:57:20 -04:00
|
|
|
|
2015-07-09 15:07:33 -04:00
|
|
|
func TestOmitsZeroTimes(t *testing.T) {
|
|
|
|
testModel := &Blog{
|
|
|
|
Id: 5,
|
|
|
|
Title: "Title 1",
|
|
|
|
CreatedAt: time.Time{},
|
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
out := bytes.NewBuffer(nil)
|
2015-07-10 12:07:12 -04:00
|
|
|
if err := MarshalOnePayload(out, testModel); err != nil {
|
2015-07-10 11:57:27 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-10 12:07:12 -04:00
|
|
|
resp := new(OnePayload)
|
2015-07-10 11:57:27 -04:00
|
|
|
if err := json.NewDecoder(out).Decode(resp); err != nil {
|
2015-07-09 15:07:33 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
data := resp.Data
|
2015-07-09 15:07:33 -04:00
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
if data.Attributes == nil {
|
2015-07-09 15:07:33 -04:00
|
|
|
t.Fatalf("Expected attributes")
|
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
if data.Attributes["created_at"] != nil {
|
2015-07-09 15:07:33 -04:00
|
|
|
t.Fatalf("Created at was serialized even though it was a zero Time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 14:57:20 -04:00
|
|
|
func TestRelations(t *testing.T) {
|
|
|
|
testModel := &Blog{
|
2015-07-06 17:35:17 -04:00
|
|
|
Id: 5,
|
|
|
|
Title: "Title 1",
|
|
|
|
CreatedAt: time.Now(),
|
2015-07-06 14:57:20 -04:00
|
|
|
Posts: []*Post{
|
|
|
|
&Post{
|
|
|
|
Id: 1,
|
|
|
|
Title: "Foo",
|
|
|
|
Body: "Bar",
|
|
|
|
},
|
|
|
|
&Post{
|
|
|
|
Id: 2,
|
|
|
|
Title: "Fuubar",
|
|
|
|
Body: "Bas",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CurrentPost: &Post{
|
|
|
|
Id: 1,
|
|
|
|
Title: "Foo",
|
|
|
|
Body: "Bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
out := bytes.NewBuffer(nil)
|
2015-07-10 12:07:12 -04:00
|
|
|
if err := MarshalOnePayload(out, testModel); err != nil {
|
2015-07-06 14:57:20 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-10 12:07:12 -04:00
|
|
|
resp := new(OnePayload)
|
2015-07-10 11:57:27 -04:00
|
|
|
if err := json.NewDecoder(out).Decode(resp); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-06 14:57:20 -04:00
|
|
|
|
|
|
|
relations := resp.Data.Relationships
|
|
|
|
|
|
|
|
if relations == nil {
|
|
|
|
t.Fatalf("Relationships were not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if relations["posts"] == nil {
|
|
|
|
t.Fatalf("Posts relationship was not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if relations["current_post"] == nil {
|
|
|
|
t.Fatalf("Current post relationship was not materialized")
|
|
|
|
}
|
2015-07-06 15:00:17 -04:00
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
if len(relations["posts"].(map[string]interface{})["data"].([]interface{})) != 2 {
|
2015-07-06 15:00:17 -04:00
|
|
|
t.Fatalf("Did not materialize two posts")
|
|
|
|
}
|
2015-07-06 14:57:20 -04:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:38:42 -04:00
|
|
|
func TestNoRelations(t *testing.T) {
|
2015-07-06 17:35:17 -04:00
|
|
|
testModel := &Blog{Id: 1, Title: "Title 1", CreatedAt: time.Now()}
|
2015-07-06 13:38:42 -04:00
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
out := bytes.NewBuffer(nil)
|
2015-07-10 12:07:12 -04:00
|
|
|
if err := MarshalOnePayload(out, testModel); err != nil {
|
2015-07-06 13:38:42 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-10 12:07:12 -04:00
|
|
|
resp := new(OnePayload)
|
2015-07-10 11:57:27 -04:00
|
|
|
if err := json.NewDecoder(out).Decode(resp); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-06 13:38:42 -04:00
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
if resp.Included != nil {
|
2015-07-06 13:38:42 -04:00
|
|
|
t.Fatalf("Encoding json response did not omit included")
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 12:52:38 -04:00
|
|
|
|
|
|
|
func TestMarshalMany(t *testing.T) {
|
|
|
|
data := Blogs{
|
|
|
|
&Blog{
|
|
|
|
Id: 5,
|
|
|
|
Title: "Title 1",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
Posts: []*Post{
|
|
|
|
&Post{
|
|
|
|
Id: 1,
|
|
|
|
Title: "Foo",
|
|
|
|
Body: "Bar",
|
|
|
|
},
|
|
|
|
&Post{
|
|
|
|
Id: 2,
|
|
|
|
Title: "Fuubar",
|
|
|
|
Body: "Bas",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CurrentPost: &Post{
|
|
|
|
Id: 1,
|
|
|
|
Title: "Foo",
|
|
|
|
Body: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Blog{
|
|
|
|
Id: 6,
|
|
|
|
Title: "Title 2",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
Posts: []*Post{
|
|
|
|
&Post{
|
|
|
|
Id: 3,
|
|
|
|
Title: "Foo",
|
|
|
|
Body: "Bar",
|
|
|
|
},
|
|
|
|
&Post{
|
|
|
|
Id: 4,
|
|
|
|
Title: "Fuubar",
|
|
|
|
Body: "Bas",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CurrentPost: &Post{
|
|
|
|
Id: 4,
|
|
|
|
Title: "Foo",
|
|
|
|
Body: "Bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-10 11:57:27 -04:00
|
|
|
out := bytes.NewBuffer(nil)
|
2015-07-10 12:07:12 -04:00
|
|
|
if err := MarshalManyPayload(out, data); err != nil {
|
2015-07-07 12:52:38 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-10 12:07:12 -04:00
|
|
|
resp := new(ManyPayload)
|
2015-07-10 11:57:27 -04:00
|
|
|
if err := json.NewDecoder(out).Decode(resp); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-07 12:52:38 -04:00
|
|
|
|
|
|
|
d := resp.Data
|
|
|
|
|
|
|
|
if len(d) != 2 {
|
|
|
|
t.Fatalf("data should have two elements")
|
|
|
|
}
|
|
|
|
}
|