-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmarshal_test.go
138 lines (123 loc) · 3.09 KB
/
unmarshal_test.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package jsonapi_test
import (
"encoding/json"
"github.com/ryanmoran/jsonapi"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Unmarshal", func() {
It("unmarshals a simple payload", func() {
var payload SimplePayload
err := jsonapi.Unmarshal([]byte(`{
"data": {
"type": "simple-payload",
"id": "some-id"
}
}`), &payload)
Expect(err).NotTo(HaveOccurred())
Expect(payload).To(Equal(SimplePayload{ID: "some-id"}))
})
It("unmarshals a payload with attributes", func() {
var payload AttributesPayload
err := jsonapi.Unmarshal([]byte(`{
"data": {
"type": "attributes-payload",
"id": "some-id",
"attributes": {
"some-attr": "some-value"
}
}
}`), &payload)
Expect(err).NotTo(HaveOccurred())
Expect(payload).To(Equal(AttributesPayload{
ID: "some-id",
SomeAttr: "some-value",
}))
})
It("unmarshals a payload with optional attributes", func() {
var payload OptionalAttributesPayload
err := jsonapi.Unmarshal([]byte(`{
"data": {
"type": "optional-attributes-payload",
"id": "some-id",
"attributes": {
"second-attr": { "second-key": "second-value" }
}
}
}`), &payload)
Expect(err).NotTo(HaveOccurred())
Expect(payload).To(Equal(OptionalAttributesPayload{
ID: "some-id",
SecondAttr: json.RawMessage(`{ "second-key": "second-value" }`),
}))
})
It("unmarshals a payload with complex attributes", func() {
var payload ComplexAttributesPayload
err := jsonapi.Unmarshal([]byte(`{
"data": {
"type": "complex-attributes-payload",
"id": "some-id",
"attributes": {
"some-attr": {
"name": "some-name"
}
}
}
}`), &payload)
Expect(err).NotTo(HaveOccurred())
Expect(payload).To(Equal(ComplexAttributesPayload{
ID: "some-id",
SomeAttr: ComplexAttributesPayloadAttribute{
Name: "some-name",
},
}))
})
It("unmarshals a payload with raw message attributes", func() {
var payload RawMessageAttributesPayload
err := jsonapi.Unmarshal([]byte(`{
"data": {
"type": "raw-message-attributes-payload",
"id": "some-id",
"attributes": {
"some-attr": { "some-key": "some-value" }
}
}
}`), &payload)
Expect(err).NotTo(HaveOccurred())
Expect(payload).To(Equal(RawMessageAttributesPayload{
ID: "some-id",
SomeAttr: json.RawMessage(`{ "some-key": "some-value" }`),
}))
})
It("unmarshals a payload with relationships", func() {
var payload RelationshipsPayload
err := jsonapi.Unmarshal([]byte(`{
"data": {
"type": "relationships-payload",
"id": "some-id",
"relationships": {
"single-relation": {
"data": {
"type": "simple-payload",
"id": "single-relation-id"
}
},
"multi-relation": {
"data": [
{
"type": "simple-payload",
"id": "multi-relation-id"
}
]
}
}
}
}`), &payload)
Expect(err).NotTo(HaveOccurred())
Expect(payload).To(Equal(RelationshipsPayload{
ID: "some-id",
SingleRelationID: "single-relation-id",
MultiRelationIDs: []string{"multi-relation-id"},
}))
})
})