forked from mfcochauxlaberge/jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunmarshaling_test.go
More file actions
107 lines (78 loc) · 2.18 KB
/
unmarshaling_test.go
File metadata and controls
107 lines (78 loc) · 2.18 KB
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
package jsonapi
import (
"testing"
"github.com/kkaribu/tchek"
)
func TestUnmarshalResource(t *testing.T) {
reg := NewMockRegistry()
res1 := Wrap(&MockType3{
ID: "mt1",
Attr1: "a string",
Attr2: 1,
Rel1: "mt2",
Rel2: []string{"mt3", "mt4"},
})
url1, err := ParseRawURL(reg, "/mocktypes3/mt1")
tchek.UnintendedError(err)
meta1 := map[string]interface{}{
"str": "a string\\^ç\"",
"num": float64(42),
"b": true,
}
doc1 := NewDocument()
doc1.Data = res1
doc1.Meta = meta1
body1, err := Marshal(doc1, url1)
tchek.UnintendedError(err)
pl1, err := Unmarshal(body1, url1, reg)
tchek.UnintendedError(err)
dst1 := pl1.Data.(Resource)
tchek.HaveEqualAttributes(t, "same attribues", res1, dst1)
tchek.AreEqual(t, "same meta object", meta1, pl1.Meta)
}
func TestUnmarshalIdentifier(t *testing.T) {
reg := NewMockRegistry()
id1 := Identifier{ID: "abc123", Type: "mocktypes1"}
url1, err := ParseRawURL(reg, "/mocktypes3/mt1/relationships/rel1")
tchek.UnintendedError(err)
meta1 := map[string]interface{}{
"str": "a string\\^ç\"",
"num": float64(42),
"b": true,
}
doc1 := NewDocument()
doc1.Data = id1
doc1.Meta = meta1
body1, err := Marshal(doc1, url1)
tchek.UnintendedError(err)
pl1, err := Unmarshal(body1, url1, reg)
tchek.UnintendedError(err)
dst1 := pl1.Data.(Identifier)
tchek.AreEqual(t, "same identifier", id1, dst1)
tchek.AreEqual(t, "same meta map", meta1, pl1.Meta)
}
func TestUnmarshalIdentifiers(t *testing.T) {
reg := NewMockRegistry()
ids1 := Identifiers{
Identifier{ID: "abc123", Type: "mocktypes1"},
Identifier{ID: "def456", Type: "mocktypes1"},
Identifier{ID: "ghi789", Type: "mocktypes1"},
}
url1, err := ParseRawURL(reg, "/mocktypes3/mt1/relationships/rel2")
tchek.UnintendedError(err)
meta1 := map[string]interface{}{
"str": "a string\\^ç\"",
"num": float64(42),
"b": true,
}
doc1 := NewDocument()
doc1.Data = ids1
doc1.Meta = meta1
body1, err := Marshal(doc1, url1)
tchek.UnintendedError(err)
pl1, err := Unmarshal(body1, url1, reg)
tchek.UnintendedError(err)
dst1 := pl1.Data.(Identifiers)
tchek.AreEqual(t, "same identifiers", ids1, dst1)
tchek.AreEqual(t, "same meta map", meta1, pl1.Meta)
}