-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmarshal_test.go
96 lines (77 loc) · 2.56 KB
/
unmarshal_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
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
package darknut_test
import (
"encoding/json"
"os"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/ttab/darknut"
"github.com/ttab/newsdoc"
)
type planningItem struct {
UUID uuid.UUID `newsdoc:"uuid"`
Title string `newsdoc:"title"`
Meta planningItemBlock `newsdoc:"meta,type=core/planning-item"`
InternalDescription *descriptionBlock `newsdoc:"meta,type=core/description,role=internal"`
PublicDescription *descriptionBlock `newsdoc:"meta,type=core/description,role=public"`
Assignments []assignmentBlock `newsdoc:"meta,type=core/assignment"`
Deliverables []deliverableLink `newsdoc:"links,rel=deliverable"`
}
type descriptionBlock struct {
Role string `newsdoc:"role"`
Text string `newsdoc:"data.text"`
}
type planningItemBlock struct {
Date *time.Time `newsdoc:"data.date,format=2006-01-02"`
Publish time.Time `newsdoc:"data.publish"`
PublishSlot *int `newsdoc:"data.publish_slot"`
Public bool `newsdoc:"data.public"`
Tentative bool `newsdoc:"data.tentative"`
Urgency int `newsdoc:"data.urgency"`
}
type assignmentBlock struct {
Starts time.Time `newsdoc:"data.starts"`
Ends *time.Time `newsdoc:"data.ends"`
Status string `newsdoc:"data.status"`
FullDay bool `newsdoc:"data.full_day"`
Kind []assignmentKind `newsdoc:"meta,type=core/assignment-kind"`
Assignees []assigneeLink `newsdoc:"links,rel=assignee"`
}
type assignmentKind struct {
Value string `newsdoc:"value"`
}
type assigneeLink struct {
UUID uuid.UUID `newsdoc:"uuid"`
}
type deliverableLink struct {
UUID uuid.UUID `newsdoc:"uuid"`
Type string `newsdoc:"type"`
}
func TestUnmarshalDocument(t *testing.T) {
docData, err := os.ReadFile("testdata/planning.json")
must(t, err, "read planning data")
goldenData, err := os.ReadFile("testdata/planning.golden.json")
must(t, err, "read planning golden data")
var (
doc newsdoc.Document
item planningItem
golden planningItem
)
err = json.Unmarshal(docData, &doc)
must(t, err, "unmarshal NewsDoc data")
err = json.Unmarshal(goldenData, &golden)
must(t, err, "unmarshal NewsDoc data")
err = darknut.UnmarshalDocument(doc, &item)
must(t, err, "unmarshal newsdoc document")
if diff := cmp.Diff(golden, item); diff != "" {
t.Errorf("UnmarshalDocument() mismatch (-want +got):\n%s", diff)
}
}
func must(t *testing.T, err error, msg string) {
t.Helper()
if err == nil {
return
}
t.Fatalf("failed to %s: %v", msg, err)
}