| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | use std::collections::HashMap;
 | 
					
						
							|  |  |  | use std::sync::Arc;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 |  |  | use collab_database::database::{gen_database_view_id, timestamp};
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | use collab_database::fields::Field;
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  | use collab_database::rows::{CreateRowParams, RowDetail, RowId};
 | 
					
						
							| 
									
										
										
										
											2023-11-29 04:42:53 +08:00
										 |  |  | use collab_database::views::OrderObjectPosition;
 | 
					
						
							| 
									
										
										
										
											2024-02-22 07:12:52 +08:00
										 |  |  | use lib_infra::box_any::BoxAny;
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | use strum::EnumCount;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-25 15:25:31 +08:00
										 |  |  | use event_integration::folder_event::ViewTest;
 | 
					
						
							| 
									
										
										
										
											2023-10-24 20:11:06 +08:00
										 |  |  | use event_integration::EventIntegrationTest;
 | 
					
						
							| 
									
										
										
										
											2023-10-24 10:15:28 +08:00
										 |  |  | use flowy_database2::entities::{FieldType, FilterPB, RowMetaPB};
 | 
					
						
							| 
									
										
										
										
											2024-02-22 07:12:52 +08:00
										 |  |  | use flowy_database2::services::cell::CellBuilder;
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  | use flowy_database2::services::database::DatabaseEditor;
 | 
					
						
							| 
									
										
										
										
											2023-05-30 09:41:33 +08:00
										 |  |  | use flowy_database2::services::field::checklist_type_option::{
 | 
					
						
							|  |  |  |   ChecklistCellChangeset, ChecklistTypeOption,
 | 
					
						
							|  |  |  | };
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | use flowy_database2::services::field::{
 | 
					
						
							| 
									
										
										
										
											2024-02-22 07:12:52 +08:00
										 |  |  |   CheckboxTypeOption, MultiSelectTypeOption, SelectOption, SelectOptionCellChangeset,
 | 
					
						
							|  |  |  |   SingleSelectTypeOption,
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:29:18 +08:00
										 |  |  | use flowy_database2::services::share::csv::{CSVFormat, ImportResult};
 | 
					
						
							| 
									
										
										
										
											2023-05-10 19:43:32 +08:00
										 |  |  | use flowy_error::FlowyResult;
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  | use crate::database::mock_data::{
 | 
					
						
							|  |  |  |   make_no_date_test_grid, make_test_board, make_test_calendar, make_test_grid,
 | 
					
						
							|  |  |  | };
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub struct DatabaseEditorTest {
 | 
					
						
							| 
									
										
										
										
											2023-10-24 20:11:06 +08:00
										 |  |  |   pub sdk: EventIntegrationTest,
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   pub view_id: String,
 | 
					
						
							|  |  |  |   pub editor: Arc<DatabaseEditor>,
 | 
					
						
							|  |  |  |   pub fields: Vec<Arc<Field>>,
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |   pub row_details: Vec<Arc<RowDetail>>,
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   pub field_count: usize,
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |   pub row_by_row_id: HashMap<String, RowMetaPB>,
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl DatabaseEditorTest {
 | 
					
						
							|  |  |  |   pub async fn new_grid() -> Self {
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:35:06 +08:00
										 |  |  |     let sdk = EventIntegrationTest::new().await;
 | 
					
						
							| 
									
										
										
										
											2023-10-24 23:13:51 +08:00
										 |  |  |     let _ = sdk.init_anon_user().await;
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let params = make_test_grid();
 | 
					
						
							|  |  |  |     let view_test = ViewTest::new_grid_view(&sdk, params.to_json_bytes().unwrap()).await;
 | 
					
						
							|  |  |  |     Self::new(sdk, view_test).await
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |   pub async fn new_no_date_grid() -> Self {
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:35:06 +08:00
										 |  |  |     let sdk = EventIntegrationTest::new().await;
 | 
					
						
							| 
									
										
										
										
											2023-10-24 23:13:51 +08:00
										 |  |  |     let _ = sdk.init_anon_user().await;
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let params = make_no_date_test_grid();
 | 
					
						
							|  |  |  |     let view_test = ViewTest::new_grid_view(&sdk, params.to_json_bytes().unwrap()).await;
 | 
					
						
							|  |  |  |     Self::new(sdk, view_test).await
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |   pub async fn new_board() -> Self {
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:35:06 +08:00
										 |  |  |     let sdk = EventIntegrationTest::new().await;
 | 
					
						
							| 
									
										
										
										
											2023-10-24 23:13:51 +08:00
										 |  |  |     let _ = sdk.init_anon_user().await;
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let params = make_test_board();
 | 
					
						
							| 
									
										
										
										
											2023-10-26 11:48:58 +08:00
										 |  |  |     let view_test = ViewTest::new_board_view(&sdk, params.to_json_bytes().unwrap()).await;
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |     Self::new(sdk, view_test).await
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |   pub async fn new_calendar() -> Self {
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:35:06 +08:00
										 |  |  |     let sdk = EventIntegrationTest::new().await;
 | 
					
						
							| 
									
										
										
										
											2023-10-24 23:13:51 +08:00
										 |  |  |     let _ = sdk.init_anon_user().await;
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |     let params = make_test_calendar();
 | 
					
						
							|  |  |  |     let view_test = ViewTest::new_grid_view(&sdk, params.to_json_bytes().unwrap()).await;
 | 
					
						
							|  |  |  |     Self::new(sdk, view_test).await
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-24 20:11:06 +08:00
										 |  |  |   pub async fn new(sdk: EventIntegrationTest, test: ViewTest) -> Self {
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     let editor = sdk
 | 
					
						
							|  |  |  |       .database_manager
 | 
					
						
							| 
									
										
										
										
											2023-05-25 23:22:23 +08:00
										 |  |  |       .get_database_with_view_id(&test.child_view.id)
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |       .await
 | 
					
						
							|  |  |  |       .unwrap();
 | 
					
						
							|  |  |  |     let fields = editor
 | 
					
						
							|  |  |  |       .get_fields(&test.child_view.id, None)
 | 
					
						
							|  |  |  |       .into_iter()
 | 
					
						
							|  |  |  |       .map(Arc::new)
 | 
					
						
							|  |  |  |       .collect();
 | 
					
						
							|  |  |  |     let rows = editor
 | 
					
						
							|  |  |  |       .get_rows(&test.child_view.id)
 | 
					
						
							|  |  |  |       .await
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							|  |  |  |       .into_iter()
 | 
					
						
							|  |  |  |       .collect();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let view_id = test.child_view.id;
 | 
					
						
							|  |  |  |     Self {
 | 
					
						
							|  |  |  |       sdk,
 | 
					
						
							|  |  |  |       view_id,
 | 
					
						
							|  |  |  |       editor,
 | 
					
						
							|  |  |  |       fields,
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |       row_details: rows,
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |       field_count: FieldType::COUNT,
 | 
					
						
							|  |  |  |       row_by_row_id: HashMap::default(),
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub async fn database_filters(&self) -> Vec<FilterPB> {
 | 
					
						
							|  |  |  |     self.editor.get_all_filters(&self.view_id).await.items
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |   pub async fn get_rows(&self) -> Vec<Arc<RowDetail>> {
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     self.editor.get_rows(&self.view_id).await.unwrap()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn get_field(&self, field_id: &str, field_type: FieldType) -> Field {
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .editor
 | 
					
						
							|  |  |  |       .get_fields(&self.view_id, None)
 | 
					
						
							|  |  |  |       .into_iter()
 | 
					
						
							|  |  |  |       .filter(|field| {
 | 
					
						
							|  |  |  |         let t_field_type = FieldType::from(field.field_type);
 | 
					
						
							|  |  |  |         field.id == field_id && t_field_type == field_type
 | 
					
						
							|  |  |  |       })
 | 
					
						
							|  |  |  |       .collect::<Vec<_>>()
 | 
					
						
							|  |  |  |       .pop()
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// returns the first `Field` in the build-in test grid.
 | 
					
						
							|  |  |  |   /// Not support duplicate `FieldType` in test grid yet.
 | 
					
						
							|  |  |  |   pub fn get_first_field(&self, field_type: FieldType) -> Field {
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .editor
 | 
					
						
							|  |  |  |       .get_fields(&self.view_id, None)
 | 
					
						
							|  |  |  |       .into_iter()
 | 
					
						
							|  |  |  |       .filter(|field| {
 | 
					
						
							|  |  |  |         let t_field_type = FieldType::from(field.field_type);
 | 
					
						
							|  |  |  |         t_field_type == field_type
 | 
					
						
							|  |  |  |       })
 | 
					
						
							|  |  |  |       .collect::<Vec<_>>()
 | 
					
						
							|  |  |  |       .pop()
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn get_fields(&self) -> Vec<Field> {
 | 
					
						
							|  |  |  |     self.editor.get_fields(&self.view_id, None)
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn get_multi_select_type_option(&self, field_id: &str) -> Vec<SelectOption> {
 | 
					
						
							|  |  |  |     let field_type = FieldType::MultiSelect;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 09:14:02 -08:00
										 |  |  |     let field = self.get_field(field_id, field_type);
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     let type_option = field
 | 
					
						
							|  |  |  |       .get_type_option::<MultiSelectTypeOption>(field_type)
 | 
					
						
							|  |  |  |       .unwrap();
 | 
					
						
							|  |  |  |     type_option.options
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-16 17:18:30 +08:00
										 |  |  |   pub fn get_single_select_type_option(&self, field_id: &str) -> Vec<SelectOption> {
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     let field_type = FieldType::SingleSelect;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 09:14:02 -08:00
										 |  |  |     let field = self.get_field(field_id, field_type);
 | 
					
						
							| 
									
										
										
										
											2024-03-16 17:18:30 +08:00
										 |  |  |     let type_option = field
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |       .get_type_option::<SingleSelectTypeOption>(field_type)
 | 
					
						
							| 
									
										
										
										
											2024-03-16 17:18:30 +08:00
										 |  |  |       .unwrap();
 | 
					
						
							|  |  |  |     type_option.options
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   #[allow(dead_code)]
 | 
					
						
							|  |  |  |   pub fn get_checklist_type_option(&self, field_id: &str) -> ChecklistTypeOption {
 | 
					
						
							|  |  |  |     let field_type = FieldType::Checklist;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 09:14:02 -08:00
										 |  |  |     let field = self.get_field(field_id, field_type);
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     field
 | 
					
						
							|  |  |  |       .get_type_option::<ChecklistTypeOption>(field_type)
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   #[allow(dead_code)]
 | 
					
						
							|  |  |  |   pub fn get_checkbox_type_option(&self, field_id: &str) -> CheckboxTypeOption {
 | 
					
						
							|  |  |  |     let field_type = FieldType::Checkbox;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 09:14:02 -08:00
										 |  |  |     let field = self.get_field(field_id, field_type);
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     field
 | 
					
						
							|  |  |  |       .get_type_option::<CheckboxTypeOption>(field_type)
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 07:12:52 +08:00
										 |  |  |   pub async fn update_cell(
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     &mut self,
 | 
					
						
							|  |  |  |     field_id: &str,
 | 
					
						
							|  |  |  |     row_id: RowId,
 | 
					
						
							| 
									
										
										
										
											2024-02-22 07:12:52 +08:00
										 |  |  |     cell_changeset: BoxAny,
 | 
					
						
							| 
									
										
										
										
											2023-05-10 19:43:32 +08:00
										 |  |  |   ) -> FlowyResult<()> {
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     let field = self
 | 
					
						
							|  |  |  |       .editor
 | 
					
						
							|  |  |  |       .get_fields(&self.view_id, None)
 | 
					
						
							|  |  |  |       .into_iter()
 | 
					
						
							|  |  |  |       .find(|field| field.id == field_id)
 | 
					
						
							|  |  |  |       .unwrap();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .editor
 | 
					
						
							|  |  |  |       .update_cell_with_changeset(&self.view_id, row_id, &field.id, cell_changeset)
 | 
					
						
							| 
									
										
										
										
											2023-05-10 19:43:32 +08:00
										 |  |  |       .await
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-10 19:43:32 +08:00
										 |  |  |   pub(crate) async fn update_text_cell(&mut self, row_id: RowId, content: &str) -> FlowyResult<()> {
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     let field = self
 | 
					
						
							|  |  |  |       .editor
 | 
					
						
							|  |  |  |       .get_fields(&self.view_id, None)
 | 
					
						
							|  |  |  |       .iter()
 | 
					
						
							|  |  |  |       .find(|field| {
 | 
					
						
							|  |  |  |         let field_type = FieldType::from(field.field_type);
 | 
					
						
							|  |  |  |         field_type == FieldType::RichText
 | 
					
						
							|  |  |  |       })
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							|  |  |  |       .clone();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     self
 | 
					
						
							| 
									
										
										
										
											2024-02-22 07:12:52 +08:00
										 |  |  |       .update_cell(&field.id, row_id, BoxAny::new(content.to_string()))
 | 
					
						
							| 
									
										
										
										
											2023-05-10 19:43:32 +08:00
										 |  |  |       .await
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-30 09:41:33 +08:00
										 |  |  |   pub(crate) async fn set_checklist_cell(
 | 
					
						
							|  |  |  |     &mut self,
 | 
					
						
							|  |  |  |     row_id: RowId,
 | 
					
						
							| 
									
										
										
										
											2023-10-24 10:15:28 +08:00
										 |  |  |     selected_options: Vec<String>,
 | 
					
						
							| 
									
										
										
										
											2023-05-30 09:41:33 +08:00
										 |  |  |   ) -> FlowyResult<()> {
 | 
					
						
							|  |  |  |     let field = self
 | 
					
						
							|  |  |  |       .editor
 | 
					
						
							|  |  |  |       .get_fields(&self.view_id, None)
 | 
					
						
							|  |  |  |       .iter()
 | 
					
						
							|  |  |  |       .find(|field| {
 | 
					
						
							|  |  |  |         let field_type = FieldType::from(field.field_type);
 | 
					
						
							|  |  |  |         field_type == FieldType::Checklist
 | 
					
						
							|  |  |  |       })
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							|  |  |  |       .clone();
 | 
					
						
							|  |  |  |     let cell_changeset = ChecklistCellChangeset {
 | 
					
						
							| 
									
										
										
										
											2023-10-24 10:15:28 +08:00
										 |  |  |       selected_option_ids: selected_options,
 | 
					
						
							| 
									
										
										
										
											2023-05-30 09:41:33 +08:00
										 |  |  |       ..Default::default()
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .editor
 | 
					
						
							|  |  |  |       .set_checklist_options(&self.view_id, row_id, &field.id, cell_changeset)
 | 
					
						
							|  |  |  |       .await
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-10 19:43:32 +08:00
										 |  |  |   pub(crate) async fn update_single_select_cell(
 | 
					
						
							|  |  |  |     &mut self,
 | 
					
						
							|  |  |  |     row_id: RowId,
 | 
					
						
							|  |  |  |     option_id: &str,
 | 
					
						
							|  |  |  |   ) -> FlowyResult<()> {
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     let field = self
 | 
					
						
							|  |  |  |       .editor
 | 
					
						
							|  |  |  |       .get_fields(&self.view_id, None)
 | 
					
						
							|  |  |  |       .iter()
 | 
					
						
							|  |  |  |       .find(|field| {
 | 
					
						
							|  |  |  |         let field_type = FieldType::from(field.field_type);
 | 
					
						
							|  |  |  |         field_type == FieldType::SingleSelect
 | 
					
						
							|  |  |  |       })
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							|  |  |  |       .clone();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let cell_changeset = SelectOptionCellChangeset::from_insert_option_id(option_id);
 | 
					
						
							| 
									
										
										
										
											2024-02-22 07:12:52 +08:00
										 |  |  |     self
 | 
					
						
							|  |  |  |       .update_cell(&field.id, row_id, BoxAny::new(cell_changeset))
 | 
					
						
							|  |  |  |       .await
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2023-05-25 23:22:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:29:18 +08:00
										 |  |  |   pub async fn import(&self, s: String, format: CSVFormat) -> ImportResult {
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .sdk
 | 
					
						
							|  |  |  |       .database_manager
 | 
					
						
							| 
									
										
										
										
											2023-05-31 14:08:54 +08:00
										 |  |  |       .import_csv(gen_database_view_id(), s, format)
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:29:18 +08:00
										 |  |  |       .await
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							| 
									
										
										
										
											2023-05-25 23:22:23 +08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub async fn get_database(&self, database_id: &str) -> Option<Arc<DatabaseEditor>> {
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .sdk
 | 
					
						
							|  |  |  |       .database_manager
 | 
					
						
							|  |  |  |       .get_database(database_id)
 | 
					
						
							|  |  |  |       .await
 | 
					
						
							|  |  |  |       .ok()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-25 23:22:23 +08:00
										 |  |  | pub struct TestRowBuilder<'a> {
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   row_id: RowId,
 | 
					
						
							| 
									
										
										
										
											2023-05-25 23:22:23 +08:00
										 |  |  |   fields: &'a [Field],
 | 
					
						
							|  |  |  |   cell_build: CellBuilder<'a>,
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-25 23:22:23 +08:00
										 |  |  | impl<'a> TestRowBuilder<'a> {
 | 
					
						
							|  |  |  |   pub fn new(row_id: RowId, fields: &'a [Field]) -> Self {
 | 
					
						
							|  |  |  |     let cell_build = CellBuilder::with_cells(Default::default(), fields);
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     Self {
 | 
					
						
							|  |  |  |       row_id,
 | 
					
						
							|  |  |  |       fields,
 | 
					
						
							| 
									
										
										
										
											2023-05-25 23:22:23 +08:00
										 |  |  |       cell_build,
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn insert_text_cell(&mut self, data: &str) -> String {
 | 
					
						
							|  |  |  |     let text_field = self.field_with_type(&FieldType::RichText);
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .cell_build
 | 
					
						
							|  |  |  |       .insert_text_cell(&text_field.id, data.to_string());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     text_field.id.clone()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn insert_number_cell(&mut self, data: &str) -> String {
 | 
					
						
							|  |  |  |     let number_field = self.field_with_type(&FieldType::Number);
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .cell_build
 | 
					
						
							|  |  |  |       .insert_text_cell(&number_field.id, data.to_string());
 | 
					
						
							|  |  |  |     number_field.id.clone()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-10 19:43:32 +08:00
										 |  |  |   pub fn insert_date_cell(
 | 
					
						
							|  |  |  |     &mut self,
 | 
					
						
							| 
									
										
										
										
											2024-02-22 07:12:52 +08:00
										 |  |  |     date: i64,
 | 
					
						
							| 
									
										
										
										
											2023-05-10 19:43:32 +08:00
										 |  |  |     time: Option<String>,
 | 
					
						
							|  |  |  |     include_time: Option<bool>,
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  |     field_type: &FieldType,
 | 
					
						
							| 
									
										
										
										
											2023-05-10 19:43:32 +08:00
										 |  |  |   ) -> String {
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  |     let date_field = self.field_with_type(field_type);
 | 
					
						
							| 
									
										
										
										
											2024-02-22 07:12:52 +08:00
										 |  |  |     self
 | 
					
						
							|  |  |  |       .cell_build
 | 
					
						
							|  |  |  |       .insert_date_cell(&date_field.id, date, time, include_time);
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     date_field.id.clone()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn insert_checkbox_cell(&mut self, data: &str) -> String {
 | 
					
						
							|  |  |  |     let checkbox_field = self.field_with_type(&FieldType::Checkbox);
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .cell_build
 | 
					
						
							|  |  |  |       .insert_text_cell(&checkbox_field.id, data.to_string());
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     checkbox_field.id.clone()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn insert_url_cell(&mut self, content: &str) -> String {
 | 
					
						
							|  |  |  |     let url_field = self.field_with_type(&FieldType::URL);
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .cell_build
 | 
					
						
							|  |  |  |       .insert_url_cell(&url_field.id, content.to_string());
 | 
					
						
							|  |  |  |     url_field.id.clone()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn insert_single_select_cell<F>(&mut self, f: F) -> String | 
					
						
							|  |  |  |   where
 | 
					
						
							|  |  |  |     F: Fn(Vec<SelectOption>) -> SelectOption,
 | 
					
						
							|  |  |  |   {
 | 
					
						
							|  |  |  |     let single_select_field = self.field_with_type(&FieldType::SingleSelect);
 | 
					
						
							|  |  |  |     let type_option = single_select_field
 | 
					
						
							| 
									
										
										
										
											2023-05-30 09:41:33 +08:00
										 |  |  |       .get_type_option::<SingleSelectTypeOption>(FieldType::SingleSelect)
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |       .unwrap();
 | 
					
						
							|  |  |  |     let option = f(type_option.options);
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .cell_build
 | 
					
						
							|  |  |  |       .insert_select_option_cell(&single_select_field.id, vec![option.id]);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     single_select_field.id.clone()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn insert_multi_select_cell<F>(&mut self, f: F) -> String | 
					
						
							|  |  |  |   where
 | 
					
						
							|  |  |  |     F: Fn(Vec<SelectOption>) -> Vec<SelectOption>,
 | 
					
						
							|  |  |  |   {
 | 
					
						
							|  |  |  |     let multi_select_field = self.field_with_type(&FieldType::MultiSelect);
 | 
					
						
							|  |  |  |     let type_option = multi_select_field
 | 
					
						
							| 
									
										
										
										
											2023-05-30 09:41:33 +08:00
										 |  |  |       .get_type_option::<MultiSelectTypeOption>(FieldType::MultiSelect)
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |       .unwrap();
 | 
					
						
							|  |  |  |     let options = f(type_option.options);
 | 
					
						
							|  |  |  |     let ops_ids = options
 | 
					
						
							|  |  |  |       .iter()
 | 
					
						
							|  |  |  |       .map(|option| option.id.clone())
 | 
					
						
							|  |  |  |       .collect::<Vec<_>>();
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .cell_build
 | 
					
						
							|  |  |  |       .insert_select_option_cell(&multi_select_field.id, ops_ids);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     multi_select_field.id.clone()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 08:00:59 +08:00
										 |  |  |   pub fn insert_checklist_cell(&mut self, options: Vec<(String, bool)>) -> String {
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     let checklist_field = self.field_with_type(&FieldType::Checklist);
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .cell_build
 | 
					
						
							| 
									
										
										
										
											2024-02-22 08:00:59 +08:00
										 |  |  |       .insert_checklist_cell(&checklist_field.id, options);
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     checklist_field.id.clone()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn field_with_type(&self, field_type: &FieldType) -> Field {
 | 
					
						
							|  |  |  |     self
 | 
					
						
							|  |  |  |       .fields
 | 
					
						
							|  |  |  |       .iter()
 | 
					
						
							|  |  |  |       .find(|field| {
 | 
					
						
							|  |  |  |         let t_field_type = FieldType::from(field.field_type);
 | 
					
						
							|  |  |  |         &t_field_type == field_type
 | 
					
						
							|  |  |  |       })
 | 
					
						
							|  |  |  |       .unwrap()
 | 
					
						
							|  |  |  |       .clone()
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pub fn build(self) -> CreateRowParams {
 | 
					
						
							|  |  |  |     CreateRowParams {
 | 
					
						
							|  |  |  |       id: self.row_id,
 | 
					
						
							|  |  |  |       cells: self.cell_build.build(),
 | 
					
						
							|  |  |  |       height: 60,
 | 
					
						
							|  |  |  |       visibility: true,
 | 
					
						
							| 
									
										
										
										
											2023-11-29 04:42:53 +08:00
										 |  |  |       row_position: OrderObjectPosition::End,
 | 
					
						
							| 
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 |  |  |       timestamp: timestamp(),
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 |