-
Notifications
You must be signed in to change notification settings - Fork 7
/
record.go
188 lines (161 loc) · 4.1 KB
/
record.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package logtest
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/ankorstore/yokai/log"
)
// TestLogRecord is a test log record, composed of attributes.
type TestLogRecord struct {
attributes map[string]interface{}
}
// NewTestLogRecord returns a [TestLogRecord].
func NewTestLogRecord(attributes map[string]interface{}) *TestLogRecord {
return &TestLogRecord{
attributes: attributes,
}
}
// Level returns the level of the [TestLogRecord].
func (r *TestLogRecord) Level() (string, error) {
value, err := r.Attribute(log.Level)
if err != nil {
return "", err
}
if level, ok := value.(string); ok {
return level, nil
} else {
return "", fmt.Errorf("cannot cast level as string")
}
}
// Message returns the message of the [TestLogRecord].
func (r *TestLogRecord) Message() (string, error) {
value, err := r.Attribute(log.Message)
if err != nil {
return "", err
}
if message, ok := value.(string); ok {
return message, nil
} else {
return "", fmt.Errorf("cannot cast message as string")
}
}
// Service returns the service name of the [TestLogRecord].
func (r *TestLogRecord) Service() (string, error) {
value, err := r.Attribute(log.Service)
if err != nil {
return "", err
}
if service, ok := value.(string); ok {
return service, nil
} else {
return "", fmt.Errorf("cannot cast service as string")
}
}
// Time returns the time of the [TestLogRecord].
func (r *TestLogRecord) Time() (time.Time, error) {
value, err := r.Attribute(log.Time)
if err != nil {
return time.Unix(0, 0), err
}
if t, ok := value.(int64); ok {
return time.Unix(t, 0), nil
} else {
return time.Unix(0, 0), fmt.Errorf("cannot cast time as time.Time")
}
}
// Attribute returns an attribute of the [TestLogRecord] by name.
func (r *TestLogRecord) Attribute(name string) (interface{}, error) {
value, ok := r.attributes[name]
if ok {
return value, nil
} else {
return "", fmt.Errorf("attribute %s not found in record", name)
}
}
// MatchAttributes returns true if the [TestLogRecord] exactly matches provided attributes.
//
//nolint:cyclop
func (r *TestLogRecord) MatchAttributes(expectedAttributes map[string]interface{}) bool {
match := true
if len(expectedAttributes) == 0 {
return false
}
for expectedName, expectedValue := range expectedAttributes {
value, ok := r.attributes[expectedName]
if ok {
switch av := value.(type) {
case json.Number:
switch ev := expectedValue.(type) {
case int:
avi, err := av.Int64()
if err != nil {
match = false
break
}
match = match && int(avi) == ev
case float64:
avf, err := av.Float64()
if err != nil {
match = false
break
}
match = match && avf == ev
default:
match = match && av.String() == fmt.Sprintf("%s", ev)
}
case bool:
match = match && fmt.Sprintf("%v", av) == fmt.Sprintf("%v", expectedValue)
default:
match = match && fmt.Sprintf("%s", av) == fmt.Sprintf("%s", expectedValue)
}
} else {
match = false
break
}
}
return match
}
// ContainAttributes returns true if the [TestLogRecord] partially matches provided attributes.
//
//nolint:cyclop
func (r *TestLogRecord) ContainAttributes(expectedAttributes map[string]interface{}) bool {
match := true
if len(expectedAttributes) == 0 {
return false
}
for expectedName, expectedValue := range expectedAttributes {
value, ok := r.attributes[expectedName]
if ok {
switch av := value.(type) {
case json.Number:
switch ev := expectedValue.(type) {
case int:
avi, err := av.Int64()
if err != nil {
match = false
break
}
match = match && int(avi) == ev
case float64:
avf, err := av.Float64()
if err != nil {
match = false
break
}
match = match && avf == ev
default:
match = match && strings.Contains(av.String(), fmt.Sprintf("%s", ev))
}
case bool:
match = match && fmt.Sprintf("%v", av) == fmt.Sprintf("%v", expectedValue)
default:
match = match && strings.Contains(fmt.Sprintf("%s", av), fmt.Sprintf("%s", expectedValue))
}
} else {
match = false
break
}
}
return match
}