-
Notifications
You must be signed in to change notification settings - Fork 20
/
event_fetcher_integration_test.go
149 lines (124 loc) · 4.44 KB
/
event_fetcher_integration_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
package overflow
import (
"io/fs"
"os"
"testing"
"time"
"github.com/hexops/autogold"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func startOverflowAndMintTokens(t *testing.T) *OverflowState {
t.Helper()
o, err := OverflowTesting()
require.NoError(t, err)
result := o.Tx("mint_tokens", WithSignerServiceAccount(), WithArg("recipient", "first"), WithArg("amount", 100.0))
assert.NoError(t, result.Err)
return o
}
type MarketEvent struct {
EventDate time.Time `json:"eventDate"`
FlowEventID string `json:"flowEventId"`
FlowTransactionID string `json:"flowTransactionId"`
ID string `json:"id"`
BlockEventData struct {
Amount float64 `json:"amount"`
} `json:"blockEventData"`
}
func TestIntegrationEventFetcher(t *testing.T) {
t.Run("Test that from index cannot be negative", func(t *testing.T) {
_, err := startOverflowAndMintTokens(t).FetchEvents(
WithEndIndex(2),
WithFromIndex(-10),
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
)
assert.Error(t, err)
assert.Contains(t, err.Error(), "FromIndex is negative")
})
t.Run("Fetch last events", func(t *testing.T) {
ev, err := startOverflowAndMintTokens(t).FetchEvents(
WithLastBlocks(2),
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
)
assert.NoError(t, err)
assert.Equal(t, 1, len(ev))
})
t.Run("Fetch last events and sort them ", func(t *testing.T) {
o := startOverflowAndMintTokens(t)
result := o.Tx("mint_tokens", WithSignerServiceAccount(), WithArg("recipient", "first"), WithArg("amount", "100.0"))
assert.NoError(t, result.Err)
ev, err := o.FetchEvents(
WithLastBlocks(3),
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
)
assert.NoError(t, err)
assert.Equal(t, 2, len(ev))
assert.True(t, ev[0].BlockHeight < ev[1].BlockHeight)
})
t.Run("Fetch last write progress file", func(t *testing.T) {
ev, err := startOverflowAndMintTokens(t).FetchEvents(
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
WithTrackProgressIn("progress"),
)
defer os.Remove("progress")
assert.NoError(t, err)
assert.Equal(t, 1, len(ev))
assert.Contains(t, ev[0].String(), "100")
})
t.Run("should fail reading invalid progress from file", func(t *testing.T) {
err := os.WriteFile("progress", []byte("invalid"), fs.ModePerm)
assert.NoError(t, err)
_, err = startOverflowAndMintTokens(t).FetchEvents(
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
WithTrackProgressIn("progress"),
)
defer os.Remove("progress")
assert.Error(t, err)
assert.Equal(t, "could not parse progress file as block height strconv.ParseInt: parsing \"invalid\": invalid syntax", err.Error())
})
t.Run("Fetch last write progress file that exists and marshal events", func(t *testing.T) {
err := os.WriteFile("progress", []byte("1"), fs.ModePerm)
assert.NoError(t, err)
ev, err := startOverflowAndMintTokens(t).FetchEvents(
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
WithTrackProgressIn("progress"),
)
defer os.Remove("progress")
assert.NoError(t, err)
assert.Equal(t, 3, len(ev))
event := ev[0]
graffleEvent := event.ToGraffleEvent()
var eventMarshal map[string]interface{}
assert.NoError(t, event.MarshalAs(&eventMarshal))
assert.NotEmpty(t, eventMarshal)
autogold.Equal(t, graffleEvent.BlockEventData, autogold.Name("graffle-event"))
var marshalTo MarketEvent
assert.NoError(t, graffleEvent.MarshalAs(&marshalTo))
assert.Equal(t, float64(10), marshalTo.BlockEventData.Amount)
})
t.Run("Return progress writer ", func(t *testing.T) {
progressFile := "progress"
err := writeProgressToFile(progressFile, 0)
require.NoError(t, err)
res := startOverflowAndMintTokens(t).FetchEventsWithResult(
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
WithTrackProgressIn(progressFile),
WithReturnProgressWriter(),
)
require.NoError(t, res.Error)
want := autogold.Want("eventProgress", `Fetched number=1 of events within from=11 block to=11 for events=A.0ae53cb6e3f42a79.FlowToken.TokensMinted
`)
want.Equal(t, res.String())
progress, err := readProgressFromFile(progressFile)
require.NoError(t, err)
assert.Equal(t, int64(0), progress)
res.ProgressWriteFunction()
progress, err = readProgressFromFile(progressFile)
require.NoError(t, err)
assert.Equal(t, int64(12), progress)
ev := res.Events
defer os.Remove(progressFile)
assert.Equal(t, 1, len(ev))
assert.Contains(t, ev[0].String(), "100")
})
}