-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogmessage_test.go
105 lines (94 loc) · 3.54 KB
/
logmessage_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
package gonyan
import (
"bytes"
"testing"
"time"
)
// TestSerialise verifies proper serialisation without
// custom metadata fields.
func TestSerialise(t *testing.T) {
date := time.Date(2017, time.January, 3, 10, 23, 34, 200, time.UTC).UnixNano()
logMessage := NewLogMessage("Test", date, "messagestring", nil)
serialised, err := logMessage.Serialise()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if serialised == nil {
t.Fatalf("Serialised is nil")
}
if bytes.Compare(serialised, []byte(`{"tag":"Test","timestamp":1483439014000000200,"message":"messagestring"}`)) != 0 {
t.Fatalf("Serialisation error, unexpected serialised log: %s", serialised)
}
}
// TestSerialiseWithMetadata verifies proper serialisation with
// custom metadata fields.
func TestSerialiseWithMetadata(t *testing.T) {
metadata := map[string]string{
"custom": "field",
}
date := time.Date(2017, time.January, 3, 10, 23, 34, 200, time.UTC).UnixNano()
logMessage := NewLogMessage("Test", date, "messagestring", metadata)
serialised, err := logMessage.Serialise()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if serialised == nil {
t.Fatalf("Serialised is nil")
}
expected := `{"tag":"Test","timestamp":1483439014000000200,"message":"messagestring","metadata":{"custom":"field"}}`
if bytes.Compare(serialised, []byte(expected)) != 0 {
t.Fatalf("Serialisation error, unexpected serialised log: %s", serialised)
}
}
// TestDeserialise verifies proper LogMessage deserialisation.
func TestDeserialise(t *testing.T) {
serialised := []byte(`{"tag":"Test","timestamp":1483439014000000200,"message":"messagestring"}`)
logMessage, err := Deserialise(serialised)
if err != nil {
t.Fatalf("Unexpected deserialisation error: %s", err.Error())
}
if logMessage.Tag != "Test" {
t.Fatalf("Unexpected Tag found. Expected: %s - Found: %s", "Test", logMessage.Tag)
}
if logMessage.Message != "messagestring" {
t.Fatalf("Unexpected Message found. Expected: %s - Found: %s", "messagestring", logMessage.Message)
}
if logMessage.Timestamp != 1483439014000000200 {
t.Fatalf("Unexpected Timestamp found. Expected: %d - Found: %d", 1483439014000000200, logMessage.Timestamp)
}
date := time.Unix(0, logMessage.Timestamp).UTC()
if date.Day() != 3 {
t.Fatalf("Unexpected Timestamp Day found. Expected: %d - Found: %d", 3, date.Day())
}
if date.Month() != time.January {
t.Fatalf("Unexpected Timestamp Month found. Expected: %s - Found: %s", time.January.String(), date.Month().String())
}
if date.Year() != 2017 {
t.Fatalf("Unexpected Timestamp Year found. Expected: %d - Found: %d", 2017, date.Year())
}
if date.Hour() != 10 {
t.Fatalf("Unexpected Timestamp Hour found. Expected: %d - Found: %d", 10, date.Hour())
}
if date.Minute() != 23 {
t.Fatalf("Unexpected Timestamp Minute found. Expected: %d - Found: %d", 23, date.Minute())
}
if date.Second() != 34 {
t.Fatalf("Unexpected Timestamp Second found. Expected: %d - Found: %d", 34, date.Second())
}
if date.Nanosecond() != 200 {
t.Fatalf("Unexpected Timestamp Nanosecond found. Expected: %d - Found: %d", 200, date.Nanosecond())
}
}
// TestDeserialisationFailure verifies that, if provided an invalid
// LogMessage, the Deserialisation function correctly fails.
func TestDeserialisationFailure(t *testing.T) {
// Invalid JSON format.
input := "{this-is-not-a-json}"
message, err := Deserialise([]byte(input))
if message != nil {
t.Fatalf("Deserialisation should have returned nil => %+v", message)
}
if err == nil {
t.Fatalf("Deserialisation should have failed for input: `%s`", input)
}
}