operation_delete_test.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. use crate::node::script::NodeScript::*;
  2. use crate::node::script::NodeTest;
  3. use lib_ot::core::{Changeset, NodeData, NodeDataBuilder};
  4. #[test]
  5. fn operation_delete_nested_node_test() {
  6. let mut test = NodeTest::new();
  7. let image_a = NodeData::new("image_a");
  8. let image_b = NodeData::new("image_b");
  9. let video_a = NodeData::new("video_a");
  10. let video_b = NodeData::new("video_b");
  11. let image_1 = NodeDataBuilder::new("image_1")
  12. .add_node_data(image_a.clone())
  13. .add_node_data(image_b.clone())
  14. .build();
  15. let video_1 = NodeDataBuilder::new("video_1")
  16. .add_node_data(video_a.clone())
  17. .add_node_data(video_b)
  18. .build();
  19. let text_node_1 = NodeDataBuilder::new("text_1")
  20. .add_node_data(image_1)
  21. .add_node_data(video_1.clone())
  22. .build();
  23. let image_2 = NodeDataBuilder::new("image_2")
  24. .add_node_data(image_a)
  25. .add_node_data(image_b.clone())
  26. .build();
  27. let text_node_2 = NodeDataBuilder::new("text_2").add_node_data(image_2).build();
  28. let scripts = vec![
  29. InsertNode {
  30. path: 0.into(),
  31. node_data: text_node_1,
  32. rev_id: 1,
  33. },
  34. InsertNode {
  35. path: 1.into(),
  36. node_data: text_node_2,
  37. rev_id: 2,
  38. },
  39. // 0:text_1
  40. // 0:image_1
  41. // 0:image_a
  42. // 1:image_b
  43. // 1:video_1
  44. // 0:video_a
  45. // 1:video_b
  46. // 1:text_2
  47. // 0:image_2
  48. // 0:image_a
  49. // 1:image_b
  50. DeleteNode {
  51. path: vec![0, 0, 0].into(),
  52. rev_id: 3,
  53. },
  54. AssertNode {
  55. path: vec![0, 0, 0].into(),
  56. expected: Some(image_b),
  57. },
  58. AssertNode {
  59. path: vec![0, 1].into(),
  60. expected: Some(video_1),
  61. },
  62. DeleteNode {
  63. path: vec![0, 1, 1].into(),
  64. rev_id: 4,
  65. },
  66. AssertNode {
  67. path: vec![0, 1, 0].into(),
  68. expected: Some(video_a),
  69. },
  70. ];
  71. test.run_scripts(scripts);
  72. }
  73. #[test]
  74. fn operation_delete_node_with_revision_conflict_test() {
  75. let mut test = NodeTest::new();
  76. let text_1 = NodeDataBuilder::new("text_1").build();
  77. let text_2 = NodeDataBuilder::new("text_2").build();
  78. let text_3 = NodeDataBuilder::new("text_3").build();
  79. let scripts = vec![
  80. InsertNode {
  81. path: 0.into(),
  82. node_data: text_1.clone(),
  83. rev_id: 1,
  84. },
  85. InsertNode {
  86. path: 1.into(),
  87. node_data: text_2,
  88. rev_id: 2,
  89. },
  90. // The node's in the tree will be:
  91. // 0: text_1
  92. // 2: text_2
  93. //
  94. // The insert action is happened concurrently with the delete action, because they
  95. // share the same rev_id. aka, 3. The delete action is want to delete the node at index 1,
  96. // but it was moved to index 2.
  97. InsertNode {
  98. path: 1.into(),
  99. node_data: text_3.clone(),
  100. rev_id: 3,
  101. },
  102. // 0: text_1
  103. // 1: text_3
  104. // 2: text_2
  105. //
  106. // The path of the delete action will be transformed to a new path that point to the text_2.
  107. // 1 -> 2
  108. DeleteNode {
  109. path: 1.into(),
  110. rev_id: 3,
  111. },
  112. // After perform the delete action, the tree will be:
  113. // 0: text_1
  114. // 1: text_3
  115. AssertNumberOfChildrenAtPath {
  116. path: None,
  117. expected: 2,
  118. },
  119. AssertNode {
  120. path: 0.into(),
  121. expected: Some(text_1),
  122. },
  123. AssertNode {
  124. path: 1.into(),
  125. expected: Some(text_3),
  126. },
  127. AssertNode {
  128. path: 2.into(),
  129. expected: None,
  130. },
  131. ];
  132. test.run_scripts(scripts);
  133. }
  134. #[test]
  135. fn operation_update_node_after_delete_test() {
  136. let mut test = NodeTest::new();
  137. let text_1 = NodeDataBuilder::new("text_1").build();
  138. let text_2 = NodeDataBuilder::new("text_2").build();
  139. let scripts = vec![
  140. InsertNode {
  141. path: 0.into(),
  142. node_data: text_1,
  143. rev_id: 1,
  144. },
  145. InsertNode {
  146. path: 1.into(),
  147. node_data: text_2,
  148. rev_id: 2,
  149. },
  150. DeleteNode {
  151. path: 0.into(),
  152. rev_id: 3,
  153. },
  154. // The node at path 1 is not exist. The following UpdateBody script will do nothing
  155. AssertNode {
  156. path: 1.into(),
  157. expected: None,
  158. },
  159. UpdateBody {
  160. path: 1.into(),
  161. changeset: Changeset::Delta {
  162. delta: Default::default(),
  163. inverted: Default::default(),
  164. },
  165. },
  166. ];
  167. test.run_scripts(scripts);
  168. }