database_editor.rs 6.1 KB

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