-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevent_test.go
100 lines (88 loc) · 2.77 KB
/
event_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
package gomessagestore_test
import (
"reflect"
"testing"
. "github.com/blackhatbrigade/gomessagestore"
"github.com/blackhatbrigade/gomessagestore/repository"
)
func getSampleEventMissing(key string) Event {
event := getSampleEvent()
switch key {
case "ID":
event.ID = NilUUID
case "MessageType":
event.MessageType = ""
case "EntityID":
event.EntityID = NilUUID
case "StreamCategory":
event.StreamCategory = ""
case "Data":
event.Data = nil
}
return event
}
func getSampleEventMalformed(key string) Event {
event := getSampleEvent()
switch key {
case "CategoryHyphen":
event.StreamCategory = "something-bad"
}
return event
}
//TestEventToEnvelope tests event.ToEnvelope
func TestEventToEnvelope(t *testing.T) {
tests := []struct {
name string
inputEvent Event
expectedEnvelope *repository.MessageEnvelope
expectedError error
failEnvMessage string
failErrMessage string
}{{
name: "Returns message envelope",
inputEvent: getSampleEvent(),
failEnvMessage: "Didn't render the MessageEnvelope correctly",
expectedEnvelope: getSampleEventAsEnvelope(),
}, {
name: "Errors if no ID",
inputEvent: getSampleEventMissing("ID"),
expectedError: ErrMessageNoID,
failErrMessage: "Expected a NEW ID for Event",
}, {
name: "Errors if no EntityID",
inputEvent: getSampleEventMissing("EntityID"),
expectedError: ErrMissingMessageCategoryID,
failErrMessage: "Expected a NEW ID for Event",
}, {
name: "Errors if a hyphen is present in the StreamCategory name",
inputEvent: getSampleEventMalformed("CategoryHyphen"),
expectedError: ErrInvalidMessageCategory,
failErrMessage: "Hyphen not allowed in StreamCategory name",
}, {
name: "Errors if the category is left blank",
inputEvent: getSampleEventMissing("StreamCategory"),
expectedError: ErrMissingMessageCategory,
failErrMessage: "StreamCategory Name must not be blank",
}, {
name: "Errors if data is nil",
inputEvent: getSampleEventMissing("Data"),
expectedError: ErrMissingMessageData,
failErrMessage: "Data must not be nil",
}, {
name: "Errors if MessageType is left blank",
inputEvent: getSampleEventMissing("MessageType"),
expectedError: ErrMissingMessageType,
failErrMessage: "MessageType must not be empty",
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
msgEnv, err := test.inputEvent.ToEnvelope()
if err != test.expectedError {
t.Errorf("Err: %s\nExpected: %v\nActual: %v\n", test.failErrMessage, test.expectedError, err)
}
if !reflect.DeepEqual(msgEnv, test.expectedEnvelope) {
t.Errorf("Err: %s\nExpected: %v\nActual: %v\n", test.failEnvMessage, test.expectedEnvelope, msgEnv)
}
})
}
}