test.rs 5.6 KB

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