script.rs 9.6 KB

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