-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_relationships_test.go
45 lines (40 loc) · 1.52 KB
/
decode_relationships_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
package jsonapi_test
import (
"github.com/ryanmoran/jsonapi"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("DecodeRelationships", func() {
Describe("UnmarshalJSON", func() {
Context("failure cases", func() {
Context("when the JSON cannot be unmarshaled", func() {
It("returns an error", func() {
var payload RelationshipsPayload
relationships := jsonapi.NewDecodeRelationships(&payload)
err := relationships.UnmarshalJSON([]byte("%%%"))
Expect(err).To(MatchError(ContainSubstring("invalid character '%'")))
})
})
Context("when the JSON is missing a data key", func() {
It("returns an error", func() {
var payload RelationshipsPayload
relationships := jsonapi.NewDecodeRelationships(&payload)
err := relationships.UnmarshalJSON([]byte(`{
"some-relationship": "no-data"
}`))
Expect(err).To(MatchError("cannot decode *jsonapi_test.RelationshipsPayload &{ []}: relationship some-relationship is not an object"))
})
})
Context("when the JSON has a slice that is not of type []map[string]interface{}", func() {
It("returns an error", func() {
var payload RelationshipsPayload
relationships := jsonapi.NewDecodeRelationships(&payload)
err := relationships.UnmarshalJSON([]byte(`{
"some-relationship": { "data": ["not-a-map"] }
}`))
Expect(err).To(MatchError("cannot decode *jsonapi_test.RelationshipsPayload &{ []}: relationship some-relationship data is not an array of objects"))
})
})
})
})
})