script.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. .unwrap();
  179. },
  180. GroupScript::UpdateGroupedCellWithData {
  181. from_group_index,
  182. row_index,
  183. cell_data,
  184. } => {
  185. let from_group = self.group_at_index(from_group_index).await;
  186. let field_id = from_group.field_id;
  187. let field = self.editor.get_field(&field_id).unwrap();
  188. let field_type = FieldType::from(field.field_type);
  189. let cell = match field_type {
  190. FieldType::URL => insert_url_cell(cell_data, &field),
  191. _ => {
  192. panic!("Unsupported group field type");
  193. },
  194. };
  195. let row_id = RowId::from(self.row_at_index(from_group_index, row_index).await.id);
  196. self
  197. .editor
  198. .update_cell(&self.view_id, row_id, &field_id, cell)
  199. .await
  200. .unwrap();
  201. },
  202. GroupScript::MoveGroup {
  203. from_group_index,
  204. to_group_index,
  205. } => {
  206. let from_group = self.group_at_index(from_group_index).await;
  207. let to_group = self.group_at_index(to_group_index).await;
  208. self
  209. .editor
  210. .move_group(&self.view_id, &from_group.group_id, &to_group.group_id)
  211. .await
  212. .unwrap();
  213. //
  214. },
  215. GroupScript::AssertGroup {
  216. group_index,
  217. expected_group: group_pb,
  218. } => {
  219. let group = self.group_at_index(group_index).await;
  220. assert_eq!(group.group_id, group_pb.group_id);
  221. assert_eq!(group.desc, group_pb.desc);
  222. },
  223. GroupScript::UpdateSingleSelectSelectOption { inserted_options } => {
  224. self
  225. .edit_single_select_type_option(|type_option| {
  226. for inserted_option in inserted_options {
  227. type_option.insert_option(inserted_option);
  228. }
  229. })
  230. .await;
  231. },
  232. GroupScript::GroupByField { field_id } => {
  233. self
  234. .editor
  235. .group_by_field(&self.view_id, &field_id)
  236. .await
  237. .unwrap();
  238. },
  239. }
  240. }
  241. pub async fn group_at_index(&self, index: usize) -> GroupPB {
  242. let groups = self.editor.load_groups(&self.view_id).await.unwrap().items;
  243. groups.get(index).unwrap().clone()
  244. }
  245. pub async fn row_at_index(&self, group_index: usize, row_index: usize) -> RowPB {
  246. let groups = self.group_at_index(group_index).await;
  247. groups.rows.get(row_index).unwrap().clone()
  248. }
  249. #[allow(dead_code)]
  250. pub async fn get_multi_select_field(&self) -> Field {
  251. self
  252. .inner
  253. .get_fields()
  254. .into_iter()
  255. .find(|field_rev| {
  256. let field_type = FieldType::from(field_rev.field_type);
  257. field_type.is_multi_select()
  258. })
  259. .unwrap()
  260. }
  261. pub async fn get_single_select_field(&self) -> Field {
  262. self
  263. .inner
  264. .get_fields()
  265. .into_iter()
  266. .find(|field| {
  267. let field_type = FieldType::from(field.field_type);
  268. field_type.is_single_select()
  269. })
  270. .unwrap()
  271. }
  272. pub async fn edit_single_select_type_option(
  273. &self,
  274. action: impl FnOnce(&mut SingleSelectTypeOption),
  275. ) {
  276. let single_select = self.get_single_select_field().await;
  277. edit_single_select_type_option(
  278. &self.view_id,
  279. &single_select.id,
  280. self.editor.clone(),
  281. action,
  282. )
  283. .await
  284. .unwrap();
  285. }
  286. pub async fn get_url_field(&self) -> Field {
  287. self
  288. .inner
  289. .get_fields()
  290. .into_iter()
  291. .find(|field| {
  292. let field_type = FieldType::from(field.field_type);
  293. field_type.is_url()
  294. })
  295. .unwrap()
  296. }
  297. }
  298. impl std::ops::Deref for DatabaseGroupTest {
  299. type Target = DatabaseEditorTest;
  300. fn deref(&self) -> &Self::Target {
  301. &self.inner
  302. }
  303. }
  304. impl std::ops::DerefMut for DatabaseGroupTest {
  305. fn deref_mut(&mut self) -> &mut Self::Target {
  306. &mut self.inner
  307. }
  308. }