-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayload_test.go
70 lines (49 loc) · 2.09 KB
/
payload_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
package fxjsonapi_test
import (
"fmt"
"testing"
"github.com/ankorstore/yokai-contrib/fxjsonapi"
"github.com/ankorstore/yokai-contrib/fxjsonapi/testdata/model"
"github.com/stretchr/testify/assert"
)
//nolint:goconst
func TestMarshall(t *testing.T) {
t.Parallel()
t.Run("test failure with invalid data", func(t *testing.T) {
t.Parallel()
invalid := struct {
ID int `jsonapi:"invalid"`
}{}
_, err := fxjsonapi.Marshall(&invalid, fxjsonapi.MarshallParams{})
assert.Error(t, err)
assert.Equal(t, "Bad jsonapi struct tag format", err.Error())
})
t.Run("test success with defaults", func(t *testing.T) {
t.Parallel()
foo := model.CreateTestFoo()
mFoo, err := fxjsonapi.Marshall(&foo, fxjsonapi.MarshallParams{})
assert.NoError(t, err)
expected := `{"data":{"type":"foo","id":"123","attributes":{"name":"foo"},"relationships":{"bar":{"data":{"type":"bar","id":"456"}}},"meta":{"meta":"foo"}},"included":[{"type":"bar","id":"456","attributes":{"name":"bar"},"meta":{"meta":"bar"}}]}`
assert.Equal(t, fmt.Sprintf("%s\n", expected), string(mFoo))
})
t.Run("test success with metadata", func(t *testing.T) {
t.Parallel()
foo := model.CreateTestFoo()
mFoo, err := fxjsonapi.Marshall(&foo, fxjsonapi.MarshallParams{
Metadata: map[string]interface{}{"foo": "bar"},
})
assert.NoError(t, err)
expected := `{"data":{"type":"foo","id":"123","attributes":{"name":"foo"},"relationships":{"bar":{"data":{"type":"bar","id":"456"}}},"meta":{"meta":"foo"}},"included":[{"type":"bar","id":"456","attributes":{"name":"bar"},"meta":{"meta":"bar"}}],"meta":{"foo":"bar"}}`
assert.Equal(t, fmt.Sprintf("%s\n", expected), string(mFoo))
})
t.Run("test success without included", func(t *testing.T) {
t.Parallel()
foo := model.CreateTestFoo()
mFoo, err := fxjsonapi.Marshall(&foo, fxjsonapi.MarshallParams{
WithoutIncluded: true,
})
assert.NoError(t, err)
expected := `{"data":{"type":"foo","id":"123","attributes":{"name":"foo"},"relationships":{"bar":{"data":{"type":"bar","id":"456"}}},"meta":{"meta":"foo"}}}`
assert.Equal(t, fmt.Sprintf("%s\n", expected), string(mFoo))
})
}