forked from Mirrors/jsonapi
Added test (serialization, deserialization) for the []string attr feature.
This commit is contained in:
parent
1d32e9110b
commit
7d60f153a2
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"io"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -30,6 +31,39 @@ type ModelBadTypes struct {
|
|||
TimePtrField *time.Time `jsonapi:"attr,time_ptr_field"`
|
||||
}
|
||||
|
||||
func TestUnmarshall_attrStringSlice(t *testing.T) {
|
||||
out := &Book{}
|
||||
tags := []string{"fiction", "sale"}
|
||||
data := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"type": "books",
|
||||
"id": "1",
|
||||
"attributes": map[string]interface{}{"tags": tags},
|
||||
},
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := UnmarshalPayload(bytes.NewReader(b), out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if e, a := len(tags), len(out.Tags); e != a {
|
||||
t.Fatalf("Was expecting %d tags, got %d", e, a)
|
||||
}
|
||||
|
||||
sort.Strings(tags)
|
||||
sort.Strings(out.Tags)
|
||||
|
||||
for i, tag := range tags {
|
||||
if e, a := tag, out.Tags[i]; e != a {
|
||||
t.Fatalf("At index %d, was expecting %s got %s", i, e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalToStructWithPointerAttr(t *testing.T) {
|
||||
out := new(WithPointer)
|
||||
in := map[string]interface{}{
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
@ -83,6 +84,43 @@ type Book struct {
|
|||
Description *string `jsonapi:"attr,description"`
|
||||
Pages *uint `jsonapi:"attr,pages,omitempty"`
|
||||
PublishedAt time.Time
|
||||
Tags []string `jsonapi:"attr,tags"`
|
||||
}
|
||||
|
||||
func TestMarshall_attrStringSlice(t *testing.T) {
|
||||
tags := []string{"fiction", "sale"}
|
||||
b := &Book{ID: 1, Tags: tags}
|
||||
|
||||
out := bytes.NewBuffer(nil)
|
||||
if err := MarshalOnePayload(out, b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var jsonData map[string]interface{}
|
||||
if err := json.Unmarshal(out.Bytes(), &jsonData); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
jsonTags := jsonData["data"].(map[string]interface{})["attributes"].(map[string]interface{})["tags"].([]interface{})
|
||||
if e, a := len(tags), len(jsonTags); e != a {
|
||||
t.Fatalf("Was expecting tags of length %d got %d", e, a)
|
||||
}
|
||||
|
||||
// Convert from []interface{} to []string
|
||||
jsonTagsStrings := []string{}
|
||||
for _, tag := range jsonTags {
|
||||
jsonTagsStrings = append(jsonTagsStrings, tag.(string))
|
||||
}
|
||||
|
||||
// Sort both
|
||||
sort.Strings(jsonTagsStrings)
|
||||
sort.Strings(tags)
|
||||
|
||||
for i, tag := range tags {
|
||||
if e, a := tag, jsonTagsStrings[i]; e != a {
|
||||
t.Fatalf("At index %d, was expecting %s got %s", i, e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithoutOmitsEmptyAnnotationOnRelation(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue