-
Notifications
You must be signed in to change notification settings - Fork 3
/
metadata_test.go
91 lines (66 loc) · 1.89 KB
/
metadata_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
package hypercat
import (
"encoding/json"
"testing"
)
func TestRel(t *testing.T) {
rel := Rel{Rel: "relation", Val: "value"}
bytes, err := json.Marshal(rel)
if err != nil {
t.Errorf("Error marshalling Rel: %v", err)
}
expected := `{"rel":"relation","val":"value"}`
if string(bytes) != expected {
t.Errorf("Rel marshalling error, expected '%v', got '%v'", expected, string(bytes))
}
}
func TestRelConstructor(t *testing.T) {
rel := NewRel("relation", "value")
bytes, err := json.Marshal(rel)
if err != nil {
t.Errorf("Error marshalling Rel: %v", err)
}
expected := `{"rel":"relation","val":"value"}`
if string(bytes) != expected {
t.Errorf("Rel marshalling error, expected '%v', got '%v'", expected, string(bytes))
}
}
func TestMetadata(t *testing.T) {
metadata := Metadata{}
if len(metadata) != 0 {
t.Errorf("Item metadata length should be 0")
}
rel := NewRel("relation1", "value")
metadata = append(metadata, *rel)
if len(metadata) != 1 {
t.Errorf("Item metadata length should be 1")
}
}
func TestRelUnmarshalling(t *testing.T) {
str := `{"rel":"relation","val":"value"}`
rel := Rel{}
err := json.Unmarshal([]byte(str), &rel)
if err != nil {
t.Errorf("Unexpected error: %#v", err)
}
if rel.Rel != "relation" {
t.Errorf("Rel unmarshalling error, expected '%v', got '%v'", "relation", rel.Rel)
}
if rel.Val != "value" {
t.Errorf("Rel unmarshalling error, expected '%v', got '%v'", "value", rel.Val)
}
}
func TestMetadataMarshalling(t *testing.T) {
metadata := Metadata{
*NewRel("relation1", "value"),
*NewRel("relation2", "value"),
}
bytes, err := json.Marshal(metadata)
if err != nil {
t.Errorf("Error marshalling Metadata: %v", err)
}
expected := `[{"rel":"relation1","val":"value"},{"rel":"relation2","val":"value"}]`
if string(bytes) != expected {
t.Errorf("Metadata marshalling error, expected '%v', got '%v'", expected, string(bytes))
}
}