database_editor.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. use crate::grid::mock_data::*;
  2. use bytes::Bytes;
  3. use flowy_database::entities::*;
  4. use flowy_database::services::cell::ToCellChangesetString;
  5. use flowy_database::services::field::SelectOptionPB;
  6. use flowy_database::services::field::*;
  7. use flowy_database::services::grid_editor::DatabaseRevisionEditor;
  8. use flowy_test::helper::ViewTest;
  9. use flowy_test::FlowySDKTest;
  10. use grid_model::*;
  11. use std::collections::HashMap;
  12. use std::sync::Arc;
  13. use strum::EnumCount;
  14. pub struct DatabaseEditorTest {
  15. pub sdk: FlowySDKTest,
  16. pub view_id: String,
  17. pub editor: Arc<DatabaseRevisionEditor>,
  18. pub field_revs: Vec<Arc<FieldRevision>>,
  19. pub block_meta_revs: Vec<Arc<GridBlockMetaRevision>>,
  20. pub row_revs: Vec<Arc<RowRevision>>,
  21. pub field_count: usize,
  22. pub row_by_row_id: HashMap<String, RowPB>,
  23. }
  24. impl DatabaseEditorTest {
  25. pub async fn new_table() -> Self {
  26. Self::new(LayoutTypePB::Grid).await
  27. }
  28. pub async fn new_board() -> Self {
  29. Self::new(LayoutTypePB::Board).await
  30. }
  31. pub async fn new(layout: LayoutTypePB) -> Self {
  32. let sdk = FlowySDKTest::default();
  33. let _ = sdk.init_user().await;
  34. let test = match layout {
  35. LayoutTypePB::Grid => {
  36. let build_context = make_test_grid();
  37. let view_data: Bytes = build_context.into();
  38. ViewTest::new_grid_view(&sdk, view_data.to_vec()).await
  39. },
  40. LayoutTypePB::Board => {
  41. let build_context = make_test_board();
  42. let view_data: Bytes = build_context.into();
  43. ViewTest::new_board_view(&sdk, view_data.to_vec()).await
  44. },
  45. LayoutTypePB::Calendar => {
  46. let build_context = make_test_calendar();
  47. let view_data: Bytes = build_context.into();
  48. ViewTest::new_calendar_view(&sdk, view_data.to_vec()).await
  49. },
  50. };
  51. let editor = sdk.grid_manager.open_database(&test.view.id).await.unwrap();
  52. let field_revs = editor.get_field_revs(None).await.unwrap();
  53. let block_meta_revs = editor.get_block_meta_revs().await.unwrap();
  54. let row_pbs = editor.get_all_row_revs(&test.view.id).await.unwrap();
  55. assert_eq!(block_meta_revs.len(), 1);
  56. // It seems like you should add the field in the make_test_grid() function.
  57. // Because we assert the initialize count of the fields is equal to FieldType::COUNT.
  58. assert_eq!(field_revs.len(), FieldType::COUNT);
  59. let grid_id = test.view.id;
  60. Self {
  61. sdk,
  62. view_id: grid_id,
  63. editor,
  64. field_revs,
  65. block_meta_revs,
  66. row_revs: row_pbs,
  67. field_count: FieldType::COUNT,
  68. row_by_row_id: HashMap::default(),
  69. }
  70. }
  71. pub async fn get_row_revs(&self) -> Vec<Arc<RowRevision>> {
  72. self.editor.get_all_row_revs(&self.view_id).await.unwrap()
  73. }
  74. pub async fn grid_filters(&self) -> Vec<FilterPB> {
  75. self.editor.get_all_filters().await.unwrap()
  76. }
  77. pub fn get_field_rev(&self, field_id: &str, field_type: FieldType) -> &Arc<FieldRevision> {
  78. self
  79. .field_revs
  80. .iter()
  81. .filter(|field_rev| {
  82. let t_field_type: FieldType = field_rev.ty.into();
  83. field_rev.id == field_id && t_field_type == field_type
  84. })
  85. .collect::<Vec<_>>()
  86. .pop()
  87. .unwrap()
  88. }
  89. /// returns the first `FieldRevision` in the build-in test grid.
  90. /// Not support duplicate `FieldType` in test grid yet.
  91. pub fn get_first_field_rev(&self, field_type: FieldType) -> &Arc<FieldRevision> {
  92. self
  93. .field_revs
  94. .iter()
  95. .filter(|field_rev| {
  96. let t_field_type: FieldType = field_rev.ty.into();
  97. t_field_type == field_type
  98. })
  99. .collect::<Vec<_>>()
  100. .pop()
  101. .unwrap()
  102. }
  103. pub fn get_multi_select_type_option(&self, field_id: &str) -> Vec<SelectOptionPB> {
  104. let field_type = FieldType::MultiSelect;
  105. let field_rev = self.get_field_rev(field_id, field_type.clone());
  106. let type_option = field_rev
  107. .get_type_option::<MultiSelectTypeOptionPB>(field_type.into())
  108. .unwrap();
  109. type_option.options
  110. }
  111. pub fn get_single_select_type_option(&self, field_id: &str) -> SingleSelectTypeOptionPB {
  112. let field_type = FieldType::SingleSelect;
  113. let field_rev = self.get_field_rev(field_id, field_type.clone());
  114. field_rev
  115. .get_type_option::<SingleSelectTypeOptionPB>(field_type.into())
  116. .unwrap()
  117. }
  118. #[allow(dead_code)]
  119. pub fn get_checklist_type_option(&self, field_id: &str) -> ChecklistTypeOptionPB {
  120. let field_type = FieldType::Checklist;
  121. let field_rev = self.get_field_rev(field_id, field_type.clone());
  122. field_rev
  123. .get_type_option::<ChecklistTypeOptionPB>(field_type.into())
  124. .unwrap()
  125. }
  126. #[allow(dead_code)]
  127. pub fn get_checkbox_type_option(&self, field_id: &str) -> CheckboxTypeOptionPB {
  128. let field_type = FieldType::Checkbox;
  129. let field_rev = self.get_field_rev(field_id, field_type.clone());
  130. field_rev
  131. .get_type_option::<CheckboxTypeOptionPB>(field_type.into())
  132. .unwrap()
  133. }
  134. pub fn block_id(&self) -> &str {
  135. &self.block_meta_revs.last().unwrap().block_id
  136. }
  137. pub async fn update_cell<T: ToCellChangesetString>(
  138. &mut self,
  139. field_id: &str,
  140. row_id: String,
  141. cell_changeset: T,
  142. ) {
  143. let field_rev = self
  144. .field_revs
  145. .iter()
  146. .find(|field_rev| field_rev.id == field_id)
  147. .unwrap();
  148. self
  149. .editor
  150. .update_cell_with_changeset(&row_id, &field_rev.id, cell_changeset)
  151. .await
  152. .unwrap();
  153. }
  154. pub(crate) async fn update_text_cell(&mut self, row_id: String, content: &str) {
  155. let field_rev = self
  156. .field_revs
  157. .iter()
  158. .find(|field_rev| {
  159. let field_type: FieldType = field_rev.ty.into();
  160. field_type == FieldType::RichText
  161. })
  162. .unwrap()
  163. .clone();
  164. self
  165. .update_cell(&field_rev.id, row_id, content.to_string())
  166. .await;
  167. }
  168. pub(crate) async fn update_single_select_cell(&mut self, row_id: String, option_id: &str) {
  169. let field_rev = self
  170. .field_revs
  171. .iter()
  172. .find(|field_rev| {
  173. let field_type: FieldType = field_rev.ty.into();
  174. field_type == FieldType::SingleSelect
  175. })
  176. .unwrap()
  177. .clone();
  178. let cell_changeset = SelectOptionCellChangeset::from_insert_option_id(option_id);
  179. self
  180. .update_cell(&field_rev.id, row_id, cell_changeset)
  181. .await;
  182. }
  183. }