script.rs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. use collab_database::fields::Field;
  2. use collab_database::rows::RowId;
  3. use flowy_database2::entities::{CreateRowParams, FieldType, GroupPB, RowPB};
  4. use flowy_database2::services::cell::{
  5. delete_select_option_cell, insert_select_option_cell, insert_url_cell,
  6. };
  7. use flowy_database2::services::field::{
  8. edit_single_select_type_option, SelectOption, SelectTypeOptionSharedAction,
  9. SingleSelectTypeOption,
  10. };
  11. use crate::database::database_editor::DatabaseEditorTest;
  12. pub enum GroupScript {
  13. AssertGroupRowCount {
  14. group_index: usize,
  15. row_count: usize,
  16. },
  17. AssertGroupCount(usize),
  18. AssertGroup {
  19. group_index: usize,
  20. expected_group: GroupPB,
  21. },
  22. AssertRow {
  23. group_index: usize,
  24. row_index: usize,
  25. row: RowPB,
  26. },
  27. MoveRow {
  28. from_group_index: usize,
  29. from_row_index: usize,
  30. to_group_index: usize,
  31. to_row_index: usize,
  32. },
  33. CreateRow {
  34. group_index: usize,
  35. },
  36. DeleteRow {
  37. group_index: usize,
  38. row_index: usize,
  39. },
  40. UpdateGroupedCell {
  41. from_group_index: usize,
  42. row_index: usize,
  43. to_group_index: usize,
  44. },
  45. UpdateGroupedCellWithData {
  46. from_group_index: usize,
  47. row_index: usize,
  48. cell_data: String,
  49. },
  50. MoveGroup {
  51. from_group_index: usize,
  52. to_group_index: usize,
  53. },
  54. UpdateSingleSelectSelectOption {
  55. inserted_options: Vec<SelectOption>,
  56. },
  57. GroupByField {
  58. field_id: String,
  59. },
  60. }
  61. pub struct DatabaseGroupTest {
  62. inner: DatabaseEditorTest,
  63. }
  64. impl DatabaseGroupTest {
  65. pub async fn new() -> Self {
  66. let editor_test = DatabaseEditorTest::new_board().await;
  67. Self { inner: editor_test }
  68. }
  69. pub async fn run_scripts(&mut self, scripts: Vec<GroupScript>) {
  70. for script in scripts {
  71. self.run_script(script).await;
  72. }
  73. }
  74. pub async fn run_script(&mut self, script: GroupScript) {
  75. match script {
  76. GroupScript::AssertGroupRowCount {
  77. group_index,
  78. row_count,
  79. } => {
  80. assert_eq!(row_count, self.group_at_index(group_index).await.rows.len());
  81. },
  82. GroupScript::AssertGroupCount(count) => {
  83. let groups = self.editor.load_groups(&self.view_id).await.unwrap();
  84. assert_eq!(count, groups.len());
  85. },
  86. GroupScript::MoveRow {
  87. from_group_index,
  88. from_row_index,
  89. to_group_index,
  90. to_row_index,
  91. } => {
  92. let groups: Vec<GroupPB> = self.editor.load_groups(&self.view_id).await.unwrap().items;
  93. let from_row = groups
  94. .get(from_group_index)
  95. .unwrap()
  96. .rows
  97. .get(from_row_index)
  98. .unwrap();
  99. let to_group = groups.get(to_group_index).unwrap();
  100. let to_row = to_group.rows.get(to_row_index).unwrap();
  101. let from_row = RowId::from(from_row.id.clone());
  102. let to_row = RowId::from(to_row.id.clone());
  103. self
  104. .editor
  105. .move_group_row(&self.view_id, &to_group.group_id, from_row, Some(to_row))
  106. .await
  107. .unwrap();
  108. },
  109. GroupScript::AssertRow {
  110. group_index,
  111. row_index,
  112. row,
  113. } => {
  114. //
  115. let group = self.group_at_index(group_index).await;
  116. let compare_row = group.rows.get(row_index).unwrap().clone();
  117. assert_eq!(row.id, compare_row.id);
  118. },
  119. GroupScript::CreateRow { group_index } => {
  120. let group = self.group_at_index(group_index).await;
  121. let params = CreateRowParams {
  122. view_id: self.view_id.clone(),
  123. start_row_id: None,
  124. group_id: Some(group.group_id.clone()),
  125. cell_data_by_field_id: None,
  126. };
  127. let _ = self.editor.create_row(params).await.unwrap();
  128. },
  129. GroupScript::DeleteRow {
  130. group_index,
  131. row_index,
  132. } => {
  133. let row = self.row_at_index(group_index, row_index).await;
  134. let row_id = RowId::from(row.id);
  135. self.editor.delete_row(&row_id).await;
  136. },
  137. GroupScript::UpdateGroupedCell {
  138. from_group_index,
  139. row_index,
  140. to_group_index,
  141. } => {
  142. let from_group = self.group_at_index(from_group_index).await;
  143. let to_group = self.group_at_index(to_group_index).await;
  144. let field_id = from_group.field_id;
  145. let field = self.editor.get_field(&field_id).unwrap();
  146. let field_type = FieldType::from(field.field_type);
  147. let cell = if to_group.is_default {
  148. match field_type {
  149. FieldType::SingleSelect => {
  150. delete_select_option_cell(vec![to_group.group_id.clone()], &field)
  151. },
  152. FieldType::MultiSelect => {
  153. delete_select_option_cell(vec![to_group.group_id.clone()], &field)
  154. },
  155. _ => {
  156. panic!("Unsupported group field type");
  157. },
  158. }
  159. } else {
  160. match field_type {
  161. FieldType::SingleSelect => {
  162. insert_select_option_cell(vec![to_group.group_id.clone()], &field)
  163. },
  164. FieldType::MultiSelect => {
  165. insert_select_option_cell(vec![to_group.group_id.clone()], &field)
  166. },
  167. FieldType::URL => insert_url_cell(to_group.group_id.clone(), &field),
  168. _ => {
  169. panic!("Unsupported group field type");
  170. },
  171. }
  172. };
  173. let row_id = RowId::from(self.row_at_index(from_group_index, row_index).await.id);
  174. self
  175. .editor
  176. .update_cell(&self.view_id, row_id, &field_id, cell)
  177. .await;
  178. },
  179. GroupScript::UpdateGroupedCellWithData {
  180. from_group_index,
  181. row_index,
  182. cell_data,
  183. } => {
  184. let from_group = self.group_at_index(from_group_index).await;
  185. let field_id = from_group.field_id;
  186. let field = self.editor.get_field(&field_id).unwrap();
  187. let field_type = FieldType::from(field.field_type);
  188. let cell = match field_type {
  189. FieldType::URL => insert_url_cell(cell_data, &field),
  190. _ => {
  191. panic!("Unsupported group field type");
  192. },
  193. };
  194. let row_id = RowId::from(self.row_at_index(from_group_index, row_index).await.id);
  195. self
  196. .editor
  197. .update_cell(&self.view_id, row_id, &field_id, cell)
  198. .await;
  199. },
  200. GroupScript::MoveGroup {
  201. from_group_index,
  202. to_group_index,
  203. } => {
  204. let from_group = self.group_at_index(from_group_index).await;
  205. let to_group = self.group_at_index(to_group_index).await;
  206. self
  207. .editor
  208. .move_group(&self.view_id, &from_group.group_id, &to_group.group_id)
  209. .await
  210. .unwrap();
  211. //
  212. },
  213. GroupScript::AssertGroup {
  214. group_index,
  215. expected_group: group_pb,
  216. } => {
  217. let group = self.group_at_index(group_index).await;
  218. assert_eq!(group.group_id, group_pb.group_id);
  219. assert_eq!(group.desc, group_pb.desc);
  220. },
  221. GroupScript::UpdateSingleSelectSelectOption { inserted_options } => {
  222. self
  223. .edit_single_select_type_option(|type_option| {
  224. for inserted_option in inserted_options {
  225. type_option.insert_option(inserted_option);
  226. }
  227. })
  228. .await;
  229. },
  230. GroupScript::GroupByField { field_id } => {
  231. self
  232. .editor
  233. .group_by_field(&self.view_id, &field_id)
  234. .await
  235. .unwrap();
  236. },
  237. }
  238. }
  239. pub async fn group_at_index(&self, index: usize) -> GroupPB {
  240. let groups = self.editor.load_groups(&self.view_id).await.unwrap().items;
  241. groups.get(index).unwrap().clone()
  242. }
  243. pub async fn row_at_index(&self, group_index: usize, row_index: usize) -> RowPB {
  244. let groups = self.group_at_index(group_index).await;
  245. groups.rows.get(row_index).unwrap().clone()
  246. }
  247. #[allow(dead_code)]
  248. pub async fn get_multi_select_field(&self) -> Field {
  249. self
  250. .inner
  251. .get_fields()
  252. .into_iter()
  253. .find(|field_rev| {
  254. let field_type = FieldType::from(field_rev.field_type);
  255. field_type.is_multi_select()
  256. })
  257. .unwrap()
  258. }
  259. pub async fn get_single_select_field(&self) -> Field {
  260. self
  261. .inner
  262. .get_fields()
  263. .into_iter()
  264. .find(|field| {
  265. let field_type = FieldType::from(field.field_type);
  266. field_type.is_single_select()
  267. })
  268. .unwrap()
  269. }
  270. pub async fn edit_single_select_type_option(
  271. &self,
  272. action: impl FnOnce(&mut SingleSelectTypeOption),
  273. ) {
  274. let single_select = self.get_single_select_field().await;
  275. edit_single_select_type_option(
  276. &self.view_id,
  277. &single_select.id,
  278. self.editor.clone(),
  279. action,
  280. )
  281. .await
  282. .unwrap();
  283. }
  284. pub async fn get_url_field(&self) -> Field {
  285. self
  286. .inner
  287. .get_fields()
  288. .into_iter()
  289. .find(|field| {
  290. let field_type = FieldType::from(field.field_type);
  291. field_type.is_url()
  292. })
  293. .unwrap()
  294. }
  295. }
  296. impl std::ops::Deref for DatabaseGroupTest {
  297. type Target = DatabaseEditorTest;
  298. fn deref(&self) -> &Self::Target {
  299. &self.inner
  300. }
  301. }
  302. impl std::ops::DerefMut for DatabaseGroupTest {
  303. fn deref_mut(&mut self) -> &mut Self::Target {
  304. &mut self.inner
  305. }
  306. }