Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit 7d60f15

Browse files
committed
Added test (serialization, deserialization) for the []string attr feature.
1 parent 1d32e91 commit 7d60f15

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

request_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"io"
77
"reflect"
8+
"sort"
89
"strings"
910
"testing"
1011
"time"
@@ -30,6 +31,39 @@ type ModelBadTypes struct {
3031
TimePtrField *time.Time `jsonapi:"attr,time_ptr_field"`
3132
}
3233

34+
func TestUnmarshall_attrStringSlice(t *testing.T) {
35+
out := &Book{}
36+
tags := []string{"fiction", "sale"}
37+
data := map[string]interface{}{
38+
"data": map[string]interface{}{
39+
"type": "books",
40+
"id": "1",
41+
"attributes": map[string]interface{}{"tags": tags},
42+
},
43+
}
44+
b, err := json.Marshal(data)
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
49+
if err := UnmarshalPayload(bytes.NewReader(b), out); err != nil {
50+
t.Fatal(err)
51+
}
52+
53+
if e, a := len(tags), len(out.Tags); e != a {
54+
t.Fatalf("Was expecting %d tags, got %d", e, a)
55+
}
56+
57+
sort.Strings(tags)
58+
sort.Strings(out.Tags)
59+
60+
for i, tag := range tags {
61+
if e, a := tag, out.Tags[i]; e != a {
62+
t.Fatalf("At index %d, was expecting %s got %s", i, e, a)
63+
}
64+
}
65+
}
66+
3367
func TestUnmarshalToStructWithPointerAttr(t *testing.T) {
3468
out := new(WithPointer)
3569
in := map[string]interface{}{

response_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"reflect"
8+
"sort"
89
"testing"
910
"time"
1011
)
@@ -83,6 +84,43 @@ type Book struct {
8384
Description *string `jsonapi:"attr,description"`
8485
Pages *uint `jsonapi:"attr,pages,omitempty"`
8586
PublishedAt time.Time
87+
Tags []string `jsonapi:"attr,tags"`
88+
}
89+
90+
func TestMarshall_attrStringSlice(t *testing.T) {
91+
tags := []string{"fiction", "sale"}
92+
b := &Book{ID: 1, Tags: tags}
93+
94+
out := bytes.NewBuffer(nil)
95+
if err := MarshalOnePayload(out, b); err != nil {
96+
t.Fatal(err)
97+
}
98+
99+
var jsonData map[string]interface{}
100+
if err := json.Unmarshal(out.Bytes(), &jsonData); err != nil {
101+
t.Fatal(err)
102+
}
103+
104+
jsonTags := jsonData["data"].(map[string]interface{})["attributes"].(map[string]interface{})["tags"].([]interface{})
105+
if e, a := len(tags), len(jsonTags); e != a {
106+
t.Fatalf("Was expecting tags of length %d got %d", e, a)
107+
}
108+
109+
// Convert from []interface{} to []string
110+
jsonTagsStrings := []string{}
111+
for _, tag := range jsonTags {
112+
jsonTagsStrings = append(jsonTagsStrings, tag.(string))
113+
}
114+
115+
// Sort both
116+
sort.Strings(jsonTagsStrings)
117+
sort.Strings(tags)
118+
119+
for i, tag := range tags {
120+
if e, a := tag, jsonTagsStrings[i]; e != a {
121+
t.Fatalf("At index %d, was expecting %s got %s", i, e, a)
122+
}
123+
}
86124
}
87125

88126
func TestWithoutOmitsEmptyAnnotationOnRelation(t *testing.T) {

0 commit comments

Comments
 (0)