forked from andygrunwald/go-gerrit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_test.go
172 lines (139 loc) · 7.31 KB
/
events_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package gerrit_test
import (
"context"
"net/http"
"testing"
"time"
"github.com/andygrunwald/go-gerrit"
)
var (
fakeEvents = []byte(`
{"submitter":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"newRev":"0000000000000000000000000000000000000000","patchSet":{"number":"1","revision":"0000000000000000000000000000000000000000","parents":["0000000000000000000000000000000000000000"],"ref":"refs/changes/1/1/1","uploader":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"createdOn":1470000000,"author":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"isDraft":false,"kind":"TRIVIAL_REBASE","sizeInsertions":10,"sizeDeletions":0},"change":{"project":"test","branch":"master","id":"Iffffffffffffffffffffffffffffffffffffffff","number":"1","subject":"subject","owner":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"url":"https://localhost/1","commitMessage":"commitMessage\n\nline2\n\nChange-Id: Iffffffffffffffffffffffffffffffffffffffff\n","status":"MERGED"},"type":"change-merged","eventCreatedOn":1470000000}
{"author":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"comment":"Patch Set 1:\n\n(2 comments)\n\nSome comment","patchSet":{"number":"1","revision":"0000000000000000000000000000000000000000","parents":["0000000000000000000000000000000000000000"],"ref":"refs/changes/1/1/1","uploader":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"createdOn":1470000000,"author":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"isDraft":false,"kind":"REWORK","sizeInsertions":4,"sizeDeletions":-2},"change":{"project":"test","branch":"master","id":"Iffffffffffffffffffffffffffffffffffffffff","number":"1","subject":"subject","owner":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"url":"https://localhost/1","commitMessage":"commitMessage\n\nChange-Id: Iffffffffffffffffffffffffffffffffffffffff\n","status":"NEW"},"type":"comment-added","eventCreatedOn":1470000000}`)
fakeEventsWithError = []byte(`
{"submitter":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"newRev":"0000000000000000000000000000000000000000","patchSet":{"number":"1","revision":"0000000000000000000000000000000000000000","parents":["0000000000000000000000000000000000000000"],"ref":"refs/changes/1/1/1","uploader":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"createdOn":1470000000,"author":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"isDraft":false,"kind":"TRIVIAL_REBASE","sizeInsertions":10,"sizeDeletions":0},"change":{"project":"test","branch":"master","id":"Iffffffffffffffffffffffffffffffffffffffff","number":"1","subject":"subject","owner":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"url":"https://localhost/1","commitMessage":"commitMessage\n\nline2\n\nChange-Id: Iffffffffffffffffffffffffffffffffffffffff\n","status":"MERGED"},"type":"change-merged","eventCreatedOn":1470000000}
{"author":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"comment":"Patch Set 1:\n\n(2 comments)\n\nSome comment","patchSet":{"number":"1","revision":"0000000000000000000000000000000000000000","parents":["0000000000000000000000000000000000000000"],"ref":"refs/changes/1/1/1","uploader":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"createdOn":1470000000,"author":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"isDraft":false,"kind":"REWORK","sizeInsertions":4,"sizeDeletions":-2},"change":{"project":"test","branch":"master","id":"Iffffffffffffffffffffffffffffffffffffffff","number":"1","subject":"subject","owner":{"name":"Foo Bar","email":"[email protected]","username":"fbar"},"url":"https://localhost/1","commitMessage":"commitMessage\n\nChange-Id: Iffffffffffffffffffffffffffffffffffffffff\n","status":"NEW"},"type":"comment-added","eventCreatedOn":1470000000}
{"author":1}`)
)
func TestEventsLogService_GetEvents_NoDateRange(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/plugins/events-log/events/", func(writer http.ResponseWriter, request *http.Request) {
if _, err := writer.Write(fakeEvents); err != nil {
t.Error(err)
}
})
options := &gerrit.EventsLogOptions{}
events, _, _, err := testClient.EventsLog.GetEvents(context.Background(), options)
if err != nil {
t.Error(err)
}
if len(events) != 2 {
t.Error("Expected 2 events")
}
// Basic test
for i, event := range events {
switch i {
case 0:
if event.Type != "change-merged" {
t.Error("Expected event type to be `change-merged`")
}
case 1:
if event.Type != "comment-added" {
t.Error("Expected event type to be `comment-added`")
}
}
}
}
func TestEventsLogService_GetEvents_DateRangeFromAndTo(t *testing.T) {
setup()
defer teardown()
to := time.Now()
from := to.AddDate(0, 0, -7)
testMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
query := request.URL.Query()
fromFormat := from.Format("2006-01-02 15:04:05")
if query.Get("t1") != fromFormat {
t.Errorf("%s != %s", query.Get("t1"), fromFormat)
}
toFormat := to.Format("2006-01-02 15:04:05")
if query.Get("t2") != toFormat {
t.Errorf("%s != %s", query.Get("t2"), toFormat)
}
if _, err := writer.Write(fakeEvents); err != nil {
t.Error(err)
}
})
options := &gerrit.EventsLogOptions{From: from, To: to}
_, _, _, err := testClient.EventsLog.GetEvents(context.Background(), options)
if err != nil {
t.Error(err)
}
}
func TestEventsLogService_GetEvents_DateRangeFromOnly(t *testing.T) {
setup()
defer teardown()
to := time.Now()
from := to.AddDate(0, 0, -7)
testMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
query := request.URL.Query()
fromFormat := from.Format("2006-01-02 15:04:05")
if query.Get("t1") != fromFormat {
t.Errorf("%s != %s", query.Get("t1"), fromFormat)
}
if query.Get("t2") != "" {
t.Error("Did not expect t2 to be set")
}
if _, err := writer.Write(fakeEvents); err != nil {
t.Error(err)
}
})
options := &gerrit.EventsLogOptions{From: from}
_, _, _, err := testClient.EventsLog.GetEvents(context.Background(), options)
if err != nil {
t.Error(err)
}
}
func TestEventsLogService_GetEvents_DateRangeToOnly(t *testing.T) {
setup()
defer teardown()
to := time.Now()
testMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
query := request.URL.Query()
toFormat := to.Format("2006-01-02 15:04:05")
if query.Get("t2") != toFormat {
t.Errorf("%s != %s", query.Get("t2"), toFormat)
}
if query.Get("t1") != "" {
t.Error("Did not expect t1 to be set")
}
if _, err := writer.Write(fakeEvents); err != nil {
t.Error(err)
}
})
options := &gerrit.EventsLogOptions{To: to}
_, _, _, err := testClient.EventsLog.GetEvents(context.Background(), options)
if err != nil {
t.Error(err)
}
}
func TestEventsLogService_GetEvents_UnmarshalError(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
if _, err := writer.Write(fakeEventsWithError); err != nil {
t.Error(err)
}
})
options := &gerrit.EventsLogOptions{IgnoreUnmarshalErrors: true}
events, _, failures, err := testClient.EventsLog.GetEvents(context.Background(), options)
if err != nil {
t.Error(err)
}
if len(failures) != 1 {
t.Error("Expected 1 failures")
}
if len(events) != 2 {
t.Error(len(events))
}
}