script.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. use crate::grid::grid_editor::GridEditorTest;
  2. use flowy_grid::entities::{CreateRowParams, FieldType, GridLayout, GroupPB, MoveGroupParams, MoveRowParams, RowPB};
  3. use flowy_grid::services::cell::insert_select_option_cell;
  4. use flowy_grid_data_model::revision::RowChangeset;
  5. pub enum GroupScript {
  6. AssertGroup {
  7. group_index: usize,
  8. row_count: usize,
  9. },
  10. AssertGroupCount(usize),
  11. AssertRow {
  12. group_index: usize,
  13. row_index: usize,
  14. row: RowPB,
  15. },
  16. MoveRow {
  17. from_group_index: usize,
  18. from_row_index: usize,
  19. to_group_index: usize,
  20. to_row_index: usize,
  21. },
  22. CreateRow {
  23. group_index: usize,
  24. },
  25. DeleteRow {
  26. group_index: usize,
  27. row_index: usize,
  28. },
  29. UpdateRow {
  30. from_group_index: usize,
  31. row_index: usize,
  32. to_group_index: usize,
  33. },
  34. MoveGroup {
  35. from_group_index: usize,
  36. to_group_index: usize,
  37. },
  38. }
  39. pub struct GridGroupTest {
  40. inner: GridEditorTest,
  41. }
  42. impl GridGroupTest {
  43. pub async fn new() -> Self {
  44. let editor_test = GridEditorTest::new_board().await;
  45. Self { inner: editor_test }
  46. }
  47. pub async fn run_scripts(&mut self, scripts: Vec<GroupScript>) {
  48. for script in scripts {
  49. self.run_script(script).await;
  50. }
  51. }
  52. pub async fn run_script(&mut self, script: GroupScript) {
  53. match script {
  54. GroupScript::AssertGroup { group_index, row_count } => {
  55. assert_eq!(row_count, self.group_at_index(group_index).await.rows.len());
  56. }
  57. GroupScript::AssertGroupCount(count) => {
  58. let groups = self.editor.load_groups().await.unwrap();
  59. assert_eq!(count, groups.len());
  60. }
  61. GroupScript::MoveRow {
  62. from_group_index,
  63. from_row_index,
  64. to_group_index,
  65. to_row_index,
  66. } => {
  67. let groups: Vec<GroupPB> = self.editor.load_groups().await.unwrap().items;
  68. let from_row = groups.get(from_group_index).unwrap().rows.get(from_row_index).unwrap();
  69. let to_row = groups.get(to_group_index).unwrap().rows.get(to_row_index).unwrap();
  70. let params = MoveRowParams {
  71. view_id: self.inner.grid_id.clone(),
  72. from_row_id: from_row.id.clone(),
  73. to_row_id: to_row.id.clone(),
  74. };
  75. self.editor.move_row(params).await.unwrap();
  76. }
  77. GroupScript::AssertRow {
  78. group_index,
  79. row_index,
  80. row,
  81. } => {
  82. //
  83. let group = self.group_at_index(group_index).await;
  84. let compare_row = group.rows.get(row_index).unwrap().clone();
  85. assert_eq!(row.id, compare_row.id);
  86. }
  87. GroupScript::CreateRow { group_index } => {
  88. //
  89. let group = self.group_at_index(group_index).await;
  90. let params = CreateRowParams {
  91. grid_id: self.editor.grid_id.clone(),
  92. start_row_id: None,
  93. group_id: Some(group.group_id.clone()),
  94. layout: GridLayout::Board,
  95. };
  96. let _ = self.editor.create_row(params).await.unwrap();
  97. }
  98. GroupScript::DeleteRow { group_index, row_index } => {
  99. let row = self.row_at_index(group_index, row_index).await;
  100. self.editor.delete_row(&row.id).await.unwrap();
  101. }
  102. GroupScript::UpdateRow {
  103. from_group_index,
  104. row_index,
  105. to_group_index,
  106. } => {
  107. let from_group = self.group_at_index(from_group_index).await;
  108. let to_group = self.group_at_index(to_group_index).await;
  109. let field_id = from_group.field_id;
  110. let field_rev = self.editor.get_field_rev(&field_id).await.unwrap();
  111. let field_type: FieldType = field_rev.ty.into();
  112. let cell_rev = match field_type {
  113. FieldType::SingleSelect => insert_select_option_cell(to_group.group_id.clone(), &field_rev),
  114. FieldType::MultiSelect => insert_select_option_cell(to_group.group_id.clone(), &field_rev),
  115. _ => {
  116. panic!("Unsupported group field type");
  117. }
  118. };
  119. let row_id = self.row_at_index(from_group_index, row_index).await.id;
  120. let mut row_changeset = RowChangeset::new(row_id);
  121. row_changeset.cell_by_field_id.insert(field_id, cell_rev);
  122. self.editor.update_row(row_changeset).await.unwrap();
  123. }
  124. GroupScript::MoveGroup {
  125. from_group_index,
  126. to_group_index,
  127. } => {
  128. let from_group = self.group_at_index(from_group_index).await;
  129. let to_group = self.group_at_index(to_group_index).await;
  130. let params = MoveGroupParams {
  131. view_id: self.editor.grid_id.clone(),
  132. from_group_id: from_group.group_id,
  133. to_group_id: to_group.group_id,
  134. };
  135. self.editor.move_group(params).await.unwrap();
  136. //
  137. }
  138. }
  139. }
  140. pub async fn group_at_index(&self, index: usize) -> GroupPB {
  141. let groups = self.editor.load_groups().await.unwrap().items;
  142. groups.get(index).unwrap().clone()
  143. }
  144. pub async fn row_at_index(&self, group_index: usize, row_index: usize) -> RowPB {
  145. let groups = self.group_at_index(group_index).await;
  146. groups.rows.get(row_index).unwrap().clone()
  147. }
  148. }
  149. impl std::ops::Deref for GridGroupTest {
  150. type Target = GridEditorTest;
  151. fn deref(&self) -> &Self::Target {
  152. &self.inner
  153. }
  154. }
  155. impl std::ops::DerefMut for GridGroupTest {
  156. fn deref_mut(&mut self) -> &mut Self::Target {
  157. &mut self.inner
  158. }
  159. }