jsonapi/response_test.go

575 lines
12 KiB
Go
Raw Normal View History

2015-07-05 11:59:30 -04:00
package jsonapi
2015-07-05 13:59:35 -04:00
import (
"bytes"
"encoding/json"
"reflect"
2015-07-05 13:59:35 -04:00
"testing"
"time"
2015-07-05 13:59:35 -04:00
)
2015-07-10 16:50:51 -04:00
type Blog struct {
2016-07-05 21:32:15 -04:00
ID int `jsonapi:"primary,blogs"`
ClientID string `jsonapi:"client-id"`
2015-07-10 16:50:51 -04:00
Title string `jsonapi:"attr,title"`
Posts []*Post `jsonapi:"relation,posts"`
CurrentPost *Post `jsonapi:"relation,current_post"`
2016-07-05 21:32:15 -04:00
CurrentPostID int `jsonapi:"attr,current_post_id"`
2015-07-10 16:50:51 -04:00
CreatedAt time.Time `jsonapi:"attr,created_at"`
ViewCount int `jsonapi:"attr,view_count"`
}
2015-07-05 13:59:35 -04:00
type Post struct {
Blog
ID uint64 `jsonapi:"primary,posts"`
2016-07-05 21:32:15 -04:00
BlogID int `jsonapi:"attr,blog_id"`
ClientID string `jsonapi:"client-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 {
2016-07-05 21:32:15 -04:00
ID int `jsonapi:"primary,comments"`
ClientID string `jsonapi:"client-id"`
PostID int `jsonapi:"attr,post_id"`
Body string `jsonapi:"attr,body"`
2015-07-05 11:59:30 -04:00
}
type Book struct {
ID uint64 `jsonapi:"primary,books"`
Author string `jsonapi:"attr,author"`
ISBN string `jsonapi:"attr,isbn"`
Title string `jsonapi:"attr,title,omitempty"`
Description *string `jsonapi:"attr,description"`
Pages *uint `jsonapi:"attr,pages,omitempty"`
PublishedAt time.Time
}
type Timestamp struct {
ID int `jsonapi:"primary,timestamps"`
Time time.Time `jsonapi:"attr,timestamp,iso8601"`
Next *time.Time `jsonapi:"attr,next,iso8601"`
}
type Car struct {
ID *string `jsonapi:"primary,cars"`
Make *string `jsonapi:"attr,make,omitempty"`
Model *string `jsonapi:"attr,model,omitempty"`
Year *uint `jsonapi:"attr,year,omitempty"`
}
func TestMarshalIDPtr(t *testing.T) {
id, make, model := "123e4567-e89b-12d3-a456-426655440000", "Ford", "Mustang"
car := &Car{
ID: &id,
Make: &make,
Model: &model,
}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, car); err != nil {
t.Fatal(err)
}
var jsonData map[string]interface{}
if err := json.Unmarshal(out.Bytes(), &jsonData); err != nil {
t.Fatal(err)
}
data := jsonData["data"].(map[string]interface{})
// attributes := data["attributes"].(map[string]interface{})
// Verify that the ID was sent
val, exists := data["id"]
if !exists {
t.Fatal("Was expecting the data.id member to exist")
}
if val != id {
t.Fatalf("Was expecting the data.id member to be `%s`, got `%s`", id, val)
}
}
func TestMarshall_invalidIDType(t *testing.T) {
type badIDStruct struct {
ID *bool `jsonapi:"primary,cars"`
}
id := true
o := &badIDStruct{ID: &id}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, o); err != ErrBadJSONAPIID {
t.Fatalf(
"Was expecting a `%s` error, got `%s`", ErrBadJSONAPIID, err,
)
}
}
func TestOmitsEmptyAnnotation(t *testing.T) {
book := &Book{
Author: "aren55555",
PublishedAt: time.Now().AddDate(0, -1, 0),
}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, book); err != nil {
t.Fatal(err)
}
var jsonData map[string]interface{}
if err := json.Unmarshal(out.Bytes(), &jsonData); err != nil {
t.Fatal(err)
}
attributes := jsonData["data"].(map[string]interface{})["attributes"].(map[string]interface{})
// Verify that the specifically omitted field were omitted
if val, exists := attributes["title"]; exists {
t.Fatalf("Was expecting the data.attributes.title key/value to have been omitted - it was not and had a value of %v", val)
}
if val, exists := attributes["pages"]; exists {
t.Fatalf("Was expecting the data.attributes.pages key/value to have been omitted - it was not and had a value of %v", val)
}
// Verify the implicity omitted fields were omitted
if val, exists := attributes["PublishedAt"]; exists {
t.Fatalf("Was expecting the data.attributes.PublishedAt key/value to have been implicity omitted - it was not and had a value of %v", val)
}
// Verify the unset fields were not omitted
if _, exists := attributes["isbn"]; !exists {
t.Fatal("Was expecting the data.attributes.isbn key/value to have NOT been omitted")
}
}
2015-07-05 11:59:30 -04:00
func TestHasPrimaryAnnotation(t *testing.T) {
2015-07-06 13:38:42 -04:00
testModel := &Blog{
2016-07-05 21:32:15 -04:00
ID: 5,
Title: "Title 1",
CreatedAt: time.Now(),
2015-07-05 11:59:30 -04:00
}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
2015-07-05 11:59:30 -04:00
t.Fatal(err)
}
data := resp.Data
2015-07-05 13:59:35 -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
}
2016-07-05 21:32:15 -04:00
if data.ID != "5" {
t.Fatalf("ID not transfered")
2015-07-05 11:59:30 -04:00
}
}
func TestSupportsAttributes(t *testing.T) {
2015-07-06 13:38:42 -04:00
testModel := &Blog{
2016-07-05 21:32:15 -04:00
ID: 5,
Title: "Title 1",
CreatedAt: time.Now(),
2015-07-05 11:59:30 -04:00
}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
2015-07-05 11:59:30 -04:00
t.Fatal(err)
}
data := resp.Data
2015-07-05 13:59:35 -04:00
if data.Attributes == nil {
t.Fatalf("Expected attributes")
2015-07-05 11:59:30 -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-09 15:07:33 -04:00
func TestOmitsZeroTimes(t *testing.T) {
testModel := &Blog{
2016-07-05 21:32:15 -04:00
ID: 5,
2015-07-09 15:07:33 -04:00
Title: "Title 1",
CreatedAt: time.Time{},
}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
2015-07-09 15:07:33 -04:00
t.Fatal(err)
}
data := resp.Data
2015-07-09 15:07:33 -04:00
if data.Attributes == nil {
2015-07-09 15:07:33 -04:00
t.Fatalf("Expected attributes")
}
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")
}
}
func TestMarshalISO8601Time(t *testing.T) {
testModel := &Timestamp{
ID: 5,
Time: time.Date(2016, 8, 17, 8, 27, 12, 23849, time.UTC),
}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
data := resp.Data
if data.Attributes == nil {
t.Fatalf("Expected attributes")
}
if data.Attributes["timestamp"] != "2016-08-17T08:27:12Z" {
t.Fatal("Timestamp was not serialised into ISO8601 correctly")
}
}
func TestMarshalISO8601TimePointer(t *testing.T) {
tm := time.Date(2016, 8, 17, 8, 27, 12, 23849, time.UTC)
testModel := &Timestamp{
ID: 5,
Next: &tm,
}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
data := resp.Data
if data.Attributes == nil {
t.Fatalf("Expected attributes")
}
if data.Attributes["next"] != "2016-08-17T08:27:12Z" {
t.Fatal("Next was not serialised into ISO8601 correctly")
}
}
func TestRelations(t *testing.T) {
2015-07-12 11:46:30 -04:00
testModel := testBlog()
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
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 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 13:38:42 -04:00
func TestNoRelations(t *testing.T) {
2016-07-05 21:32:15 -04:00
testModel := &Blog{ID: 1, Title: "Title 1", CreatedAt: time.Now()}
2015-07-06 13:38:42 -04:00
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, testModel); err != nil {
2015-07-06 13:38:42 -04:00
t.Fatal(err)
}
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
2015-07-06 13:38:42 -04:00
if resp.Included != nil {
t.Fatalf("Encoding json response did not omit included")
}
}
func TestMarshalOnePayloadWithoutIncluded(t *testing.T) {
data := &Post{
2016-07-15 16:40:23 -04:00
ID: 1,
BlogID: 2,
ClientID: "123e4567-e89b-12d3-a456-426655440000",
Title: "Foo",
Body: "Bar",
Comments: []*Comment{
&Comment{
2016-07-15 16:40:23 -04:00
ID: 20,
Body: "First",
},
&Comment{
2016-07-15 16:40:23 -04:00
ID: 21,
Body: "Hello World",
},
},
LatestComment: &Comment{
2016-07-15 16:40:23 -04:00
ID: 22,
Body: "Cool!",
},
}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayloadWithoutIncluded(out, data); err != nil {
t.Fatal(err)
}
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
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 := []interface{}{
2015-07-07 12:52:38 -04:00
&Blog{
2016-07-05 21:32:15 -04:00
ID: 5,
2015-07-07 12:52:38 -04:00
Title: "Title 1",
CreatedAt: time.Now(),
Posts: []*Post{
&Post{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-07 12:52:38 -04:00
Title: "Foo",
Body: "Bar",
},
&Post{
2016-07-05 21:32:15 -04:00
ID: 2,
2015-07-07 12:52:38 -04:00
Title: "Fuubar",
Body: "Bas",
},
},
CurrentPost: &Post{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-07 12:52:38 -04:00
Title: "Foo",
Body: "Bar",
},
},
&Blog{
2016-07-05 21:32:15 -04:00
ID: 6,
2015-07-07 12:52:38 -04:00
Title: "Title 2",
CreatedAt: time.Now(),
Posts: []*Post{
&Post{
2016-07-05 21:32:15 -04:00
ID: 3,
2015-07-07 12:52:38 -04:00
Title: "Foo",
Body: "Bar",
},
&Post{
2016-07-05 21:32:15 -04:00
ID: 4,
2015-07-07 12:52:38 -04:00
Title: "Fuubar",
Body: "Bas",
},
},
CurrentPost: &Post{
2016-07-05 21:32:15 -04:00
ID: 4,
2015-07-07 12:52:38 -04:00
Title: "Foo",
Body: "Bar",
},
},
}
out := bytes.NewBuffer(nil)
if err := MarshalManyPayload(out, data); err != nil {
2015-07-07 12:52:38 -04:00
t.Fatal(err)
}
resp := new(ManyPayload)
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")
}
}
2015-07-12 11:46:30 -04:00
func TestMarshalMany_WithSliceOfStructPointers(t *testing.T) {
var data []*Blog
for len(data) < 2 {
data = append(data, testBlog())
}
out := bytes.NewBuffer(nil)
if err := MarshalManyPayload(out, data); err != nil {
t.Fatal(err)
}
resp := new(ManyPayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
d := resp.Data
if len(d) != 2 {
t.Fatalf("data should have two elements")
}
}
func TestMarshalMany_SliceOfInterfaceAndSliceOfStructsSameJSON(t *testing.T) {
structs := []*Book{
&Book{ID: 1, Author: "aren55555", ISBN: "abc"},
&Book{ID: 2, Author: "shwoodard", ISBN: "xyz"},
}
interfaces := []interface{}{}
for _, s := range structs {
interfaces = append(interfaces, s)
}
// Perform Marshals
structsOut := new(bytes.Buffer)
if err := MarshalManyPayload(structsOut, structs); err != nil {
t.Fatal(err)
}
interfacesOut := new(bytes.Buffer)
if err := MarshalManyPayload(interfacesOut, interfaces); err != nil {
t.Fatal(err)
}
// Generic JSON Unmarshal
structsData, interfacesData :=
make(map[string]interface{}), make(map[string]interface{})
if err := json.Unmarshal(structsOut.Bytes(), &structsData); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(interfacesOut.Bytes(), &interfacesData); err != nil {
t.Fatal(err)
}
// Compare Result
if !reflect.DeepEqual(structsData, interfacesData) {
t.Fatal("Was expecting the JSON API generated to be the same")
}
}
func TestMarshalMany_InvalidIntefaceArgument(t *testing.T) {
out := new(bytes.Buffer)
if err := MarshalManyPayload(out, true); err != ErrExpectedSlice {
t.Fatal("Was expecting an error")
}
if err := MarshalManyPayload(out, 25); err != ErrExpectedSlice {
t.Fatal("Was expecting an error")
}
if err := MarshalManyPayload(out, Book{}); err != ErrExpectedSlice {
t.Fatal("Was expecting an error")
}
}
2015-07-12 11:46:30 -04:00
func testBlog() *Blog {
return &Blog{
2016-07-05 21:32:15 -04:00
ID: 5,
2015-07-12 11:46:30 -04:00
Title: "Title 1",
CreatedAt: time.Now(),
Posts: []*Post{
&Post{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-12 11:46:30 -04:00
Title: "Foo",
Body: "Bar",
Comments: []*Comment{
&Comment{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-12 11:46:30 -04:00
Body: "foo",
},
&Comment{
2016-07-05 21:32:15 -04:00
ID: 2,
2015-07-12 11:46:30 -04:00
Body: "bar",
},
},
LatestComment: &Comment{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-12 11:46:30 -04:00
Body: "foo",
},
},
&Post{
2016-07-05 21:32:15 -04:00
ID: 2,
2015-07-12 11:46:30 -04:00
Title: "Fuubar",
Body: "Bas",
Comments: []*Comment{
&Comment{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-12 11:46:30 -04:00
Body: "foo",
},
&Comment{
2016-07-05 21:32:15 -04:00
ID: 3,
2015-07-12 11:46:30 -04:00
Body: "bas",
},
},
LatestComment: &Comment{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-12 11:46:30 -04:00
Body: "foo",
},
},
},
CurrentPost: &Post{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-12 11:46:30 -04:00
Title: "Foo",
Body: "Bar",
Comments: []*Comment{
&Comment{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-12 11:46:30 -04:00
Body: "foo",
},
&Comment{
2016-07-05 21:32:15 -04:00
ID: 2,
2015-07-12 11:46:30 -04:00
Body: "bar",
},
},
LatestComment: &Comment{
2016-07-05 21:32:15 -04:00
ID: 1,
2015-07-12 11:46:30 -04:00
Body: "foo",
},
},
}
}