-
Notifications
You must be signed in to change notification settings - Fork 2
/
deduplicate_test.go
221 lines (185 loc) · 5.62 KB
/
deduplicate_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
package simpleflow
import (
"fmt"
"github.com/stretchr/testify/suite"
"strconv"
"strings"
"testing"
)
type DeDuplicateSuite struct {
suite.Suite
}
func TestDeduplicate(t *testing.T) {
s := new(DeDuplicateSuite)
suite.Run(t, s)
}
func (s *DeDuplicateSuite) TestDeduplicateAdd() {
dd := NewDeduplicator[int]()
values := []int{1, 2, 3, 3, 4, 5, 6, 6, 6}
for ii, v := range values {
isDupe := ii == 3 || ii == 7 || ii == 8
seen := dd.Seen(v)
s.Equal(isDupe, seen, "%v (index %d) is expected to be seen", v, ii)
added := dd.Add(v)
s.Equal(isDupe, !added, "%v (index %d) is expected to be duplicate", v, ii)
}
}
func (s *DeDuplicateSuite) TestDeduplicateSlice() {
dd := NewDeduplicator[int]()
values := []int{1, 2, 3, 3, 4, 5, 6, 6, 6}
deduped := dd.Deduplicate(values)
s.ElementsMatch(deduped, []int{1, 2, 3, 4, 5, 6})
deduped = dd.Deduplicate(values)
s.ElementsMatch(deduped, []int{})
dd.Reset()
deduped = dd.Deduplicate(values)
s.ElementsMatch(deduped, []int{1, 2, 3, 4, 5, 6})
deduped = Deduplicate(values)
s.ElementsMatch(deduped, []int{1, 2, 3, 4, 5, 6})
dd.Reset()
dedupedIndices := dd.DeduplicateIndices(values)
s.ElementsMatch(dedupedIndices, []int{3, 7, 8})
}
func (s *DeDuplicateSuite) TestDeduplicateNilSlice() {
type Object struct {
slice []int
pointer *int
value string
}
s.Run("empty slice should return empty slice", func() {
dd := NewDeduplicator[int]()
deduped := dd.Deduplicate([]int{})
s.Equal(deduped, []int{})
})
s.Run("nil slice should return nil slice", func() {
dd := NewDeduplicator[int]()
deduped := dd.Deduplicate(nil)
s.Nil(deduped)
})
s.Run("empty object slice should return empty slice", func() {
dd := NewObjectDeduplicator[Object](func(v Object) string {
return v.value
})
deduped := dd.Deduplicate([]Object{})
s.Equal(deduped, []Object{})
})
s.Run("nil object slice should return nil slice", func() {
dd := NewObjectDeduplicator[Object](func(v Object) string {
return v.value
})
deduped := dd.Deduplicate(nil)
s.Nil(deduped)
})
}
func (s *DeDuplicateSuite) TestDeduplicateObject() {
// A few values that we can take pointers of
one := 1
one_2 := 1
two := 2
two_2 := 2
type Object struct {
slice []int
pointer *int
value string
}
a := Object{
slice: []int{1, 2, 3},
pointer: &one,
value: "one",
}
a2 := Object{
slice: []int{1, 2, 3},
pointer: &one_2,
value: "one",
}
b := Object{
slice: []int{2, 3, 4},
pointer: &two,
value: "two",
}
b2 := Object{
slice: []int{2, 3, 4},
pointer: &two,
value: "two",
}
c := Object{
slice: []int{2, 3, 4},
pointer: &two_2,
value: "three",
}
c2 := Object{
slice: []int{4, 5, 6},
pointer: &two_2,
value: "three",
}
values := []Object{a, a2, b, b2, c, c2}
validateSeen := func(dd *ObjectDeduplicator[Object], expectedSeen []Object) {
for _, expected := range expectedSeen {
s.True(dd.Seen(expected))
}
}
s.Run("dedupe by value", func() {
// Create an object deduplicator that looks for unique objects by their "value" field
dd := NewObjectDeduplicator[Object](func(v Object) string {
return v.value
})
expected := []Object{a, b, c}
expectedIdx := []int{1, 3, 5}
s.ElementsMatch(expected, dd.Deduplicate(values))
dd.Reset() // Need to reset in order to call dd again with the same values
s.ElementsMatch(expectedIdx, dd.DeduplicateIndices(values))
validateSeen(dd, expected)
})
s.Run("dedupe by pointer value", func() {
// Create an object deduplicator that looks for unique objects by the value pointed to by the "pointer" field
dd := NewObjectDeduplicator[Object](func(v Object) string {
return strconv.Itoa(*v.pointer)
})
expected := []Object{a, b}
expectedIdx := []int{1, 3, 4, 5}
s.ElementsMatch(expected, dd.Deduplicate(values))
dd.Reset() // Need to reset in order to call dd again with the same values
s.ElementsMatch(expectedIdx, dd.DeduplicateIndices(values))
validateSeen(dd, expected)
})
s.Run("dedupe by pointer", func() {
// Create an object deduplicator that looks for unique objects by the "pointer" field (pointer address)
dd := NewObjectDeduplicator[Object](func(v Object) string {
return fmt.Sprintf("%p", v.pointer)
})
expected := []Object{a, a2, b, c}
expectedIdx := []int{3, 5}
s.ElementsMatch(expected, dd.Deduplicate(values))
dd.Reset() // Need to reset in order to call dd again with the same values
s.ElementsMatch(expectedIdx, dd.DeduplicateIndices(values))
validateSeen(dd, expected)
})
s.Run("dedupe by slice value", func() {
// Create an object deduplicator that looks for unique objects by their "slice" field
dd := NewObjectDeduplicator[Object](func(v Object) string {
b := strings.Builder{}
for _, elem := range v.slice {
b.WriteString(strconv.Itoa(elem))
}
return b.String()
})
expected := []Object{a, b, c2}
expectedIdx := []int{1, 3, 4}
s.ElementsMatch(expected, dd.Deduplicate(values))
dd.Reset() // Need to reset in order to call dd again with the same values
s.ElementsMatch(expectedIdx, dd.DeduplicateIndices(values))
validateSeen(dd, expected)
})
s.Run("dedupe by value and pointer value", func() {
// Create an object deduplicator that looks for unique objects by multiple fields ("value" and "pointer"'s value)
dd := NewObjectDeduplicator[Object](func(v Object) string {
return fmt.Sprintf("%s_%d", v.value, *v.pointer)
})
expected := []Object{a, b, c}
expectedIdx := []int{1, 3, 5}
s.ElementsMatch(expected, dd.Deduplicate(values))
dd.Reset() // Need to reset in order to call dd again with the same values
s.ElementsMatch(expectedIdx, dd.DeduplicateIndices(values))
validateSeen(dd, expected)
})
}