forked from Mirrors/jsonapi
rename struct, add test for relationships
This commit is contained in:
parent
d1af74e681
commit
68210ff3ec
6
node.go
6
node.go
|
@ -1,7 +1,9 @@
|
|||
package jsonapi
|
||||
|
||||
type JsonApiNodeWrapper struct {
|
||||
Data *JsonApiNode `json:"data"`
|
||||
type JsonApiPayload struct {
|
||||
Data *JsonApiNode `json:"data"`
|
||||
Included []*JsonApiNode `json:"included,omitempty"`
|
||||
Links *map[string]string `json:"links,omitempty"`
|
||||
}
|
||||
|
||||
type JsonApiNode struct {
|
||||
|
|
|
@ -7,18 +7,13 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
type JsonApiResponse struct {
|
||||
Data *JsonApiNode `json:"data"`
|
||||
Included []*JsonApiNode `json:"included,omitempty"`
|
||||
}
|
||||
|
||||
func CreateJsonApiResponse(model interface{}) (*JsonApiResponse, error) {
|
||||
func CreateJsonApiResponse(model interface{}) (*JsonApiPayload, error) {
|
||||
rootNode, included, err := visitModelNode(model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &JsonApiResponse{Data: rootNode}
|
||||
resp := &JsonApiPayload{Data: rootNode}
|
||||
|
||||
uniqueIncluded := make(map[string]*JsonApiNode)
|
||||
|
||||
|
|
|
@ -24,23 +24,6 @@ func TestHasPrimaryAnnotation(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",
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := CreateJsonApiResponse(testModel)
|
||||
|
@ -48,11 +31,6 @@ func TestHasPrimaryAnnotation(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
out := bytes.NewBuffer(nil)
|
||||
json.NewEncoder(out).Encode(resp)
|
||||
|
||||
fmt.Printf("%s\n", out.Bytes())
|
||||
|
||||
response := resp.Data
|
||||
|
||||
if response.Type != "blogs" {
|
||||
|
@ -85,6 +63,55 @@ func TestSupportsAttributes(t *testing.T) {
|
|||
t.Fatalf("Attributes hash not populated using tags correctly")
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := CreateJsonApiResponse(testModel)
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoRelations(t *testing.T) {
|
||||
testModel := &Blog{Id: 1, Title: "Title 1"}
|
||||
|
||||
|
@ -99,7 +126,7 @@ func TestNoRelations(t *testing.T) {
|
|||
|
||||
fmt.Printf("%s\n", jsonBuffer.Bytes())
|
||||
|
||||
decodedResponse := new(JsonApiResponse)
|
||||
decodedResponse := new(JsonApiPayload)
|
||||
|
||||
json.NewDecoder(jsonBuffer).Decode(decodedResponse)
|
||||
|
||||
|
|
Loading…
Reference in New Issue