-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_namespace_test.go
More file actions
110 lines (101 loc) · 3.07 KB
/
Copy pathdocument_namespace_test.go
File metadata and controls
110 lines (101 loc) · 3.07 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
package crdt
import (
"context"
"encoding/json"
"errors"
"testing"
)
func TestDocumentNamespaceRejectsMisroutedState(t *testing.T) {
t.Parallel()
left := NewDocument("document-a", "actor")
right := NewDocument("document-b", "actor")
mustApply(t, left, InsertFeature{
FeatureID: "left",
Geometry: json.RawMessage(`{"type":"Point","coordinates":[1,1]}`),
})
mustApply(t, right, InsertFeature{
FeatureID: "right",
Geometry: json.RawMessage(`{"type":"Point","coordinates":[2,2]}`),
})
if _, err := right.Merge(left); !errors.Is(err, ErrDocumentMismatch) {
t.Fatalf("document merge error = %v", err)
}
if _, err := right.MergeDelta(left.DeltaSince(nil)); !errors.Is(err, ErrDocumentMismatch) {
t.Fatalf("delta merge error = %v", err)
}
if _, err := right.MergeOps("document-a", left.Ops()); !errors.Is(err, ErrDocumentMismatch) {
t.Fatalf("operation merge error = %v", err)
}
if _, ok := right.Feature("left"); ok {
t.Fatal("misrouted feature entered destination document")
}
}
func TestEmptyDocumentIDsCreateDistinctNamespaces(t *testing.T) {
t.Parallel()
left := NewDocument("", "left")
right := NewDocument("", "right")
if left.DocumentID() == "" || right.DocumentID() == "" {
t.Fatal("empty constructor input did not create a document namespace")
}
if left.DocumentID() == right.DocumentID() {
t.Fatal("fresh documents unexpectedly share a namespace")
}
if _, err := left.Merge(right); !errors.Is(err, ErrDocumentMismatch) {
t.Fatalf("fresh document merge error = %v", err)
}
}
func TestMemStoreMultiplexesDocumentNamespaces(t *testing.T) {
t.Parallel()
ctx := context.Background()
store := NewMemStore()
opA := DocumentOp{
Type: OpInsertFeature, SiteID: "actor", Seq: 1, Timestamp: 1,
FeatureID: "a",
}
opB := opA
opB.FeatureID = "b"
if err := store.Append(ctx, "document-a", []DocumentOp{opA}); err != nil {
t.Fatal(err)
}
if err := store.Append(ctx, "document-b", []DocumentOp{opB}); err != nil {
t.Fatalf("same operation identity in another document collided: %v", err)
}
gotA, err := store.Since(ctx, "document-a", nil)
if err != nil {
t.Fatal(err)
}
gotB, err := store.Since(ctx, "document-b", nil)
if err != nil {
t.Fatal(err)
}
if len(gotA) != 1 || gotA[0].FeatureID != "a" || len(gotB) != 1 || gotB[0].FeatureID != "b" {
t.Fatalf("document operation keys crossed: A=%+v B=%+v", gotA, gotB)
}
docA := NewDocument("document-a", "a")
docB := NewDocument("document-b", "b")
snapshotA, err := docA.Snapshot("checkpoint")
if err != nil {
t.Fatal(err)
}
snapshotB, err := docB.Snapshot("checkpoint")
if err != nil {
t.Fatal(err)
}
if err := store.Save(ctx, snapshotA); err != nil {
t.Fatal(err)
}
if err := store.Save(ctx, snapshotB); err != nil {
t.Fatal(err)
}
loadedA, err := store.Load(ctx, "document-a", "checkpoint")
if err != nil {
t.Fatal(err)
}
loadedB, err := store.Load(ctx, "document-b", "checkpoint")
if err != nil {
t.Fatal(err)
}
if loadedA.DocumentID != "document-a" || loadedB.DocumentID != "document-b" {
t.Fatalf("snapshot keys crossed: A=%q B=%q", loadedA.DocumentID, loadedB.DocumentID)
}
}