test.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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, cell) in cells.into_iter().enumerate() {
  70. let text = StrCellData::from(cell.as_ref());
  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, cell) in cells.into_iter().enumerate() {
  91. let cell = URLCellData::from(cell.as_ref());
  92. if i == 0 {
  93. assert_eq!(cell.url.as_str(), "https://www.appflowy.io/");
  94. }
  95. }
  96. }
  97. #[tokio::test]
  98. async fn update_updated_at_field_on_other_cell_update() {
  99. let mut test = DatabaseCellTest::new().await;
  100. let updated_at_field = test.get_first_field(FieldType::LastEditedTime);
  101. let text_field = test
  102. .fields
  103. .iter()
  104. .find(|&f| FieldType::from(f.field_type) == FieldType::RichText)
  105. .unwrap();
  106. let before_update_timestamp = chrono::offset::Utc::now().timestamp();
  107. test
  108. .run_script(UpdateCell {
  109. changeset: CellChangesetPB {
  110. view_id: test.view_id.clone(),
  111. row_id: test.rows[0].id.to_string(),
  112. field_id: text_field.id.clone(),
  113. cell_changeset: "change".to_string(),
  114. },
  115. is_err: false,
  116. })
  117. .await;
  118. let after_update_timestamp = chrono::offset::Utc::now().timestamp();
  119. let cells = test
  120. .editor
  121. .get_cells_for_field(&test.view_id, &updated_at_field.id)
  122. .await;
  123. assert!(cells.len() > 0);
  124. for (i, cell) in cells.into_iter().enumerate() {
  125. let timestamp = DateCellData::from(cell.as_ref()).timestamp.unwrap();
  126. println!(
  127. "{}, bf: {}, af: {}",
  128. timestamp, before_update_timestamp, after_update_timestamp
  129. );
  130. match i {
  131. 0 => assert!(
  132. timestamp >= before_update_timestamp && timestamp <= after_update_timestamp,
  133. "{} >= {} && {} <= {}",
  134. timestamp,
  135. before_update_timestamp,
  136. timestamp,
  137. after_update_timestamp
  138. ),
  139. 1 => assert!(
  140. timestamp <= before_update_timestamp,
  141. "{} <= {}",
  142. timestamp,
  143. before_update_timestamp
  144. ),
  145. 2 => assert!(
  146. timestamp <= before_update_timestamp,
  147. "{} <= {}",
  148. timestamp,
  149. before_update_timestamp
  150. ),
  151. 3 => assert!(
  152. timestamp <= before_update_timestamp,
  153. "{} <= {}",
  154. timestamp,
  155. before_update_timestamp
  156. ),
  157. 4 => assert!(
  158. timestamp <= before_update_timestamp,
  159. "{} <= {}",
  160. timestamp,
  161. before_update_timestamp
  162. ),
  163. 5 => assert!(
  164. timestamp <= before_update_timestamp,
  165. "{} <= {}",
  166. timestamp,
  167. before_update_timestamp
  168. ),
  169. _ => {},
  170. }
  171. }
  172. }