-
Notifications
You must be signed in to change notification settings - Fork 1
/
processor_test.go
173 lines (137 loc) · 4.03 KB
/
processor_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
package gstream
import (
"context"
"errors"
"github.com/KumKeeHyun/gstream/state"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFallThroughProcessor(t *testing.T) {
var shouldBeEqualToV int
p := newFallThroughSupplier[int]().
Processor(func(ctx context.Context, v int) {
shouldBeEqualToV = v
})
ctx := context.Background()
p(ctx, 5)
assert.Equal(t, 5, shouldBeEqualToV)
p(ctx, 100)
assert.Equal(t, 100, shouldBeEqualToV)
p(ctx, -1234)
assert.Equal(t, -1234, shouldBeEqualToV)
}
// -------------------------------
func TestFilterProcessor(t *testing.T) {
var trueIfBiggerThan10 bool
p := newFilterSupplier(func(i int) bool {
return i > 10
}).Processor(func(ctx context.Context, v int) {
trueIfBiggerThan10 = true
})
ctx := context.Background()
p(ctx, 11)
assert.True(t, trueIfBiggerThan10)
trueIfBiggerThan10 = false
p(ctx, 10)
assert.False(t, trueIfBiggerThan10)
}
// -------------------------------
func TestMapProcessor(t *testing.T) {
var shouldBeItoa string
p := newMapSupplier(func(_ context.Context, d int) string {
return strconv.Itoa(d)
}).
Processor(func(ctx context.Context, v string) {
shouldBeItoa = v
})
ctx := context.Background()
p(ctx, 10)
assert.Equal(t, strconv.Itoa(10), shouldBeItoa)
p(ctx, 10000)
assert.Equal(t, strconv.Itoa(10000), shouldBeItoa)
p(ctx, -1234)
assert.Equal(t, strconv.Itoa(-1234), shouldBeItoa)
}
// -------------------------------
func TestFlatMapProcessor(t *testing.T) {
var shouldBeInc []int
p := newFlatMapSupplier(func(ctx context.Context, i int) []int {
res := make([]int, 0, i)
for n := 0; n < i; n++ {
res = append(res, n)
}
return res
}).Processor(func(ctx context.Context, v int) {
shouldBeInc = append(shouldBeInc, v)
})
ctx := context.Background()
p(ctx, 5)
assert.ElementsMatch(t, []int{0, 1, 2, 3, 4}, shouldBeInc)
}
// -------------------------------
type mockKvstore struct {
store map[int]int
}
var _ state.KeyValueStore[int, int] = &mockKvstore{}
func (kvs *mockKvstore) Get(key int) (int, error) {
v, ok := kvs.store[key]
if ok {
return v, nil
}
return 0, errors.New("mock error")
}
func (kvs *mockKvstore) Put(key int, value int) {
kvs.store[key] = value
}
func (*mockKvstore) Delete(key int) {
panic("unimplemented")
}
// -------------------------------
func TestStreamToTableProcessor(t *testing.T) {
var shouldBeEqual KeyValue[int, Change[int]]
mockKvs := &mockKvstore{map[int]int{}}
p := newStreamToTableSupplier[int, int](mockKvs).
Processor(func(ctx context.Context, kv KeyValue[int, Change[int]]) {
shouldBeEqual = kv
})
ctx := context.Background()
p(ctx, NewKeyValue(1, 1))
assert.Equal(t, 1, shouldBeEqual.Key)
assert.Equal(t, 1, shouldBeEqual.Value.NewValue)
p(ctx, NewKeyValue(1, 2))
assert.Equal(t, 1, shouldBeEqual.Key)
assert.Equal(t, 1, shouldBeEqual.Value.OldValue)
assert.Equal(t, 2, shouldBeEqual.Value.NewValue)
p(ctx, NewKeyValue(1, 3))
assert.Equal(t, 1, shouldBeEqual.Key)
assert.Equal(t, 2, shouldBeEqual.Value.OldValue)
assert.Equal(t, 3, shouldBeEqual.Value.NewValue)
}
// -------------------------------
func TestStreamTableJoinProcessor(t *testing.T) {
var shouldBeEqual KeyValue[int, int]
foundGetter := func(int) (int, error) { return 10, nil }
notFoundGetter := func(int) (int, error) { return 0, errors.New("mock error") }
joiner := func(k, v, vo int) int { return v + vo }
p := newStreamTableJoinSupplier(foundGetter, joiner).
Processor(func(ctx context.Context, kv KeyValue[int, int]) {
shouldBeEqual = kv
})
ctx := context.Background()
p(ctx, NewKeyValue(1, 1))
assert.Equal(t, 1, shouldBeEqual.Key)
assert.Equal(t, 11, shouldBeEqual.Value)
p(ctx, NewKeyValue(2, 22))
assert.Equal(t, 2, shouldBeEqual.Key)
assert.Equal(t, 32, shouldBeEqual.Value)
var trueIfProcessed bool
p = newStreamTableJoinSupplier(notFoundGetter, joiner).
Processor(func(ctx context.Context, kv KeyValue[int, int]) {
trueIfProcessed = true
})
p(ctx, NewKeyValue(1, 1))
assert.False(t, trueIfProcessed)
p(ctx, NewKeyValue(2, 1))
assert.False(t, trueIfProcessed)
}