-
Notifications
You must be signed in to change notification settings - Fork 731
/
dapr_test.go
151 lines (141 loc) Β· 2.92 KB
/
dapr_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
142
143
144
145
146
147
148
149
150
151
package dapr
import (
"context"
"os"
"testing"
"github.com/open-policy-agent/gatekeeper/v3/pkg/pubsub/connection"
"github.com/stretchr/testify/assert"
)
var testClient connection.Connection
func TestMain(m *testing.M) {
c, f := FakeConnection()
testClient = c
r := m.Run()
f()
if r != 0 {
os.Exit(r)
}
}
func TestNewConnection(t *testing.T) {
tests := []struct {
name string
config interface{}
expected connection.Connection
errorMsg string
}{
{
name: "invalid config",
config: "test",
expected: nil,
errorMsg: "invalid type assertion, config is not in expected format",
},
{
name: "config with missing component",
config: map[string]interface{}{"enableBatching": true},
expected: nil,
errorMsg: "failed to get value of component",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ret, err := NewConnection(context.TODO(), tc.config)
assert.Equal(t, ret, tc.expected)
assert.EqualError(t, err, tc.errorMsg)
})
}
}
func TestDapr_Publish(t *testing.T) {
ctx := context.Background()
type args struct {
ctx context.Context
data interface{}
topic string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "test publish",
args: args{
ctx: ctx,
data: map[string]interface{}{
"test": "test",
},
topic: "test",
},
wantErr: false,
},
{
name: "test publish without data",
args: args{
ctx: ctx,
data: nil,
topic: "test",
},
wantErr: false,
},
{
name: "test publish without topic",
args: args{
ctx: ctx,
data: map[string]interface{}{
"test": "test",
},
topic: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := testClient
if err := r.Publish(tt.args.ctx, tt.args.data, tt.args.topic); (err != nil) != tt.wantErr {
t.Errorf("Dapr.Publish() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDapr_UpdateConnection(t *testing.T) {
tests := []struct {
name string
config interface{}
wantErr bool
}{
{
name: "test update connection",
config: map[string]interface{}{
"component": "foo",
},
wantErr: false,
},
{
name: "test update connection with invalid config",
config: map[string]interface{}{
"foo": "bar",
},
wantErr: true,
},
{
name: "test update connection with nil config",
config: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := testClient
if err := r.UpdateConnection(context.Background(), tt.config); (err != nil) != tt.wantErr {
t.Errorf("Dapr.UpdateConnection() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr {
cmp, ok := tt.config.(map[string]interface{})["component"].(string)
assert.True(t, ok)
tmp, ok := r.(*Dapr)
assert.True(t, ok)
assert.Equal(t, cmp, tmp.pubSubComponent)
}
})
}
}