-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresource_integration_test.go
More file actions
72 lines (61 loc) · 1.9 KB
/
Copy pathresource_integration_test.go
File metadata and controls
72 lines (61 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package jsonapi_test
import (
"encoding/json"
"reflect"
"testing"
"github.com/smotes/jsonapi"
)
func TestResource_MarshalJSON(t *testing.T) {
r, err := jsonapi.ToResource(&testArticle, true)
if err != nil {
t.Errorf("unexpected error when converting article struct to resource: %+v", err)
return
}
b, err := json.Marshal(&r)
if err != nil {
t.Errorf("unexpected error when marshaling resource to json: %+v", err)
return
}
actual := string(b)
expected := testArticleJSON
if err := compareJSON(expected, actual); err != nil {
t.Errorf("%+v", err)
}
}
func TestResource_UnmarshalJSON(t *testing.T) {
p := Person{}
r1 := jsonapi.Resource{}
r2 := jsonapi.Resource{}
expected := testArticle
actual := Article{}
if err := json.Unmarshal([]byte(testArticleJSON), &r1); err != nil {
t.Errorf("unexpected error when unmarshaling resource from json: %+v", err)
return
}
if err := jsonapi.FromResource(&actual, &r1, true); err != nil {
t.Errorf("unexpected error when converting resource to article struct: %+v", err)
return
}
// relationship resource(s) must be unmarshaled separately
if rel, ok := r1.Relationships.Get("author"); ok {
if err := json.Unmarshal(rel.Data, &r2); err != nil {
t.Errorf("unexpected error when unmarshaling resource relationship from json: %+v", err)
return
}
}
if err := jsonapi.FromResource(&p, &r2, true); err != nil {
t.Errorf("unexpected error when converting resource to person struct: %+v", err)
return
}
actual.Author = &p
if !reflect.DeepEqual(expected.Author, actual.Author) {
t.Errorf("article.Author from unmarshaling resource does not match expected value:\n- expected:\t%+v\n- actual\t%+v",
expected, actual)
}
expected.Author = nil
actual.Author = nil
if !reflect.DeepEqual(expected, actual) {
t.Errorf("article from unmarshaling resource does not match expected value:\n- expected:\t%+v\n- actual:\t%+v",
expected, actual)
}
}