test.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. use std::time::Duration;
  2. use flowy_database2::entities::{CellChangesetPB, FieldType};
  3. use flowy_database2::services::cell::ToCellChangeset;
  4. use flowy_database2::services::field::checklist_type_option::ChecklistCellChangeset;
  5. use flowy_database2::services::field::{
  6. DateCellData, MultiSelectTypeOption, SelectOptionCellChangeset, SingleSelectTypeOption,
  7. StrCellData, URLCellData,
  8. };
  9. use crate::database::cell_test::script::CellScript::UpdateCell;
  10. use crate::database::cell_test::script::DatabaseCellTest;
  11. use crate::database::field_test::util::make_date_cell_string;
  12. #[tokio::test]
  13. async fn grid_cell_update() {
  14. let mut test = DatabaseCellTest::new().await;
  15. let fields = test.get_fields();
  16. let rows = &test.row_details;
  17. let mut scripts = vec![];
  18. for (_, row_detail) in rows.iter().enumerate() {
  19. for field in &fields {
  20. let field_type = FieldType::from(field.field_type);
  21. if field_type == FieldType::LastEditedTime || field_type == FieldType::CreatedTime {
  22. continue;
  23. }
  24. let cell_changeset = match field_type {
  25. FieldType::RichText => "".to_string(),
  26. FieldType::Number => "123".to_string(),
  27. FieldType::DateTime => make_date_cell_string(123),
  28. FieldType::SingleSelect => {
  29. let type_option = field
  30. .get_type_option::<SingleSelectTypeOption>(field.field_type)
  31. .unwrap();
  32. SelectOptionCellChangeset::from_insert_option_id(&type_option.options.first().unwrap().id)
  33. .to_cell_changeset_str()
  34. },
  35. FieldType::MultiSelect => {
  36. let type_option = field
  37. .get_type_option::<MultiSelectTypeOption>(field.field_type)
  38. .unwrap();
  39. SelectOptionCellChangeset::from_insert_option_id(&type_option.options.first().unwrap().id)
  40. .to_cell_changeset_str()
  41. },
  42. FieldType::Checklist => ChecklistCellChangeset {
  43. insert_options: vec!["new option".to_string()],
  44. ..Default::default()
  45. }
  46. .to_cell_changeset_str(),
  47. FieldType::Checkbox => "1".to_string(),
  48. FieldType::URL => "1".to_string(),
  49. _ => "".to_string(),
  50. };
  51. scripts.push(UpdateCell {
  52. changeset: CellChangesetPB {
  53. view_id: test.view_id.clone(),
  54. row_id: row_detail.row.id.clone().into(),
  55. field_id: field.id.clone(),
  56. cell_changeset,
  57. },
  58. is_err: false,
  59. });
  60. }
  61. }
  62. test.run_scripts(scripts).await;
  63. }
  64. #[tokio::test]
  65. async fn text_cell_data_test() {
  66. let test = DatabaseCellTest::new().await;
  67. let text_field = test.get_first_field(FieldType::RichText);
  68. let cells = test
  69. .editor
  70. .get_cells_for_field(&test.view_id, &text_field.id)
  71. .await;
  72. for (i, row_cell) in cells.into_iter().enumerate() {
  73. let text = StrCellData::from(row_cell.cell.as_ref().unwrap());
  74. match i {
  75. 0 => assert_eq!(text.as_str(), "A"),
  76. 1 => assert_eq!(text.as_str(), ""),
  77. 2 => assert_eq!(text.as_str(), "C"),
  78. 3 => assert_eq!(text.as_str(), "DA"),
  79. 4 => assert_eq!(text.as_str(), "AE"),
  80. 5 => assert_eq!(text.as_str(), "AE"),
  81. _ => {},
  82. }
  83. }
  84. }
  85. #[tokio::test]
  86. async fn url_cell_data_test() {
  87. let test = DatabaseCellTest::new().await;
  88. let url_field = test.get_first_field(FieldType::URL);
  89. let cells = test
  90. .editor
  91. .get_cells_for_field(&test.view_id, &url_field.id)
  92. .await;
  93. for (i, row_cell) in cells.into_iter().enumerate() {
  94. if let Some(cell) = row_cell.cell.as_ref() {
  95. let cell = URLCellData::from(cell);
  96. if i == 0 {
  97. assert_eq!(cell.url.as_str(), "https://www.appflowy.io/");
  98. }
  99. }
  100. }
  101. }
  102. #[tokio::test]
  103. async fn update_updated_at_field_on_other_cell_update() {
  104. let mut test = DatabaseCellTest::new().await;
  105. let updated_at_field = test.get_first_field(FieldType::LastEditedTime);
  106. let text_field = test
  107. .fields
  108. .iter()
  109. .find(|&f| FieldType::from(f.field_type) == FieldType::RichText)
  110. .unwrap();
  111. let before_update_timestamp = chrono::offset::Utc::now().timestamp();
  112. test
  113. .run_script(UpdateCell {
  114. changeset: CellChangesetPB {
  115. view_id: test.view_id.clone(),
  116. row_id: test.row_details[0].row.id.to_string(),
  117. field_id: text_field.id.clone(),
  118. cell_changeset: "change".to_string(),
  119. },
  120. is_err: false,
  121. })
  122. .await;
  123. let cells = test
  124. .editor
  125. .get_cells_for_field(&test.view_id, &updated_at_field.id)
  126. .await;
  127. tokio::time::sleep(Duration::from_millis(500)).await;
  128. let after_update_timestamp = chrono::offset::Utc::now().timestamp();
  129. assert!(!cells.is_empty());
  130. for (i, row_cell) in cells.into_iter().enumerate() {
  131. let timestamp = DateCellData::from(row_cell.cell.as_ref().unwrap())
  132. .timestamp
  133. .unwrap();
  134. println!(
  135. "{}, bf: {}, af: {}",
  136. timestamp, before_update_timestamp, after_update_timestamp
  137. );
  138. match i {
  139. 0 => assert!(
  140. timestamp >= before_update_timestamp && timestamp <= after_update_timestamp,
  141. "{} >= {} && {} <= {}",
  142. timestamp,
  143. before_update_timestamp,
  144. timestamp,
  145. after_update_timestamp
  146. ),
  147. 1 => assert!(
  148. timestamp <= before_update_timestamp,
  149. "{} <= {}",
  150. timestamp,
  151. before_update_timestamp
  152. ),
  153. 2 => assert!(
  154. timestamp <= before_update_timestamp,
  155. "{} <= {}",
  156. timestamp,
  157. before_update_timestamp
  158. ),
  159. 3 => assert!(
  160. timestamp <= before_update_timestamp,
  161. "{} <= {}",
  162. timestamp,
  163. before_update_timestamp
  164. ),
  165. 4 => assert!(
  166. timestamp <= before_update_timestamp,
  167. "{} <= {}",
  168. timestamp,
  169. before_update_timestamp
  170. ),
  171. 5 => assert!(
  172. timestamp <= before_update_timestamp,
  173. "{} <= {}",
  174. timestamp,
  175. before_update_timestamp
  176. ),
  177. _ => {},
  178. }
  179. }
  180. }