-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_encoder_test.go
64 lines (51 loc) · 1.21 KB
/
json_encoder_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
package eventstore_test
import (
"reflect"
"testing"
"github.com/aneshas/eventstore"
)
type AnotherEvent struct {
Smth string
}
func TestShouldDecodeEncodedEvent(t *testing.T) {
enc := eventstore.NewJSONEncoder(SomeEvent{}, AnotherEvent{})
decodeEncode(t, enc, SomeEvent{
UserID: "some-user",
})
decodeEncode(t, enc, AnotherEvent{
Smth: "foo",
})
}
func decodeEncode(t *testing.T, enc eventstore.Encoder, e interface{}) {
encoded, err := enc.Encode(e)
if err != nil {
t.Fatalf("%v", err)
}
decoded, err := enc.Decode(encoded)
if err != nil {
t.Fatalf("%v", err)
}
if !reflect.DeepEqual(e, decoded) {
t.Fatalf("event not decoded. want: %#v, got: %#v", e, decoded)
}
}
func TestShouldErrorOutIfMalformedJSON(t *testing.T) {
enc := eventstore.NewJSONEncoder(SomeEvent{}, AnotherEvent{})
_, err := enc.Decode(&eventstore.EncodedEvt{
Data: "malformed-json",
Type: "SomeEvent",
})
if err == nil {
t.Fatal("should error out")
}
}
func TestShouldErrorOutIfEvtTypeUnknown(t *testing.T) {
enc := eventstore.NewJSONEncoder(SomeEvent{}, AnotherEvent{})
_, err := enc.Decode(&eventstore.EncodedEvt{
Data: `{"userId": "123"}`,
Type: "unknown",
})
if err == nil {
t.Fatal("should error out")
}
}