Skip to content

Commit

Permalink
Add integration tree test for sync with js-sdk (yorkie-team#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonGyu1 authored and Wu22e committed Sep 3, 2023
1 parent 4da415b commit cff209e
Showing 1 changed file with 111 additions and 4 deletions.
115 changes: 111 additions & 4 deletions test/integration/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/yorkie-team/yorkie/pkg/document"
"github.com/yorkie-team/yorkie/pkg/document/json"
"github.com/yorkie-team/yorkie/pkg/document/presence"
"github.com/yorkie-team/yorkie/pkg/index"
"github.com/yorkie-team/yorkie/test/helper"
)

Expand Down Expand Up @@ -224,7 +225,7 @@ func TestTree(t *testing.T) {
assert.NoError(t, err)
})

t.Run("edit its content with path", func(t *testing.T) {
t.Run("edit content with path test", func(t *testing.T) {
doc := document.New(helper.TestDocKey(t))
err := doc.Update(func(root *json.Object, p *presence.Presence) error {
root.SetNewTree("t", &json.TreeNode{
Expand Down Expand Up @@ -255,10 +256,112 @@ func TestTree(t *testing.T) {
assert.Equal(t, "<doc><tc><p><tn>aXb!</tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 0, 1}, []int{0, 0, 1}, &json.TreeNode{
Type: "tn",
Children: []json.TreeNode{},
Type: "tn",
Children: []json.TreeNode{{
Type: "text", Value: "cd",
}},
})
assert.Equal(t, "<doc><tc><p><tn>aXb!</tn><tn>cd</tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 1}, []int{0, 1}, &json.TreeNode{
Type: "p",
Children: []json.TreeNode{{
Type: "tn", Children: []json.TreeNode{{
Type: "text", Value: "q",
}},
}},
})
assert.Equal(t, "<doc><tc><p><tn>aXb!</tn><tn>cd</tn></p><p><tn>q</tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 1, 0, 0}, []int{0, 1, 0, 0}, &json.TreeNode{
Type: "text",
Value: "a",
})
assert.Equal(t, "<doc><tc><p><tn>aXb!</tn><tn>cd</tn></p><p><tn>aq</tn></p></tc></doc>", root.GetTree("t").ToXML())

assert.Panics(t, func() {doc.Update(func(root *json.Object, p *presence.Presence) error {
root.GetTree("t").EditByPath([]int{0, 0, 4}, []int{0, 0, 4}, &json.TreeNode{
Type: "tn",
Children: []json.TreeNode{},
})
return nil
})}, index.ErrUnreachablePath)
return nil
})
assert.NoError(t, err)
})

t.Run("edit content with path test 2", func(t *testing.T) {
doc := document.New(helper.TestDocKey(t))
err := doc.Update(func(root *json.Object, p *presence.Presence) error {
root.SetNewTree("t", &json.TreeNode{
Type: "doc",
Children: []json.TreeNode{{
Type: "tc",
Children: []json.TreeNode{{
Type: "p", Children: []json.TreeNode{{
Type: "tn", Children: []json.TreeNode{},
}},
}},
}},
})
assert.Equal(t, "<doc><tc><p><tn></tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 0, 0, 0}, []int{0, 0, 0, 0}, &json.TreeNode{
Type: "text",
Value: "a",
})
assert.Equal(t, "<doc><tc><p><tn>a</tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 1}, []int{0, 1}, &json.TreeNode{
Type: "p",
Children: []json.TreeNode{{
Type: "tn", Children: []json.TreeNode{},
}},
})
assert.Equal(t, "<doc><tc><p><tn>a</tn></p><p><tn></tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 1, 0, 0}, []int{0, 1, 0, 0}, &json.TreeNode{
Type: "text",
Value: "b",
})
assert.Equal(t, "<doc><tc><p><tn>a</tn></p><p><tn>b</tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 2}, []int{0, 2}, &json.TreeNode{
Type: "p",
Children: []json.TreeNode{{
Type: "tn", Children: []json.TreeNode{},
}},
})
assert.Equal(t, "<doc><tc><p><tn>a</tn></p><p><tn>b</tn></p><p><tn></tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 2, 0, 0}, []int{0, 2, 0, 0}, &json.TreeNode{
Type: "text",
Value: "c",
})
assert.Equal(t, "<doc><tc><p><tn>a</tn></p><p><tn>b</tn></p><p><tn>c</tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 3}, []int{0, 3}, &json.TreeNode{
Type: "p",
Children: []json.TreeNode{{
Type: "tn", Children: []json.TreeNode{},
}},
})
assert.Equal(t, "<doc><tc><p><tn>a</tn></p><p><tn>b</tn></p><p><tn>c</tn></p><p><tn></tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 3, 0, 0}, []int{0, 3, 0, 0}, &json.TreeNode{
Type: "text",
Value: "d",
})
assert.Equal(t, "<doc><tc><p><tn>a</tn></p><p><tn>b</tn></p><p><tn>c</tn></p><p><tn>d</tn></p></tc></doc>", root.GetTree("t").ToXML())

root.GetTree("t").EditByPath([]int{0, 3}, []int{0, 3}, &json.TreeNode{
Type: "p",
Children: []json.TreeNode{{
Type: "tn", Children: []json.TreeNode{},
}},
})
assert.Equal(t, "<doc><tc><p><tn>aXb!</tn><tn></tn></p></tc></doc>", root.GetTree("t").ToXML())
assert.Equal(t, "<doc><tc><p><tn>a</tn></p><p><tn>b</tn></p><p><tn>c</tn></p><p><tn></tn></p><p><tn>d</tn></p></tc></doc>", root.GetTree("t").ToXML())
return nil
})
assert.NoError(t, err)
Expand Down Expand Up @@ -401,6 +504,7 @@ func TestTree(t *testing.T) {
assert.Equal(t, `{"type":"root","children":[{"type":"p","children":[{"type":"text","value":"ab"}],"attributes":{"bold":"true"}},{"type":"p","children":[{"type":"text","value":"cd"}],"attributes":{"italic":"true"}}]}`, d2.Root().GetTree("t").Marshal())
})

// Concurrent editing, overlapping range test
t.Run("concurrently delete overlapping elements test", func(t *testing.T) {
ctx := context.Background()
d1 := document.New(helper.TestDocKey(t))
Expand Down Expand Up @@ -479,6 +583,7 @@ func TestTree(t *testing.T) {
assert.Equal(t, "<root><p></p></root>", d1.Root().GetTree("t").ToXML())
})

// Concurrent editing, contained range test
t.Run("concurrently insert and delete contained elements of the same depth test", func(t *testing.T) {
ctx := context.Background()
d1 := document.New(helper.TestDocKey(t))
Expand Down Expand Up @@ -788,6 +893,7 @@ func TestTree(t *testing.T) {
assert.Equal(t, "<root></root>", d1.Root().GetTree("t").ToXML())
})

// Concurrent editing, side by side range test
t.Run("concurrently insert side by side elements (left) test", func(t *testing.T) {
ctx := context.Background()
d1 := document.New(helper.TestDocKey(t))
Expand Down Expand Up @@ -1340,6 +1446,7 @@ func TestTree(t *testing.T) {
assert.Equal(t, "<root><p>12</p></root>", d1.Root().GetTree("t").ToXML())
})

// Concurrent editing, complex cases test
t.Run("delete text content anchored to another concurrently test", func(t *testing.T) {
ctx := context.Background()
d1 := document.New(helper.TestDocKey(t))
Expand Down

0 comments on commit cff209e

Please sign in to comment.