File: json_test.go

package info (click to toggle)
golang-github-mimuret-golang-iij-dpf 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: makefile: 55
file content (90 lines) | stat: -rw-r--r-- 2,540 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
package api_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/mimuret/golang-iij-dpf/pkg/api"
)

var _ api.Spec = &JsonTest{}

type JsonTest struct {
	Name string `json:"name" read:"read_name" create:"create_name" update:"update_name" apply:"apply_name"`
}

func (j *JsonTest) DeepCopyObject() api.Object { return &JsonTest{Name: j.Name} }
func (JsonTest) GetName() string               { return "jsontests" }
func (JsonTest) GetGroup() string              { return "tests" }
func (JsonTest) GetPathMethod(a api.Action) (string, string) {
	return a.ToMethod(), "/tests/jsontests"
}

var _ = Describe("json", func() {
	var (
		bs    []byte
		err   error
		value JsonTest
	)
	BeforeEach(func() {
		value = JsonTest{Name: "hogehoge"}
	})
	Describe("for API", func() {
		Context("UnmarshalRead(tag name is `read`)", func() {
			BeforeEach(func() {
				err = api.UnmarshalRead([]byte(`{"read_name": "book"}`), &value)
			})
			It("can read `name`", func() {
				Expect(err).To(Succeed())
				Expect(value.Name).To(Equal("book"))
			})
		})
		Context("MarshalCreate(tag name is `create`)", func() {
			BeforeEach(func() {
				bs, err = api.MarshalCreate(&value)
			})
			It("can read `name`", func() {
				Expect(err).To(Succeed())
				Expect(string(bs)).To(Equal(`{"create_name":"hogehoge"}`))
			})
		})
		Context("MarshalUpdate(tag name is `update`)", func() {
			BeforeEach(func() {
				bs, err = api.MarshalUpdate(&value)
			})
			It("can read `name`", func() {
				Expect(err).To(Succeed())
				Expect(string(bs)).To(Equal(`{"update_name":"hogehoge"}`))
			})
		})
		Context("MarshalApply (tag name is `apply`)", func() {
			BeforeEach(func() {
				bs, err = api.MarshalApply(&value)
			})
			It("can read `name`", func() {
				Expect(err).To(Succeed())
				Expect(string(bs)).To(Equal(`{"apply_name":"hogehoge"}`))
			})
		})
	})
	Describe("for file", func() {
		Context("MarshalOutput(tag name is `json`)", func() {
			BeforeEach(func() {
				bs, err = api.MarshalOutput(&value)
			})
			It("can read `name`", func() {
				Expect(err).To(Succeed())
				Expect(string(bs)).To(MatchJSON(`{"kind":"JsonTest","apiVersion":"tests","resource":{"name":"hogehoge"}}`))
			})
		})
		Context("UnMarshalInput(tag name is `json`)", func() {
			BeforeEach(func() {
				err = api.UnMarshalInput([]byte(`{"kind": "JsonTest", "apiVersion": "tests", "resource": {"name": "book"}}`), &value)
			})
			It("can read `name`", func() {
				Expect(err).To(Succeed())
				Expect(value.Name).To(Equal("book"))
			})
		})
	})
})