-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_test.go
141 lines (126 loc) · 3.51 KB
/
message_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package types
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestSource_MarshalBinary(t *testing.T) {
var empty Source
for _, test := range []struct {
name string
s Source
expect []byte
}{
{"Empty Source marshals to 0x0", empty, []byte("0")},
{"Autoresponse marshals to 0x2", SourceAutoresponse, []byte("2")},
} {
t.Run(test.name, func(t *testing.T) {
received, _ := test.s.MarshalBinary()
if !cmp.Equal(test.expect, received) {
t.Error(cmp.Diff(test.expect, received))
}
})
}
}
func TestSource_UnmarshalBinary(t *testing.T) {
for _, test := range []struct {
name string
s []byte
expect Source
}{
{"Empty Source unmarshals from 0x0", []byte{}, SourceUnknown},
{"Garbage/ broken data unmarshals to Unknown", []byte("hello, world!"), SourceUnknown},
{"Autoresponse unmarshals from 0x2", []byte("2"), SourceAutoresponse},
} {
t.Run(test.name, func(t *testing.T) {
var received Source
_ = received.UnmarshalBinary(test.s)
if !cmp.Equal(test.expect, received) {
t.Error(cmp.Diff(test.expect, received))
}
})
}
}
func TestNewMessage(t *testing.T) {
m := NewMessage(SourceAutoresponse, "some-id", "Hello, world!")
for _, test := range []struct {
name string
expect any
got any
}{
{"Source", SourceAutoresponse, m.Source},
{"ID", "some-id", m.ID},
{"Message", "Hello, world!", m.Message},
} {
t.Run(test.name, func(t *testing.T) {
if test.expect != test.got {
t.Errorf("expected %#v, received %#v", test.expect, test.got)
}
})
}
}
func TestParseMessage(t *testing.T) {
for _, test := range []struct {
name string
m map[string]interface{}
expect Message
expectErr bool
}{
{"Bad types", map[string]interface{}{"source": true, "ts": "invalid"}, Message{}, true},
{"Empty message", map[string]interface{}{}, Message{}, false},
{"Happy path", map[string]interface{}{"source": 2, "ts": 0, "id": "some-id", "msg": "<3", "sentiment": 2}, Message{
Source: SourceAutoresponse,
ID: "some-id",
Timestamp: 0,
Message: "<3",
Sentiment: 2,
}, false},
{"Happy path, from redis", map[string]interface{}{"source": "2", "ts": "0", "id": "some-id", "msg": "<3", "sentiment": "3"}, Message{
Source: SourceAutoresponse,
ID: "some-id",
Timestamp: 0,
Message: "<3",
Sentiment: 3,
}, false},
} {
t.Run(test.name, func(t *testing.T) {
m, err := ParseMessage(test.m)
if err == nil && test.expectErr {
t.Errorf("expected error")
} else if err != nil && !test.expectErr {
t.Errorf("unexpected error: %+v", err)
}
if !cmp.Equal(test.expect, m) {
t.Error(cmp.Diff(test.expect, m))
}
})
}
}
func TestMessage_Map(t *testing.T) {
for _, test := range []struct {
name string
m Message
expect map[string]any
}{
{"Empty message", Message{}, map[string]any{"id": "", "msg": "", "source": Source(0), "ts": int64(0), "sentiment": 0}},
{"Happy path", Message{
Source: SourceAutoresponse,
ID: "some-id",
Timestamp: 0,
Message: "<3",
}, map[string]any{"source": Source(2), "ts": int64(0), "id": "some-id", "msg": "<3", "sentiment": 0}},
} {
t.Run(test.name, func(t *testing.T) {
m := test.m.Map()
if !cmp.Equal(test.expect, m) {
t.Error(cmp.Diff(test.expect, m))
}
})
}
}
func TestMessage_GetTimestamp(t *testing.T) {
expect := "2022-10-02 18:31:15 +0000 UTC"
got := Message{Timestamp: 1664735475}.GetTimestamp().UTC().String()
if expect != got {
t.Errorf("expected %q, received %q", expect, got)
}
}