-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreader_test.go
More file actions
89 lines (72 loc) · 2.28 KB
/
reader_test.go
File metadata and controls
89 lines (72 loc) · 2.28 KB
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
package goro_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/dghubble/sling"
"github.com/vectorhacker/goro"
"github.com/gorilla/pat"
"github.com/stretchr/testify/assert"
)
func TestReader(t *testing.T) {
generateEvents := func(count int) goro.Events {
events := make(goro.Events, count)
for i := range events {
events[i] = goro.Event{
ID: goro.NewUUID(),
Type: "deposit",
Data: []byte("{\"double\":\"trouble\"}"),
}
}
return events
}
t.Run("it should read forwards 2 pages", func(t *testing.T) {
mux := pat.New()
mux.Get("/streams/{stream}/{start}/{direction}/{pageSize}", func(w http.ResponseWriter, r *http.Request) {
stream := r.URL.Query().Get(":stream")
start, err := strconv.Atoi(r.URL.Query().Get(":start"))
direction := r.URL.Query().Get(":direction")
assert.Nil(t, err)
assert.Equal(t, "forward", direction)
assert.Equal(t, "test", stream)
assert.True(t, start == 0 || start == 10)
err = json.NewEncoder(w).Encode(map[string]interface{}{
"entries": generateEvents(10),
})
assert.Nil(t, err)
})
s := httptest.NewServer(mux)
r := goro.NewForwardsReader(goro.SlingerFunc(func() *sling.Sling {
return sling.New().Base(s.URL).Client(s.Client()).New()
}), "test")
events, err := r.Read(context.Background(), 0, 20)
assert.Nil(t, err)
assert.Len(t, events, 20)
})
t.Run("it should read backwards 2 pages", func(t *testing.T) {
mux := pat.New()
mux.Get("/streams/{stream}/{start}/{direction}/{pageSize}", func(w http.ResponseWriter, r *http.Request) {
stream := r.URL.Query().Get(":stream")
start, err := strconv.Atoi(r.URL.Query().Get(":start"))
direction := r.URL.Query().Get(":direction")
assert.Nil(t, err)
assert.Equal(t, "backward", direction)
assert.Equal(t, "test", stream)
assert.True(t, start == 21 || start == 11)
err = json.NewEncoder(w).Encode(map[string]interface{}{
"entries": generateEvents(10),
})
assert.Nil(t, err)
})
s := httptest.NewServer(mux)
r := goro.NewBackwardsReader(goro.SlingerFunc(func() *sling.Sling {
return sling.New().Base(s.URL).Client(s.Client()).New()
}), "test")
events, err := r.Read(context.Background(), 20, 20)
assert.Nil(t, err)
assert.Len(t, events, 20)
})
}