test.rs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. use collab_database::database::gen_option_id;
  2. use flowy_database2::entities::{FieldChangesetParams, FieldType};
  3. use flowy_database2::services::field::{SelectOption, CHECK, UNCHECK};
  4. use crate::database::field_test::script::DatabaseFieldTest;
  5. use crate::database::field_test::script::FieldScript::*;
  6. use crate::database::field_test::util::*;
  7. #[tokio::test]
  8. async fn grid_create_field() {
  9. let mut test = DatabaseFieldTest::new().await;
  10. let (params, field) = create_text_field(&test.view_id());
  11. let scripts = vec![
  12. CreateField { params },
  13. AssertFieldTypeOptionEqual {
  14. field_index: test.field_count(),
  15. expected_type_option_data: field.get_any_type_option(field.field_type).unwrap(),
  16. },
  17. ];
  18. test.run_scripts(scripts).await;
  19. let (params, field) = create_single_select_field(&test.view_id());
  20. let scripts = vec![
  21. CreateField { params },
  22. AssertFieldTypeOptionEqual {
  23. field_index: test.field_count(),
  24. expected_type_option_data: field.get_any_type_option(field.field_type).unwrap(),
  25. },
  26. ];
  27. test.run_scripts(scripts).await;
  28. }
  29. #[tokio::test]
  30. async fn grid_create_duplicate_field() {
  31. let mut test = DatabaseFieldTest::new().await;
  32. let (params, _) = create_text_field(&test.view_id());
  33. let field_count = test.field_count();
  34. let expected_field_count = field_count + 1;
  35. let scripts = vec![
  36. CreateField {
  37. params: params.clone(),
  38. },
  39. AssertFieldCount(expected_field_count),
  40. ];
  41. test.run_scripts(scripts).await;
  42. }
  43. #[tokio::test]
  44. async fn grid_update_field_with_empty_change() {
  45. let mut test = DatabaseFieldTest::new().await;
  46. let (params, _) = create_single_select_field(&test.view_id());
  47. let create_field_index = test.field_count();
  48. let scripts = vec![CreateField { params }];
  49. test.run_scripts(scripts).await;
  50. let field = test.get_fields().pop().unwrap().clone();
  51. let changeset = FieldChangesetParams {
  52. field_id: field.id.clone(),
  53. view_id: test.view_id(),
  54. ..Default::default()
  55. };
  56. let scripts = vec![
  57. UpdateField { changeset },
  58. AssertFieldTypeOptionEqual {
  59. field_index: create_field_index,
  60. expected_type_option_data: field.get_any_type_option(field.field_type).unwrap(),
  61. },
  62. ];
  63. test.run_scripts(scripts).await;
  64. }
  65. #[tokio::test]
  66. async fn grid_delete_field() {
  67. let mut test = DatabaseFieldTest::new().await;
  68. let original_field_count = test.field_count();
  69. let (params, _) = create_text_field(&test.view_id());
  70. let scripts = vec![CreateField { params }];
  71. test.run_scripts(scripts).await;
  72. let field = test.get_fields().pop().unwrap();
  73. let scripts = vec![
  74. DeleteField { field },
  75. AssertFieldCount(original_field_count),
  76. ];
  77. test.run_scripts(scripts).await;
  78. }
  79. #[tokio::test]
  80. async fn grid_switch_from_select_option_to_checkbox_test() {
  81. let mut test = DatabaseFieldTest::new().await;
  82. let field = test.get_first_field(FieldType::SingleSelect);
  83. // Update the type option data of single select option
  84. let mut single_select_type_option = test.get_single_select_type_option(&field.id);
  85. single_select_type_option.options.clear();
  86. // Add a new option with name CHECK
  87. single_select_type_option.options.push(SelectOption {
  88. id: gen_option_id(),
  89. name: CHECK.to_string(),
  90. color: Default::default(),
  91. });
  92. // Add a new option with name UNCHECK
  93. single_select_type_option.options.push(SelectOption {
  94. id: gen_option_id(),
  95. name: UNCHECK.to_string(),
  96. color: Default::default(),
  97. });
  98. let scripts = vec![
  99. UpdateTypeOption {
  100. field_id: field.id.clone(),
  101. type_option: single_select_type_option.into(),
  102. },
  103. SwitchToField {
  104. field_id: field.id.clone(),
  105. new_field_type: FieldType::Checkbox,
  106. },
  107. ];
  108. test.run_scripts(scripts).await;
  109. }
  110. #[tokio::test]
  111. async fn grid_switch_from_checkbox_to_select_option_test() {
  112. let mut test = DatabaseFieldTest::new().await;
  113. let checkbox_field = test.get_first_field(FieldType::Checkbox).clone();
  114. let scripts = vec![
  115. // switch to single-select field type
  116. SwitchToField {
  117. field_id: checkbox_field.id.clone(),
  118. new_field_type: FieldType::SingleSelect,
  119. },
  120. // Assert the cell content after switch the field type. The cell content will be changed if
  121. // the FieldType::SingleSelect implement the cell data TypeOptionTransform. Check out the
  122. // TypeOptionTransform trait for more information.
  123. //
  124. // Make sure which cell of the row you want to check.
  125. AssertCellContent {
  126. field_id: checkbox_field.id.clone(),
  127. // the mock data of the checkbox with row_index one is "true"
  128. row_index: 1,
  129. // the from_field_type represents as the current field type
  130. from_field_type: FieldType::Checkbox,
  131. // The content of the checkbox should transform to the corresponding option name.
  132. expected_content: CHECK.to_string(),
  133. },
  134. ];
  135. test.run_scripts(scripts).await;
  136. let single_select_type_option = test.get_single_select_type_option(&checkbox_field.id);
  137. assert_eq!(single_select_type_option.options.len(), 2);
  138. assert!(single_select_type_option
  139. .options
  140. .iter()
  141. .any(|option| option.name == UNCHECK));
  142. assert!(single_select_type_option
  143. .options
  144. .iter()
  145. .any(|option| option.name == CHECK));
  146. }
  147. // Test when switching the current field from Multi-select to Text test
  148. // The build-in test data is located in `make_test_grid` method(flowy-database/tests/grid_editor.rs).
  149. // input:
  150. // option1, option2 -> "option1.name, option2.name"
  151. #[tokio::test]
  152. async fn grid_switch_from_multi_select_to_text_test() {
  153. let mut test = DatabaseFieldTest::new().await;
  154. let field_rev = test.get_first_field(FieldType::MultiSelect).clone();
  155. let multi_select_type_option = test.get_multi_select_type_option(&field_rev.id);
  156. let script_switch_field = vec![SwitchToField {
  157. field_id: field_rev.id.clone(),
  158. new_field_type: FieldType::RichText,
  159. }];
  160. test.run_scripts(script_switch_field).await;
  161. let script_assert_field = vec![AssertCellContent {
  162. field_id: field_rev.id.clone(),
  163. row_index: 0,
  164. from_field_type: FieldType::MultiSelect,
  165. expected_content: format!(
  166. "{},{}",
  167. multi_select_type_option.get(0).unwrap().name,
  168. multi_select_type_option.get(1).unwrap().name
  169. ),
  170. }];
  171. test.run_scripts(script_assert_field).await;
  172. }
  173. // Test when switching the current field from Checkbox to Text test
  174. // input:
  175. // check -> "Yes"
  176. // unchecked -> ""
  177. #[tokio::test]
  178. async fn grid_switch_from_checkbox_to_text_test() {
  179. let mut test = DatabaseFieldTest::new().await;
  180. let field_rev = test.get_first_field(FieldType::Checkbox);
  181. let scripts = vec![
  182. SwitchToField {
  183. field_id: field_rev.id.clone(),
  184. new_field_type: FieldType::RichText,
  185. },
  186. AssertCellContent {
  187. field_id: field_rev.id.clone(),
  188. row_index: 1,
  189. from_field_type: FieldType::Checkbox,
  190. expected_content: "Yes".to_string(),
  191. },
  192. AssertCellContent {
  193. field_id: field_rev.id.clone(),
  194. row_index: 2,
  195. from_field_type: FieldType::Checkbox,
  196. expected_content: "No".to_string(),
  197. },
  198. ];
  199. test.run_scripts(scripts).await;
  200. }
  201. // Test when switching the current field from Checkbox to Text test
  202. // input:
  203. // "Yes" -> check
  204. // "" -> unchecked
  205. #[tokio::test]
  206. async fn grid_switch_from_text_to_checkbox_test() {
  207. let mut test = DatabaseFieldTest::new().await;
  208. let field = test.get_first_field(FieldType::RichText).clone();
  209. let scripts = vec![
  210. SwitchToField {
  211. field_id: field.id.clone(),
  212. new_field_type: FieldType::Checkbox,
  213. },
  214. AssertCellContent {
  215. field_id: field.id.clone(),
  216. row_index: 0,
  217. from_field_type: FieldType::RichText,
  218. expected_content: "".to_string(),
  219. },
  220. ];
  221. test.run_scripts(scripts).await;
  222. }
  223. // Test when switching the current field from Date to Text test
  224. // input:
  225. // 1647251762 -> Mar 14,2022 (This string will be different base on current data setting)
  226. #[tokio::test]
  227. async fn grid_switch_from_date_to_text_test() {
  228. let mut test = DatabaseFieldTest::new().await;
  229. let field = test.get_first_field(FieldType::DateTime).clone();
  230. let scripts = vec![
  231. SwitchToField {
  232. field_id: field.id.clone(),
  233. new_field_type: FieldType::RichText,
  234. },
  235. AssertCellContent {
  236. field_id: field.id.clone(),
  237. row_index: 2,
  238. from_field_type: FieldType::DateTime,
  239. expected_content: "2022/03/14".to_string(),
  240. },
  241. AssertCellContent {
  242. field_id: field.id.clone(),
  243. row_index: 3,
  244. from_field_type: FieldType::DateTime,
  245. expected_content: "2022/11/17".to_string(),
  246. },
  247. ];
  248. test.run_scripts(scripts).await;
  249. }
  250. // Test when switching the current field from Number to Text test
  251. // input:
  252. // $1 -> "$1"(This string will be different base on current data setting)
  253. #[tokio::test]
  254. async fn grid_switch_from_number_to_text_test() {
  255. let mut test = DatabaseFieldTest::new().await;
  256. let field = test.get_first_field(FieldType::Number).clone();
  257. let scripts = vec![
  258. SwitchToField {
  259. field_id: field.id.clone(),
  260. new_field_type: FieldType::RichText,
  261. },
  262. AssertCellContent {
  263. field_id: field.id.clone(),
  264. row_index: 0,
  265. from_field_type: FieldType::Number,
  266. expected_content: "$1".to_string(),
  267. },
  268. AssertCellContent {
  269. field_id: field.id.clone(),
  270. row_index: 4,
  271. from_field_type: FieldType::Number,
  272. expected_content: "".to_string(),
  273. },
  274. ];
  275. test.run_scripts(scripts).await;
  276. }