File: marshal_composition_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 (65 lines) | stat: -rw-r--r-- 1,384 bytes parent folder | download | duplicates (3)
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
package jsonapi

import (
	"time"

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

type TaggedPost struct {
	SimplePost
	Tag string `json:"tag"`
}

var _ = Describe("Embedded struct types", func() {
	created, _ := time.Parse(time.RFC3339, "2014-11-10T16:30:48.823Z")
	post := TaggedPost{
		SimplePost{ID: "first", Title: "First Post", Text: "Lipsum", Created: created},
		"important",
	}

	Context("When marshaling objects with struct composition", func() {
		It("marshals", func() {
			i, err := Marshal(post)
			Expect(err).To(BeNil())
			Expect(i).To(MatchJSON(`{
				"data": {
					"type":"taggedPosts",
					"id":"first",
					"attributes": {
						"title":"First Post",
						"text":"Lipsum",
						"size":0,
						"created-date":"2014-11-10T16:30:48.823Z",
						"updated-date":"0001-01-01T00:00:00Z",
						"tag":"important"
					}
				}
			}`))
		})
	})

	Context("When unmarshaling objects with struct composition", func() {
		postJSON := `{
			"data": {
				"type": "taggedPosts",
				"id": "first",
				"attributes": {
					"title": "First Post",
					"text":  "Lipsum",
					"size": 0,
					"created-date": "2014-11-10T16:30:48.823Z",
					"tag": "important"
				}
			}
		}`

		It("unmarshals", func() {
			target := TaggedPost{}
			err := Unmarshal([]byte(postJSON), &target)
			Expect(err).ToNot(HaveOccurred())
			Expect(target).To(Equal(post))
		})
	})
})