-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata_test.go
112 lines (106 loc) · 2.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package metadata
import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
"testing"
)
type Data struct {
ID int `annotation:"id"`
Name string `annotation:"name"`
Age uint `label:"age"`
Skills []string `label:"skills"`
}
func TestUnmarshal(t *testing.T) {
type args struct {
metadata v1.ObjectMeta
prefix string
}
tests := []struct {
name string
args args
want Data
wantErr bool
}{
{
name: "",
args: args{
metadata: v1.ObjectMeta{
Annotations: map[string]string{
"prefix/id": "1",
"prefix/name": "John",
},
Labels: map[string]string{
"prefix/age": "30",
"prefix/skills": "cooking,swimming,driving",
},
},
prefix: "prefix",
},
want: Data{
ID: 1,
Name: "John",
Age: 30,
Skills: []string{"cooking", "swimming", "driving"},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data := Data{}
if err := Unmarshal(tt.args.metadata, &data, tt.args.prefix); (err != nil) != tt.wantErr {
t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && !reflect.DeepEqual(data, tt.want) {
t.Errorf("Unmarshal() struct = %v, want %v", data, tt.want)
}
})
}
}
func TestMarshall(t *testing.T) {
type args struct {
data Data
prefix string
}
tests := []struct {
name string
args args
want v1.ObjectMeta
wantErr bool
}{
{
name: "",
args: args{
data: Data{
ID: 1,
Name: "John",
Age: 30,
Skills: []string{"cooking", "swimming", "driving"},
},
prefix: "prefix",
},
want: v1.ObjectMeta{
Annotations: map[string]string{
"prefix/id": "1",
"prefix/name": "John",
},
Labels: map[string]string{
"prefix/age": "30",
"prefix/skills": "cooking,swimming,driving",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
metadata := v1.ObjectMeta{}
if err := Marshal(&metadata, &tt.args.data, tt.args.prefix); (err != nil) != tt.wantErr {
t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && !reflect.DeepEqual(metadata, tt.want) {
t.Errorf("Marshal() struct = %v, want %v", metadata, tt.want)
}
})
}
}