File: data_structs_test.go

package info (click to toggle)
golang-github-manyminds-api2go 1.0-RC2%2Bgit20161229.31.dc368bb-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 484 kB
  • ctags: 488
  • sloc: sh: 23; makefile: 3
file content (235 lines) | stat: -rw-r--r-- 5,373 bytes parent folder | download
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package jsonapi

import (
	"encoding/json"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("JSONAPI Struct tests", func() {
	Context("Testing array and object data payload", func() {
		It("detects object payload", func() {
			sampleJSON := `{
				"data": {
					"type": "test",
					"id": "1",
					"attributes": {"foo": "bar"},
					"relationships": {
						"author": {
							"data": {"type": "author", "id": "1"}
						}
					}
				}
			}`

			expectedData := &Data{
				Type:       "test",
				ID:         "1",
				Attributes: json.RawMessage([]byte(`{"foo": "bar"}`)),
				Relationships: map[string]Relationship{
					"author": {
						Data: &RelationshipDataContainer{
							DataObject: &RelationshipData{
								Type: "author",
								ID:   "1",
							},
						},
					},
				},
			}

			target := Document{}

			err := json.Unmarshal([]byte(sampleJSON), &target)
			Expect(err).ToNot(HaveOccurred())
			Expect(target.Data.DataObject).To(Equal(expectedData))
		})

		It("detects array payload", func() {
			sampleJSON := `{
				"data": [
					{
						"type": "test",
						"id": "1",
						"attributes": {"foo": "bar"},
						"relationships": {
							"comments": {
								"data": [
									{"type": "comments", "id": "1"},
									{"type": "comments", "id": "2"}
								]
							}
						}
					}
				]
			}`

			expectedData := Data{
				Type:       "test",
				ID:         "1",
				Attributes: json.RawMessage([]byte(`{"foo": "bar"}`)),
				Relationships: map[string]Relationship{
					"comments": {
						Data: &RelationshipDataContainer{
							DataArray: []RelationshipData{
								{
									Type: "comments",
									ID:   "1",
								},
								{
									Type: "comments",
									ID:   "2",
								},
							},
						},
					},
				},
			}

			target := Document{}

			err := json.Unmarshal([]byte(sampleJSON), &target)
			Expect(err).ToNot(HaveOccurred())
			Expect(target.Data.DataArray).To(Equal([]Data{expectedData}))
		})
	})

	It("return an error for invalid relationship data format", func() {
		sampleJSON := `
		{
			"data": [
				{
					"type": "test",
					"id": "1",
					"attributes": {"foo": "bar"},
					"relationships": {
						"comments": {
							"data": "foo"
						}
					}
				}
			]
		}`

		target := Document{}

		err := json.Unmarshal([]byte(sampleJSON), &target)
		Expect(err).To(HaveOccurred())
		Expect(err.Error()).To(Equal("Invalid json for relationship data array/object"))
	})

	It("creates an empty slice for empty to-many relationships and nil for empty toOne", func() {
		sampleJSON := `{
			"data": [
				{
					"type": "test",
					"id": "1",
					"attributes": {"foo": "bar"},
					"relationships": {
						"comments": {
							"data": []
						},
						"author": {
							"data": null
						}
					}
				}
			]
		}`

		expectedData := Data{
			Type:       "test",
			ID:         "1",
			Attributes: json.RawMessage([]byte(`{"foo": "bar"}`)),
			Relationships: map[string]Relationship{
				"comments": {
					Data: &RelationshipDataContainer{
						DataArray: []RelationshipData{},
					},
				},
				"author": {
					Data: nil,
				},
			},
		}

		target := Document{}

		err := json.Unmarshal([]byte(sampleJSON), &target)
		Expect(err).ToNot(HaveOccurred())
		Expect(target.Data.DataArray).To(Equal([]Data{expectedData}))
	})

	Context("Marshal and Unmarshal link structs", func() {
		It("marshals to a string with no metadata", func() {
			link := Link{Href: "test link"}
			ret, err := json.Marshal(&link)
			Expect(err).ToNot(HaveOccurred())
			Expect(ret).To(MatchJSON(`"test link"`))
		})

		It("marshals to an object with metadata", func() {
			link := Link{
				Href: "test link",
				Meta: map[string]interface{}{
					"test": "data",
				},
			}
			ret, err := json.Marshal(&link)
			Expect(err).ToNot(HaveOccurred())
			Expect(ret).To(MatchJSON(`{
				"href": "test link",
				"meta": {"test": "data"}
			}`))
		})

		It("unmarshals from a string", func() {
			expected := Link{Href: "test link"}
			target := Link{}
			err := json.Unmarshal([]byte(`"test link"`), &target)
			Expect(err).ToNot(HaveOccurred())
			Expect(target).To(Equal(expected))
		})

		It("unmarshals from an object", func() {
			expected := Link{
				Href: "test link",
				Meta: map[string]interface{}{
					"test": "data",
				},
			}
			target := Link{}
			err := json.Unmarshal([]byte(`{
				"href": "test link",
				"meta": {"test": "data"}
			}`), &target)
			Expect(err).ToNot(HaveOccurred())
			Expect(target).To(Equal(expected))
		})

		It("unmarshals with an error when href is missing", func() {
			err := json.Unmarshal([]byte(`{}`), &Link{})
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(Equal(`link object expects a "href" key`))
		})

		It("unmarshals with an error for syntax error", func() {
			badPayloads := []string{`{`, `"`}
			for _, payload := range badPayloads {
				err := json.Unmarshal([]byte(payload), &Link{})
				Expect(err).To(HaveOccurred())
				Expect(err.Error()).To(Equal("unexpected end of JSON input"))
			}
		})

		It("unmarshals with an error for wrong types", func() {
			badPayloads := []string{`null`, `13`, `[]`}
			for _, payload := range badPayloads {
				err := json.Unmarshal([]byte(payload), &Link{})
				Expect(err).To(HaveOccurred())
				Expect(err.Error()).To(Equal("expected a JSON encoded string or object"))
			}
		})
	})
})