Skip to content

Commit 2d738fe

Browse files
committed
chore: update test
1 parent fa2cfd7 commit 2d738fe

File tree

3 files changed

+85
-28
lines changed

3 files changed

+85
-28
lines changed

shared-lib/lib-ot/tests/node/operation_test.rs

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ fn operation_update_node_body_deserialize_test() {
7373
}
7474

7575
#[test]
76-
fn operation_insert_transform_test() {
76+
fn operation_insert_op_transform_test() {
7777
let node_1 = NodeDataBuilder::new("text_1").build();
7878
let node_2 = NodeDataBuilder::new("text_2").build();
7979
let op_1 = NodeOperation::Insert {
8080
path: Path(vec![0, 1]),
8181
nodes: vec![node_1],
8282
};
8383

84-
let mut insert_2 = NodeOperation::Insert {
84+
let insert_2 = NodeOperation::Insert {
8585
path: Path(vec![0, 1]),
8686
nodes: vec![node_2],
8787
};
@@ -95,14 +95,23 @@ fn operation_insert_transform_test() {
9595
}
9696

9797
#[test]
98-
fn operation_insert_transform_test2() {
98+
fn operation_insert_transform_test() {
9999
let mut test = NodeTest::new();
100100
let node_data_1 = NodeDataBuilder::new("text_1").build();
101101
let node_data_2 = NodeDataBuilder::new("text_2").build();
102-
let node_2: Node = node_data_2.clone().into();
103102
let node_data_3 = NodeDataBuilder::new("text_3").build();
104103
let node_3: Node = node_data_3.clone().into();
105-
104+
//
105+
// rev_id:1 0: text_1
106+
// rev_id:2 1: text_2
107+
//
108+
// Insert a new operation with rev_id 1.But the rev_id:1 is already exist, so
109+
// it needs to do the transform.
110+
//
111+
// --> 1:text_3
112+
// transform into:
113+
// --> 2:text_3
114+
//
106115
let scripts = vec![
107116
InsertNode {
108117
path: 0.into(),
@@ -119,14 +128,65 @@ fn operation_insert_transform_test2() {
119128
node_data: node_data_3.clone(),
120129
rev_id: 1,
121130
},
122-
// AssertNode {
123-
// path: 2.into(),
124-
// expected: node_2,
125-
// },
126131
AssertNode {
132+
path: 2.into(),
133+
expected: Some(node_3),
134+
},
135+
];
136+
test.run_scripts(scripts);
137+
}
138+
139+
#[test]
140+
fn operation_delete_transform_test() {
141+
let mut test = NodeTest::new();
142+
let node_data_1 = NodeDataBuilder::new("text_1").build();
143+
let node_data_2 = NodeDataBuilder::new("text_2").build();
144+
let node_data_3 = NodeDataBuilder::new("text_3").build();
145+
let node_3: Node = node_data_3.clone().into();
146+
147+
let scripts = vec![
148+
InsertNode {
149+
path: 0.into(),
150+
node_data: node_data_1.clone(),
151+
rev_id: 1,
152+
},
153+
InsertNode {
127154
path: 1.into(),
128-
expected: node_3,
155+
node_data: node_data_2.clone(),
156+
rev_id: 2,
157+
},
158+
// The node's in the tree will be:
159+
// 0: text_1
160+
// 2: text_2
161+
//
162+
// The insert action is happened concurrently with the delete action, because they
163+
// share the same rev_id. aka, 3. The delete action is want to delete the node at index 1,
164+
// but it was moved to index 2.
165+
InsertNode {
166+
path: 1.into(),
167+
node_data: node_data_3.clone(),
168+
rev_id: 3,
169+
},
170+
//
171+
// 0: text_1
172+
// 1: text_3
173+
// 2: text_2
174+
//
175+
// The path of the delete action will be transformed to a new path that point to the text_2.
176+
// 1 -> 2
177+
DeleteNode {
178+
path: 1.into(),
179+
rev_id: 3,
180+
},
181+
AssertNode {
182+
path: 1.into(),
183+
expected: Some(node_3),
184+
},
185+
AssertNode {
186+
path: 2.into(),
187+
expected: None,
129188
},
189+
AssertNumberOfNodesAtPath { path: None, len: 2 },
130190
];
131191
test.run_scripts(scripts);
132192
}

shared-lib/lib-ot/tests/node/script.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub enum NodeScript {
2222
},
2323
DeleteNode {
2424
path: Path,
25+
rev_id: usize,
2526
},
2627
AssertNumberOfNodesAtPath {
2728
path: Option<Path>,
@@ -33,16 +34,12 @@ pub enum NodeScript {
3334
},
3435
AssertNode {
3536
path: Path,
36-
expected: Node,
37+
expected: Option<Node>,
3738
},
3839
AssertNodeDelta {
3940
path: Path,
4041
expected: TextDelta,
4142
},
42-
ApplyTransaction {
43-
transaction: Transaction,
44-
rev_id: usize,
45-
},
4643
}
4744

4845
pub struct NodeTest {
@@ -92,15 +89,20 @@ impl NodeTest {
9289
.finalize();
9390
self.apply_transaction(transaction);
9491
}
95-
NodeScript::DeleteNode { path } => {
96-
let transaction = TransactionBuilder::new(&self.node_tree)
92+
NodeScript::DeleteNode { path, rev_id } => {
93+
let mut transaction = TransactionBuilder::new(&self.node_tree)
9794
.delete_node_at_path(&path)
9895
.finalize();
96+
self.transform_transaction_if_need(&mut transaction, rev_id);
9997
self.apply_transaction(transaction);
10098
}
10199
NodeScript::AssertNode { path, expected } => {
102-
let node_id = self.node_tree.node_id_at_path(path).unwrap();
103-
let node = self.node_tree.get_node(node_id).cloned().unwrap();
100+
let node_id = self.node_tree.node_id_at_path(path);
101+
if expected.is_none() && node_id.is_none() {
102+
return;
103+
}
104+
105+
let node = self.node_tree.get_node(node_id.unwrap()).cloned();
104106
assert_eq!(node, expected);
105107
}
106108
NodeScript::AssertNodeData { path, expected } => {
@@ -136,14 +138,6 @@ impl NodeTest {
136138
panic!("Node body type not match, expect Delta");
137139
}
138140
}
139-
140-
NodeScript::ApplyTransaction {
141-
mut transaction,
142-
rev_id,
143-
} => {
144-
self.transform_transaction_if_need(&mut transaction, rev_id);
145-
self.apply_transaction(transaction);
146-
}
147141
}
148142
}
149143

shared-lib/lib-ot/tests/node/tree_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ fn node_delete_test() {
185185
node_data: inserted_node,
186186
rev_id: 1,
187187
},
188-
DeleteNode { path: path.clone() },
188+
DeleteNode {
189+
path: path.clone(),
190+
rev_id: 2,
191+
},
189192
AssertNodeData { path, expected: None },
190193
];
191194
test.run_scripts(scripts);

0 commit comments

Comments
 (0)