-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent_feed_test.go
289 lines (218 loc) · 8.74 KB
/
event_feed_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package fauna_test
import (
"testing"
"time"
"github.com/fauna/fauna-go/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEventFeed(t *testing.T) {
t.Setenv(fauna.EnvFaunaEndpoint, fauna.EndpointLocal)
t.Setenv(fauna.EnvFaunaSecret, "secret")
client, clientErr := fauna.NewDefaultClient()
require.NoError(t, clientErr)
resetCollection(t, client)
t.Run("returns errors correctly", func(t *testing.T) {
t.Run("should error when the query doesn't return an event source", func(t *testing.T) {
query, queryErr := fauna.FQL(`42`, nil)
require.NoError(t, queryErr)
_, feedErr := client.FeedFromQuery(query)
require.ErrorContains(t, feedErr, "query should return a fauna.EventSource but got int")
})
t.Run("should allow passing a cursor with a query", func(t *testing.T) {
query, queryErr := fauna.FQL(`EventFeedTest.all().eventSource()`, nil)
require.NoError(t, queryErr, "failed to create a query for EventSource")
feed, feedErr := client.FeedFromQuery(query, fauna.EventFeedCursor("cursor"))
require.NoError(t, feedErr, "failed to init events feed")
require.NotNil(t, feed, "feed is nil")
})
t.Run("should error when attempting to use a start time and a cursor", func(t *testing.T) {
query, queryErr := fauna.FQL(`EventFeedTest.all().eventSource()`, nil)
require.NoError(t, queryErr, "failed to create a query for EventSource")
req, reqErr := client.Query(query)
require.NoError(t, reqErr, "failed to execute query")
var response fauna.EventSource
unmarshalErr := req.Unmarshal(&response)
require.NoError(t, unmarshalErr, "failed to unmarshal EventSource")
_, feedErr := client.Feed(response, fauna.EventFeedStartTime(time.Now()), fauna.EventFeedCursor("cursor"))
require.ErrorContains(t, feedErr, "cannot use EventFeedStartTime and EventFeedCursor together")
})
})
t.Run("can use event feeds from a query", func(t *testing.T) {
query, queryErr := fauna.FQL(`EventFeedTest.all().eventSource()`, nil)
require.NoError(t, queryErr, "failed to create a query for EventSource")
feed, feedErr := client.FeedFromQuery(query)
require.NoError(t, feedErr, "failed to init events feed")
var (
start = 5
end = 20
)
createOne(t, client, feed)
createMultipleDocs(t, client, start, end)
var page fauna.FeedPage
eventsErr := feed.Next(&page)
require.NoError(t, eventsErr, "failed to get events from EventSource")
require.Equal(t, end-start, len(page.Events), "unexpected number of events")
})
t.Run("can get events from EventSource", func(t *testing.T) {
t.Run("can get an EventSource", func(t *testing.T) {
eventSource := getEventSource(t, client)
require.NotNil(t, eventSource, "failed to get an EventSource")
})
t.Run("get events from an EventSource", func(t *testing.T) {
eventSource := getEventSource(t, client)
feed, feedErr := client.Feed(eventSource)
require.NoError(t, feedErr, "failed to init events feed")
var (
start = 5
end = 20
)
createOne(t, client, feed)
createMultipleDocs(t, client, start, end)
var page fauna.FeedPage
eventsErr := feed.Next(&page)
require.NoError(t, eventsErr, "failed to get events from EventSource")
require.Equal(t, end-start, len(page.Events), "unexpected number of events")
})
})
t.Run("can get events from history", func(t *testing.T) {
resetCollection(t, client)
createOne(t, client, nil)
eventSource := getEventSource(t, client)
require.NotNil(t, eventSource, "failed to get an EventSource")
feed, feedErr := client.Feed(eventSource)
require.NoError(t, feedErr, "failed to init events feed")
var page fauna.FeedPage
eventsErr := feed.Next(&page)
require.NoError(t, eventsErr, "failed to get events")
require.Equal(t, 0, len(page.Events), "unexpected number of events")
eventSource = getEventSource(t, client)
require.NotNil(t, eventSource, "failed to get an EventSource")
feed, feedErr = client.Feed(eventSource, fauna.EventFeedStartTime(time.Now().Add(-10*time.Minute)))
require.NoError(t, feedErr, "failed to init events feed")
eventsErr = feed.Next(&page)
require.NoError(t, eventsErr, "failed to get events")
require.Equal(t, 1, len(page.Events), "unexpected number of events")
require.NotNil(t, feed)
// get a blank page
for {
eventErr := feed.Next(&page)
require.NoError(t, eventErr)
break
}
require.Empty(t, page.Events)
})
t.Run("can use page size", func(t *testing.T) {
resetCollection(t, client)
eventSource := getEventSource(t, client)
pageSize := 3
feed, feedErr := client.Feed(eventSource, fauna.EventFeedPageSize(pageSize))
require.NoError(t, feedErr, "failed to init events feed")
var (
start = 5
end = 20
page fauna.FeedPage
seenEvents int
)
createOne(t, client, feed)
createMultipleDocs(t, client, start, end)
didPaginate := false
for {
eventsErr := feed.Next(&page)
require.NoError(t, eventsErr, "failed to get events from EventSource")
seenEvents += len(page.Events)
if !page.HasNext {
break
}
didPaginate = true
// every page but the last should have the right page size
require.Equal(t, pageSize, len(page.Events), "unexpected number of events")
}
require.True(t, didPaginate, "expected to have called for multiple event pages")
require.Equal(t, end-start, seenEvents, "unexpected number of events")
})
t.Run("can unmarshall events", func(t *testing.T) {
resetCollection(t, client)
createEvent := func(v string) {
createOneQuery, createOneQueryErr := fauna.FQL(`EventFeedTest.create({ foo: ${v} })`, map[string]any{"v": v})
require.NoError(t, createOneQueryErr, "failed to init query for create statement")
require.NotNil(t, createOneQuery, "create statement is nil")
_, createOneErr := client.Query(createOneQuery)
require.NoError(t, createOneErr, "failed to create a document")
}
query, queryErr := fauna.FQL(`EventFeedTest.all().eventSource()`, nil)
require.NoError(t, queryErr, "failed to create a query for EventSource")
feed, feedErr := client.FeedFromQuery(query)
require.NoError(t, feedErr, "failed to init events feed")
createEvent("bar")
createEvent("baz")
createEvent("bak")
type TestEvent struct {
Foo string `fauna:"foo"`
}
var (
page fauna.FeedPage
testEvent TestEvent
)
events := make([]TestEvent, 0, 3)
for {
eventsErr := feed.Next(&page)
require.NoError(t, eventsErr, "failed to get page of events")
for _, event := range page.Events {
err := event.Unmarshal(&testEvent)
require.NoError(t, err, "error unmarshalling event")
events = append(events, testEvent)
}
if !page.HasNext {
break
}
}
require.Equal(t, []TestEvent{TestEvent{Foo: "bar"}, TestEvent{Foo: "baz"}, TestEvent{Foo: "bak"}}, events)
})
}
func resetCollection(t *testing.T, client *fauna.Client) {
t.Helper()
setupQuery, setupQueryErr := fauna.FQL(`Collection.byName("EventFeedTest")?.delete()
Collection.create({ name: "EventFeedTest" })`, nil)
require.NoError(t, setupQueryErr, "setup query error: %s", setupQueryErr)
_, setupErr := client.Query(setupQuery)
require.NoError(t, setupErr, "setup error: %s", setupErr)
}
func getEventSource(t *testing.T, client *fauna.Client) fauna.EventSource {
t.Helper()
query, queryErr := fauna.FQL(`EventFeedTest.all().eventSource()`, nil)
require.NoError(t, queryErr, "failed to create a query for EventSource")
feedRes, feedResErr := client.Query(query)
require.NoError(t, feedResErr, "failed to init events feed")
var eventSource fauna.EventSource
unmarshalErr := feedRes.Unmarshal(&eventSource)
require.NoError(t, unmarshalErr, "failed to unmarshal EventSource")
require.NotNil(t, eventSource, "event source is nil")
require.NotEmpty(t, eventSource, "event source is empty")
return eventSource
}
func createOne(t *testing.T, client *fauna.Client, feed *fauna.EventFeed) {
t.Helper()
createOneQuery, createOneQueryErr := fauna.FQL("EventFeedTest.create({ foo: 'bar' })", nil)
require.NoError(t, createOneQueryErr, "failed to init query for create statement")
require.NotNil(t, createOneQuery, "create statement is nil")
_, createOneErr := client.Query(createOneQuery)
require.NoError(t, createOneErr, "failed to create a document")
if feed == nil {
return
}
var page fauna.FeedPage
eventsErr := feed.Next(&page)
require.NoError(t, eventsErr, "failed to get events")
assert.Equal(t, 1, len(page.Events), "unexpected number of events")
}
func createMultipleDocs(t *testing.T, client *fauna.Client, start int, end int) {
t.Helper()
query, queryErr := fauna.FQL(`Set.sequence(${start}, ${end}).forEach(n => EventFeedTest.create({ n: n }))`, map[string]any{
"start": start,
"end": end,
})
require.NoError(t, queryErr, "failed to init query for create statement")
_, err := client.Query(query)
require.NoError(t, err)
}