script.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. use crate::grid::grid_editor::GridEditorTest;
  2. use flowy_grid::entities::{CreateRowParams, FieldType, GridLayout, GroupPB, 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. }
  35. pub struct GridGroupTest {
  36. inner: GridEditorTest,
  37. }
  38. impl GridGroupTest {
  39. pub async fn new() -> Self {
  40. let editor_test = GridEditorTest::new_board().await;
  41. Self { inner: editor_test }
  42. }
  43. pub async fn run_scripts(&mut self, scripts: Vec<GroupScript>) {
  44. for script in scripts {
  45. self.run_script(script).await;
  46. }
  47. }
  48. pub async fn run_script(&mut self, script: GroupScript) {
  49. match script {
  50. GroupScript::AssertGroup { group_index, row_count } => {
  51. assert_eq!(row_count, self.group_at_index(group_index).await.rows.len());
  52. }
  53. GroupScript::AssertGroupCount(count) => {
  54. let groups = self.editor.load_groups().await.unwrap();
  55. assert_eq!(count, groups.len());
  56. }
  57. GroupScript::MoveRow {
  58. from_group_index,
  59. from_row_index,
  60. to_group_index,
  61. to_row_index,
  62. } => {
  63. let groups: Vec<GroupPB> = self.editor.load_groups().await.unwrap().items;
  64. let from_row = groups.get(from_group_index).unwrap().rows.get(from_row_index).unwrap();
  65. let to_row = groups.get(to_group_index).unwrap().rows.get(to_row_index).unwrap();
  66. let params = MoveRowParams {
  67. view_id: self.inner.grid_id.clone(),
  68. from_row_id: from_row.id.clone(),
  69. to_row_id: to_row.id.clone(),
  70. };
  71. self.editor.move_row(params).await.unwrap();
  72. }
  73. GroupScript::AssertRow {
  74. group_index,
  75. row_index,
  76. row,
  77. } => {
  78. //
  79. let group = self.group_at_index(group_index).await;
  80. let compare_row = group.rows.get(row_index).unwrap().clone();
  81. assert_eq!(row.id, compare_row.id);
  82. }
  83. GroupScript::CreateRow { group_index } => {
  84. //
  85. let group = self.group_at_index(group_index).await;
  86. let params = CreateRowParams {
  87. grid_id: self.editor.grid_id.clone(),
  88. start_row_id: None,
  89. group_id: Some(group.group_id.clone()),
  90. layout: GridLayout::Board,
  91. };
  92. let _ = self.editor.create_row(params).await.unwrap();
  93. }
  94. GroupScript::DeleteRow { group_index, row_index } => {
  95. let row = self.row_at_index(group_index, row_index).await;
  96. self.editor.delete_row(&row.id).await.unwrap();
  97. }
  98. GroupScript::UpdateRow {
  99. from_group_index,
  100. row_index,
  101. to_group_index,
  102. } => {
  103. let from_group = self.group_at_index(from_group_index).await;
  104. let to_group = self.group_at_index(to_group_index).await;
  105. let field_id = from_group.field_id;
  106. let field_rev = self.editor.get_field_rev(&field_id).await.unwrap();
  107. let field_type: FieldType = field_rev.ty.into();
  108. let cell_rev = match field_type {
  109. FieldType::SingleSelect => insert_select_option_cell(to_group.group_id.clone(), &field_rev),
  110. FieldType::MultiSelect => insert_select_option_cell(to_group.group_id.clone(), &field_rev),
  111. _ => {
  112. panic!("Unsupported group field type");
  113. }
  114. };
  115. let row_id = self.row_at_index(from_group_index, row_index).await.id;
  116. let mut row_changeset = RowChangeset::new(row_id);
  117. row_changeset.cell_by_field_id.insert(field_id, cell_rev);
  118. self.editor.update_row(row_changeset).await.unwrap();
  119. }
  120. }
  121. }
  122. pub async fn group_at_index(&self, index: usize) -> GroupPB {
  123. let groups = self.editor.load_groups().await.unwrap().items;
  124. groups.get(index).unwrap().clone()
  125. }
  126. pub async fn row_at_index(&self, group_index: usize, row_index: usize) -> RowPB {
  127. let groups = self.group_at_index(group_index).await;
  128. groups.rows.get(row_index).unwrap().clone()
  129. }
  130. }
  131. impl std::ops::Deref for GridGroupTest {
  132. type Target = GridEditorTest;
  133. fn deref(&self) -> &Self::Target {
  134. &self.inner
  135. }
  136. }
  137. impl std::ops::DerefMut for GridGroupTest {
  138. fn deref_mut(&mut self) -> &mut Self::Target {
  139. &mut self.inner
  140. }
  141. }