2015-07-05 11:59:30 -04:00
|
|
|
package jsonapi
|
|
|
|
|
2015-07-05 13:59:35 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-07-06 15:00:17 -04:00
|
|
|
"reflect"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type Post struct {
|
|
|
|
Id int `jsonapi:"primary,posts"`
|
|
|
|
Title string `jsonapi:"attr,title"`
|
|
|
|
Body string `jsonapi:"attr,body"`
|
|
|
|
}
|
2015-07-05 11:59:30 -04:00
|
|
|
|
|
|
|
type Blog struct {
|
2015-07-06 16:40:43 -04:00
|
|
|
Id int `jsonapi:"primary,blogs"`
|
|
|
|
Title string `jsonapi:"attr,title"`
|
|
|
|
Posts []*Post `jsonapi:"relation,posts"`
|
|
|
|
CurrentPost *Post `jsonapi:"relation,current_post"`
|
|
|
|
CreatedAt time.Time `jsonapi:"attr,created_at"`
|
|
|
|
ViewCount int `jsonapi:"attr,view_count"`
|
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-05 11:59:30 -04:00
|
|
|
Id: 5,
|
|
|
|
Title: "Title 1",
|
|
|
|
}
|
|
|
|
|
2015-07-06 15:06:12 -04:00
|
|
|
resp, err := MarshalJsonApiPayload(testModel)
|
2015-07-05 11:59:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-05 13:59:35 -04:00
|
|
|
response := resp.Data
|
|
|
|
|
2015-07-06 13:38:42 -04:00
|
|
|
if response.Type != "blogs" {
|
|
|
|
t.Fatalf("type should have been blogs, got %s", response.Type)
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if response.Id != "5" {
|
|
|
|
t.Fatalf("Id not transfered")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSupportsAttributes(t *testing.T) {
|
2015-07-06 13:38:42 -04:00
|
|
|
testModel := &Blog{
|
2015-07-05 11:59:30 -04:00
|
|
|
Id: 5,
|
|
|
|
Title: "Title 1",
|
|
|
|
}
|
|
|
|
|
2015-07-06 15:06:12 -04:00
|
|
|
resp, err := MarshalJsonApiPayload(testModel)
|
2015-07-05 11:59:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-05 13:59:35 -04:00
|
|
|
response := resp.Data
|
|
|
|
|
2015-07-06 16:40:43 -04:00
|
|
|
if response.Attributes == nil {
|
|
|
|
t.Fatalf("Expected attributes")
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if response.Attributes["title"] != "Title 1" {
|
|
|
|
t.Fatalf("Attributes hash not populated using tags correctly")
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 14:57:20 -04:00
|
|
|
|
|
|
|
func TestRelations(t *testing.T) {
|
|
|
|
testModel := &Blog{
|
|
|
|
Id: 5,
|
|
|
|
Title: "Title 1",
|
|
|
|
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-06 15:06:12 -04:00
|
|
|
resp, err := MarshalJsonApiPayload(testModel)
|
2015-07-06 14:57:20 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
json.NewEncoder(out).Encode(resp)
|
|
|
|
|
|
|
|
fmt.Printf("%s\n", out.Bytes())
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if reflect.ValueOf(relations["posts"]).Len() != 2 {
|
|
|
|
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) {
|
|
|
|
testModel := &Blog{Id: 1, Title: "Title 1"}
|
|
|
|
|
2015-07-06 15:06:12 -04:00
|
|
|
resp, err := MarshalJsonApiPayload(testModel)
|
2015-07-06 13:38:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBuffer := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
json.NewEncoder(jsonBuffer).Encode(resp)
|
|
|
|
|
|
|
|
fmt.Printf("%s\n", jsonBuffer.Bytes())
|
|
|
|
|
2015-07-06 14:57:20 -04:00
|
|
|
decodedResponse := new(JsonApiPayload)
|
2015-07-06 13:38:42 -04:00
|
|
|
|
|
|
|
json.NewDecoder(jsonBuffer).Decode(decodedResponse)
|
|
|
|
|
|
|
|
if decodedResponse.Included != nil {
|
|
|
|
t.Fatalf("Encoding json response did not omit included")
|
|
|
|
}
|
|
|
|
}
|