-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathengine_test.go
More file actions
320 lines (272 loc) · 7.06 KB
/
engine_test.go
File metadata and controls
320 lines (272 loc) · 7.06 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package deep_test
import (
"fmt"
"strings"
"testing"
"github.com/brunoga/deep/v5"
"github.com/brunoga/deep/v5/crdt"
"github.com/brunoga/deep/v5/crdt/hlc"
"github.com/brunoga/deep/v5/internal/testmodels"
)
func TestCausality(t *testing.T) {
type Doc struct {
Title string
Score int
}
nodeA := crdt.NewCRDT(Doc{Title: "Original", Score: 0}, "node-a")
nodeB := crdt.NewCRDT(Doc{Title: "Original", Score: 0}, "node-b")
// Node A updates Title; node B updates Score concurrently.
deltaA := nodeA.Edit(func(d *Doc) { d.Title = "Updated" })
deltaB := nodeB.Edit(func(d *Doc) { d.Score = 42 })
// Both nodes apply both deltas — should converge.
nodeA.ApplyDelta(deltaB)
nodeB.ApplyDelta(deltaA)
vA, vB := nodeA.View(), nodeB.View()
if vA != vB {
t.Errorf("nodes did not converge: A=%+v B=%+v", vA, vB)
}
if vA.Title != "Updated" || vA.Score != 42 {
t.Errorf("wrong converged state: %+v", vA)
}
// Stale delta: applying an older edit after a newer one should be a no-op.
stale := nodeA.Edit(func(d *Doc) { d.Title = "Stale" })
_ = nodeA.Edit(func(d *Doc) { d.Title = "Definitive" })
nodeA.ApplyDelta(stale)
if nodeA.View().Title != "Definitive" {
t.Errorf("stale delta overwrote newer update")
}
}
func TestApplyOperation(t *testing.T) {
u := testmodels.User{
ID: 1,
Name: "Alice",
Bio: crdt.Text{{Value: "Hello"}},
}
p := deep.Patch[testmodels.User]{}
p.Operations = append(p.Operations, deep.Operation{
Kind: deep.OpReplace,
Path: "/full_name",
New: "Bob",
})
if err := deep.Apply(&u, p); err != nil {
t.Fatalf("Apply failed: %v", err)
}
if u.Name != "Bob" {
t.Errorf("expected Bob, got %s", u.Name)
}
}
func TestApplyError(t *testing.T) {
err1 := fmt.Errorf("error 1")
err2 := fmt.Errorf("error 2")
ae := &deep.ApplyError{Errors: []error{err1, err2}}
s := ae.Error()
if !strings.Contains(s, "2 errors during apply") {
t.Errorf("expected 2 errors message, got %s", s)
}
if !strings.Contains(s, "error 1") || !strings.Contains(s, "error 2") {
t.Errorf("missing individual errors in message: %s", s)
}
aeSingle := &deep.ApplyError{Errors: []error{err1}}
if aeSingle.Error() != "error 1" {
t.Errorf("expected error 1, got %s", aeSingle.Error())
}
}
func TestNilMapDiff(t *testing.T) {
type S struct {
M map[string]int
}
// nil source map → all keys should produce OpAdd, not OpReplace
a := S{M: nil}
b := S{M: map[string]int{"x": 1, "y": 2}}
p, err := deep.Diff(a, b)
if err != nil {
t.Fatalf("Diff failed: %v", err)
}
for _, op := range p.Operations {
if op.Kind != deep.OpReplace && op.Kind != deep.OpAdd {
continue
}
if op.Kind == deep.OpReplace {
t.Errorf("Diff with nil source map should emit OpAdd, got OpReplace at %s", op.Path)
}
}
}
func TestReflectionEngineAdvanced(t *testing.T) {
type Data struct {
A int
B int
}
d := &Data{A: 1, B: 2}
p := deep.Patch[Data]{}
p.Operations = []deep.Operation{
{Kind: deep.OpMove, Path: "/B", Old: "/A"},
{Kind: deep.OpCopy, Path: "/A", Old: "/B"},
{Kind: deep.OpRemove, Path: "/A"},
}
if err := deep.Apply(d, p); err != nil {
t.Errorf("Apply failed: %v", err)
}
}
func TestEngineFailures(t *testing.T) {
u := &testmodels.User{}
// Move from non-existent
p1 := deep.Patch[testmodels.User]{}
p1.Operations = []deep.Operation{{Kind: deep.OpMove, Path: "/id", Old: "/nonexistent"}}
deep.Apply(u, p1)
// Copy from non-existent
p2 := deep.Patch[testmodels.User]{}
p2.Operations = []deep.Operation{{Kind: deep.OpCopy, Path: "/id", Old: "/nonexistent"}}
deep.Apply(u, p2)
// Apply to nil
if err := deep.Apply((*testmodels.User)(nil), p1); err == nil {
t.Error("Apply to nil should fail")
}
}
func TestFinalPush(t *testing.T) {
// 1. All deep.OpKinds
for i := 0; i < 10; i++ {
_ = deep.OpKind(i).String()
}
// 2. Nested delegation failure (nil field)
type NestedNil struct {
User *testmodels.User
}
nn := &NestedNil{}
deep.Apply(nn, deep.Patch[NestedNil]{Operations: []deep.Operation{{Kind: deep.OpReplace, Path: "/User/id", New: 1}}})
}
func TestReflectionEqualCopy(t *testing.T) {
type Simple struct {
A int
}
s1 := Simple{A: 1}
s2 := Simple{A: 2}
if deep.Equal(s1, s2) {
t.Error("deep.Equal failed for different simple structs")
}
s3 := deep.Clone(s1)
if s3.A != 1 {
t.Error("deep.Clone failed for simple struct")
}
}
func TestTextAdvanced(t *testing.T) {
clock := hlc.NewClock("node-a")
t1 := clock.Now()
t2 := clock.Now()
// Complex ordering
text := crdt.Text{
{ID: t2, Value: "world", Prev: t1},
{ID: t1, Value: "hello "},
}
s := text.String()
if s != "hello world" {
t.Errorf("expected hello world, got %q", s)
}
text2 := crdt.Text{{Value: "old"}}
p2 := deep.Patch[crdt.Text]{Operations: []deep.Operation{
{Kind: deep.OpReplace, Path: "/", New: crdt.Text{{Value: "new"}}},
}}
text2.Patch(p2, nil)
}
func BenchmarkDiffGenerated(b *testing.B) {
u1 := testmodels.User{ID: 1, Name: "Alice", Roles: []string{"admin", "user"}}
u2 := testmodels.User{ID: 1, Name: "Bob", Roles: []string{"admin"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
deep.Diff(u1, u2)
}
}
func BenchmarkDiffReflection(b *testing.B) {
type SimpleData struct {
A int
B string
C []string
}
a := SimpleData{A: 1, B: "Alice", C: []string{"admin", "user"}}
c := SimpleData{A: 1, B: "Bob", C: []string{"admin"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
deep.Diff(a, c)
}
}
func BenchmarkEqualGenerated(b *testing.B) {
u1 := testmodels.User{ID: 1, Name: "Alice", Roles: []string{"admin", "user"}}
u2 := testmodels.User{ID: 1, Name: "Alice", Roles: []string{"admin", "user"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
deep.Equal(u1, u2)
}
}
func BenchmarkEqualReflection(b *testing.B) {
type SimpleData struct {
A int
B string
C []string
}
a := SimpleData{A: 1, B: "Alice", C: []string{"admin", "user"}}
c := SimpleData{A: 1, B: "Alice", C: []string{"admin", "user"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
deep.Equal(a, c)
}
}
func BenchmarkCopyGenerated(b *testing.B) {
u := testmodels.User{
ID: 1,
Name: "Alice",
Roles: []string{"admin", "user"},
Score: map[string]int{"chess": 1500, "go": 2000},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
deep.Clone(u)
}
}
func BenchmarkCopyReflection(b *testing.B) {
type SimpleData struct {
A int
B string
C []string
D map[string]int
}
a := SimpleData{
A: 1,
B: "Alice",
C: []string{"admin", "user"},
D: map[string]int{"chess": 1500, "go": 2000},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
deep.Clone(a)
}
}
func BenchmarkApplyGenerated(b *testing.B) {
u1 := testmodels.User{ID: 1, Name: "Alice"}
u2 := testmodels.User{ID: 1, Name: "Bob"}
p, err := deep.Diff(u1, u2)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
u3 := u1
deep.Apply(&u3, p)
}
}
func BenchmarkApplyReflection(b *testing.B) {
// SimpleData has no generated code, forcing the reflection engine path.
type SimpleData struct {
A int
B string
}
a := SimpleData{A: 1, B: "hello"}
c := SimpleData{A: 2, B: "world"}
p, err := deep.Diff(a, c)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
d := a
deep.Apply(&d, p)
}
}