script.rs 9.2 KB

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