-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
outbox_message_test.go
125 lines (99 loc) · 2.82 KB
/
outbox_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
package outboxer_test
import (
"strings"
"testing"
"github.com/italolelis/outboxer"
)
func TestOutboxMessage(t *testing.T) {
t.Parallel()
tests := []struct {
function func(*testing.T)
scenario string
}{
{
testDynamicValuesScan,
"check if DynamicValues Scan works for message",
},
{
testDynamicValuesValue,
"check if DynamicValues Value works for message",
},
}
for _, test := range tests {
test := test
t.Run(test.scenario, func(t *testing.T) {
test.function(t)
})
}
}
func testDynamicValuesScan(t *testing.T) {
dv := outboxer.DynamicValues{}
if err := dv.Scan([]byte(`{"key": "value"}`)); err != nil {
t.Fatalf("failed to scan DynamicValues value: %s", err)
}
if _, ok := dv["key"]; !ok {
t.Fatal("failed to scan DynamicValue json")
}
if dv["key"] != "value" {
t.Fatal("a string `value` was expected")
}
if err := dv.Scan([]byte(``)); err == nil {
t.Fatal("an error was expected when parsing an empty slice of bytes")
}
if err := dv.Scan(nil); err != nil {
t.Fatal("no error was expected when scanning a nil value")
}
}
func testDynamicValuesValue(t *testing.T) {
dv := outboxer.DynamicValues{}
dv["key"] = "value"
v, err := dv.Value()
if err != nil {
t.Fatalf("failed to get driver.Value from DynamicValues: %s", err)
}
if v == nil {
t.Fatalf("driver.Value is not supposed to be nil: %s", err)
}
dv = outboxer.DynamicValues{}
v, err = dv.Value()
if err != nil {
t.Fatalf("failed to get driver.Value from DynamicValues: %s", err)
}
if v != nil {
t.Fatalf("driver.Value is supposed to be nil: %s", err)
}
}
func TestDynamicValues_Scan_InvalidType(t *testing.T) {
p := &outboxer.DynamicValues{}
// call Scan with an invalid type
err := p.Scan(123)
// assert that the error is not nil
if err == nil {
t.Error("expected an error, but got nil")
}
// assert that the error message contains the expected text
expectedErrMsg := "could not not decode type int -> *outboxer.DynamicValues: failed to decode type"
if !strings.Contains(err.Error(), expectedErrMsg) {
t.Errorf("expected error message to contain %q, but got %q", expectedErrMsg, err.Error())
}
}
func TestDynamicValues_Scan_InvalidSrc(t *testing.T) {
p := &outboxer.DynamicValues{}
// call Scan with an invalid src
err := p.Scan(nil)
// assert that the error is nil
if err != nil {
t.Errorf("expected nil error, but got %s", err)
}
// call Scan with an invalid src
err = p.Scan((*int)(nil))
// assert that the error is not nil
if err == nil {
t.Error("expected an error, but got nil")
}
// assert that the error message contains the expected text
expectedErrMsg := "could not not decode type *int -> *outboxer.DynamicValues: failed to decode type"
if !strings.Contains(err.Error(), expectedErrMsg) {
t.Errorf("expected error message to contain %q, but got %q", expectedErrMsg, err.Error())
}
}