test.rs 5.7 KB

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