-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels_test.go
120 lines (110 loc) · 2.18 KB
/
models_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
113
114
115
116
117
118
119
120
package brandalert
import (
"encoding/json"
"testing"
)
// TestTime tests the time conversion functions.
func TestTime(t *testing.T) {
tests := []struct {
name string
decErr string
encErr string
}{
{
name: `"2006-01-02"`,
decErr: "",
encErr: "",
},
{
name: `"2006-01-02T15:04:05Z08:00"`,
decErr: `parsing time "2006-01-02T15:04:05Z08:00": extra text: "T15:04:05Z08:00"`,
encErr: "",
},
{
name: `""`,
decErr: "",
encErr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var v Time
err := json.Unmarshal([]byte(tt.name), &v)
checkErr(t, err, tt.decErr)
if tt.decErr != "" {
return
}
bb, err := json.Marshal(v)
checkErr(t, err, tt.encErr)
if tt.encErr != "" {
return
}
if string(bb) != tt.name {
t.Errorf("got = %v, want %v", string(bb), tt.name)
}
})
}
}
// TestMessages tests the Messages conversion functions.
func TestMessages(t *testing.T) {
tests := []struct {
name string
want string
decErr string
encErr string
}{
{
name: `["Message1","Message2"]`,
want: `["Message1","Message2"]`,
decErr: "",
encErr: "",
},
{
name: `"Message"`,
want: `["Message"]`,
decErr: "",
encErr: "",
},
{
name: `""`,
want: `[""]`,
decErr: "",
encErr: "",
},
{
name: `{"key1":["Message1"],"key2":["Message2"]}`,
want: `["map[key1:[Message1] key2:[Message2]]"]`,
decErr: "",
encErr: "",
},
{
name: `{"key1":["Message1"],"key2":"Message2"}`,
want: `["map[key1:[Message1] key2:Message2]"]`,
decErr: "",
encErr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var v Messages
err := json.Unmarshal([]byte(tt.name), &v)
checkErr(t, err, tt.decErr)
if tt.decErr != "" {
return
}
bb, err := json.Marshal(v)
checkErr(t, err, tt.encErr)
if tt.encErr != "" {
return
}
if string(bb) != tt.want {
t.Errorf("got = %v, want %v", string(bb), tt.want)
}
})
}
}
func checkErr(t *testing.T, err error, want string) {
if (err != nil || want != "") && (err == nil || err.Error() != want) {
t.Errorf("error = %v, wantErr %v", err, want)
}
}