database_editor.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. use std::collections::HashMap;
  2. use std::sync::Arc;
  3. use collab_database::fields::Field;
  4. use collab_database::rows::{CreateRowParams, Row, RowId};
  5. use strum::EnumCount;
  6. use flowy_database2::entities::{DatabaseLayoutPB, FieldType, FilterPB, RowPB};
  7. use flowy_database2::services::cell::{CellBuilder, ToCellChangeset};
  8. use flowy_database2::services::database::DatabaseEditor;
  9. use flowy_database2::services::field::{
  10. CheckboxTypeOption, ChecklistTypeOption, DateCellChangeset, MultiSelectTypeOption, SelectOption,
  11. SelectOptionCellChangeset, SingleSelectTypeOption,
  12. };
  13. use flowy_error::FlowyResult;
  14. use flowy_test::helper::ViewTest;
  15. use flowy_test::FlowySDKTest;
  16. use crate::database::mock_data::{make_test_board, make_test_calendar, make_test_grid};
  17. pub struct DatabaseEditorTest {
  18. pub sdk: FlowySDKTest,
  19. pub app_id: String,
  20. pub view_id: String,
  21. pub editor: Arc<DatabaseEditor>,
  22. pub fields: Vec<Arc<Field>>,
  23. pub rows: Vec<Arc<Row>>,
  24. pub field_count: usize,
  25. pub row_by_row_id: HashMap<String, RowPB>,
  26. }
  27. impl DatabaseEditorTest {
  28. pub async fn new_grid() -> Self {
  29. Self::new(DatabaseLayoutPB::Grid).await
  30. }
  31. pub async fn new_board() -> Self {
  32. Self::new(DatabaseLayoutPB::Board).await
  33. }
  34. pub async fn new_calendar() -> Self {
  35. Self::new(DatabaseLayoutPB::Calendar).await
  36. }
  37. pub async fn new(layout: DatabaseLayoutPB) -> Self {
  38. let sdk = FlowySDKTest::default();
  39. let _ = sdk.init_user().await;
  40. let test = match layout {
  41. DatabaseLayoutPB::Grid => {
  42. let params = make_test_grid();
  43. ViewTest::new_grid_view(&sdk, params.to_json_bytes().unwrap()).await
  44. },
  45. DatabaseLayoutPB::Board => {
  46. let data = make_test_board();
  47. ViewTest::new_board_view(&sdk, data.to_json_bytes().unwrap()).await
  48. },
  49. DatabaseLayoutPB::Calendar => {
  50. let data = make_test_calendar();
  51. ViewTest::new_calendar_view(&sdk, data.to_json_bytes().unwrap()).await
  52. },
  53. };
  54. let editor = sdk
  55. .database_manager
  56. .get_database(&test.child_view.id)
  57. .await
  58. .unwrap();
  59. let fields = editor
  60. .get_fields(&test.child_view.id, None)
  61. .into_iter()
  62. .map(Arc::new)
  63. .collect();
  64. let rows = editor
  65. .get_rows(&test.child_view.id)
  66. .await
  67. .unwrap()
  68. .into_iter()
  69. .collect();
  70. let view_id = test.child_view.id;
  71. let app_id = test.parent_view.id;
  72. Self {
  73. sdk,
  74. app_id,
  75. view_id,
  76. editor,
  77. fields,
  78. rows,
  79. field_count: FieldType::COUNT,
  80. row_by_row_id: HashMap::default(),
  81. }
  82. }
  83. pub async fn database_filters(&self) -> Vec<FilterPB> {
  84. self.editor.get_all_filters(&self.view_id).await.items
  85. }
  86. pub async fn get_rows(&self) -> Vec<Arc<Row>> {
  87. self.editor.get_rows(&self.view_id).await.unwrap()
  88. }
  89. pub fn get_field(&self, field_id: &str, field_type: FieldType) -> Field {
  90. self
  91. .editor
  92. .get_fields(&self.view_id, None)
  93. .into_iter()
  94. .filter(|field| {
  95. let t_field_type = FieldType::from(field.field_type);
  96. field.id == field_id && t_field_type == field_type
  97. })
  98. .collect::<Vec<_>>()
  99. .pop()
  100. .unwrap()
  101. }
  102. /// returns the first `Field` in the build-in test grid.
  103. /// Not support duplicate `FieldType` in test grid yet.
  104. pub fn get_first_field(&self, field_type: FieldType) -> Field {
  105. self
  106. .editor
  107. .get_fields(&self.view_id, None)
  108. .into_iter()
  109. .filter(|field| {
  110. let t_field_type = FieldType::from(field.field_type);
  111. t_field_type == field_type
  112. })
  113. .collect::<Vec<_>>()
  114. .pop()
  115. .unwrap()
  116. }
  117. pub fn get_fields(&self) -> Vec<Field> {
  118. self.editor.get_fields(&self.view_id, None)
  119. }
  120. pub fn get_multi_select_type_option(&self, field_id: &str) -> Vec<SelectOption> {
  121. let field_type = FieldType::MultiSelect;
  122. let field = self.get_field(field_id, field_type.clone());
  123. let type_option = field
  124. .get_type_option::<MultiSelectTypeOption>(field_type)
  125. .unwrap();
  126. type_option.options
  127. }
  128. pub fn get_single_select_type_option(&self, field_id: &str) -> SingleSelectTypeOption {
  129. let field_type = FieldType::SingleSelect;
  130. let field = self.get_field(field_id, field_type.clone());
  131. field
  132. .get_type_option::<SingleSelectTypeOption>(field_type)
  133. .unwrap()
  134. }
  135. #[allow(dead_code)]
  136. pub fn get_checklist_type_option(&self, field_id: &str) -> ChecklistTypeOption {
  137. let field_type = FieldType::Checklist;
  138. let field = self.get_field(field_id, field_type.clone());
  139. field
  140. .get_type_option::<ChecklistTypeOption>(field_type)
  141. .unwrap()
  142. }
  143. #[allow(dead_code)]
  144. pub fn get_checkbox_type_option(&self, field_id: &str) -> CheckboxTypeOption {
  145. let field_type = FieldType::Checkbox;
  146. let field = self.get_field(field_id, field_type.clone());
  147. field
  148. .get_type_option::<CheckboxTypeOption>(field_type)
  149. .unwrap()
  150. }
  151. pub async fn update_cell<T: ToCellChangeset>(
  152. &mut self,
  153. field_id: &str,
  154. row_id: RowId,
  155. cell_changeset: T,
  156. ) -> FlowyResult<()> {
  157. let field = self
  158. .editor
  159. .get_fields(&self.view_id, None)
  160. .into_iter()
  161. .find(|field| field.id == field_id)
  162. .unwrap();
  163. self
  164. .editor
  165. .update_cell_with_changeset(&self.view_id, row_id, &field.id, cell_changeset)
  166. .await
  167. }
  168. pub(crate) async fn update_text_cell(&mut self, row_id: RowId, content: &str) -> FlowyResult<()> {
  169. let field = self
  170. .editor
  171. .get_fields(&self.view_id, None)
  172. .iter()
  173. .find(|field| {
  174. let field_type = FieldType::from(field.field_type);
  175. field_type == FieldType::RichText
  176. })
  177. .unwrap()
  178. .clone();
  179. self
  180. .update_cell(&field.id, row_id, content.to_string())
  181. .await
  182. }
  183. pub(crate) async fn update_single_select_cell(
  184. &mut self,
  185. row_id: RowId,
  186. option_id: &str,
  187. ) -> FlowyResult<()> {
  188. let field = self
  189. .editor
  190. .get_fields(&self.view_id, None)
  191. .iter()
  192. .find(|field| {
  193. let field_type = FieldType::from(field.field_type);
  194. field_type == FieldType::SingleSelect
  195. })
  196. .unwrap()
  197. .clone();
  198. let cell_changeset = SelectOptionCellChangeset::from_insert_option_id(option_id);
  199. self.update_cell(&field.id, row_id, cell_changeset).await
  200. }
  201. }
  202. pub struct TestRowBuilder {
  203. row_id: RowId,
  204. fields: Vec<Field>,
  205. cell_build: CellBuilder,
  206. }
  207. impl TestRowBuilder {
  208. pub fn new(row_id: RowId, fields: Vec<Field>) -> Self {
  209. let inner_builder = CellBuilder::with_cells(Default::default(), fields.clone());
  210. Self {
  211. row_id,
  212. fields,
  213. cell_build: inner_builder,
  214. }
  215. }
  216. pub fn insert_text_cell(&mut self, data: &str) -> String {
  217. let text_field = self.field_with_type(&FieldType::RichText);
  218. self
  219. .cell_build
  220. .insert_text_cell(&text_field.id, data.to_string());
  221. text_field.id.clone()
  222. }
  223. pub fn insert_number_cell(&mut self, data: &str) -> String {
  224. let number_field = self.field_with_type(&FieldType::Number);
  225. self
  226. .cell_build
  227. .insert_text_cell(&number_field.id, data.to_string());
  228. number_field.id.clone()
  229. }
  230. pub fn insert_date_cell(
  231. &mut self,
  232. data: &str,
  233. time: Option<String>,
  234. include_time: Option<bool>,
  235. timezone_id: Option<String>,
  236. ) -> String {
  237. let value = serde_json::to_string(&DateCellChangeset {
  238. date: Some(data.to_string()),
  239. time,
  240. include_time,
  241. timezone_id,
  242. })
  243. .unwrap();
  244. let date_field = self.field_with_type(&FieldType::DateTime);
  245. self.cell_build.insert_text_cell(&date_field.id, value);
  246. date_field.id.clone()
  247. }
  248. pub fn insert_checkbox_cell(&mut self, data: &str) -> String {
  249. let checkbox_field = self.field_with_type(&FieldType::Checkbox);
  250. self
  251. .cell_build
  252. .insert_text_cell(&checkbox_field.id, data.to_string());
  253. checkbox_field.id.clone()
  254. }
  255. pub fn insert_url_cell(&mut self, content: &str) -> String {
  256. let url_field = self.field_with_type(&FieldType::URL);
  257. self
  258. .cell_build
  259. .insert_url_cell(&url_field.id, content.to_string());
  260. url_field.id.clone()
  261. }
  262. pub fn insert_single_select_cell<F>(&mut self, f: F) -> String
  263. where
  264. F: Fn(Vec<SelectOption>) -> SelectOption,
  265. {
  266. let single_select_field = self.field_with_type(&FieldType::SingleSelect);
  267. let type_option = single_select_field
  268. .get_type_option::<ChecklistTypeOption>(FieldType::SingleSelect)
  269. .unwrap();
  270. let option = f(type_option.options);
  271. self
  272. .cell_build
  273. .insert_select_option_cell(&single_select_field.id, vec![option.id]);
  274. single_select_field.id.clone()
  275. }
  276. pub fn insert_multi_select_cell<F>(&mut self, f: F) -> String
  277. where
  278. F: Fn(Vec<SelectOption>) -> Vec<SelectOption>,
  279. {
  280. let multi_select_field = self.field_with_type(&FieldType::MultiSelect);
  281. let type_option = multi_select_field
  282. .get_type_option::<ChecklistTypeOption>(FieldType::MultiSelect)
  283. .unwrap();
  284. let options = f(type_option.options);
  285. let ops_ids = options
  286. .iter()
  287. .map(|option| option.id.clone())
  288. .collect::<Vec<_>>();
  289. self
  290. .cell_build
  291. .insert_select_option_cell(&multi_select_field.id, ops_ids);
  292. multi_select_field.id.clone()
  293. }
  294. pub fn insert_checklist_cell<F>(&mut self, f: F) -> String
  295. where
  296. F: Fn(Vec<SelectOption>) -> Vec<SelectOption>,
  297. {
  298. let checklist_field = self.field_with_type(&FieldType::Checklist);
  299. let type_option = checklist_field
  300. .get_type_option::<ChecklistTypeOption>(FieldType::Checklist)
  301. .unwrap();
  302. let options = f(type_option.options);
  303. let ops_ids = options
  304. .iter()
  305. .map(|option| option.id.clone())
  306. .collect::<Vec<_>>();
  307. self
  308. .cell_build
  309. .insert_select_option_cell(&checklist_field.id, ops_ids);
  310. checklist_field.id.clone()
  311. }
  312. pub fn field_with_type(&self, field_type: &FieldType) -> Field {
  313. self
  314. .fields
  315. .iter()
  316. .find(|field| {
  317. let t_field_type = FieldType::from(field.field_type);
  318. &t_field_type == field_type
  319. })
  320. .unwrap()
  321. .clone()
  322. }
  323. pub fn build(self) -> CreateRowParams {
  324. CreateRowParams {
  325. id: self.row_id,
  326. cells: self.cell_build.build(),
  327. height: 60,
  328. visibility: true,
  329. prev_row_id: None,
  330. timestamp: 0,
  331. }
  332. }
  333. }