test.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. use flowy_database2::entities::{CellChangesetPB, FieldType};
  2. use flowy_database2::services::cell::ToCellChangeset;
  3. use flowy_database2::services::field::{
  4. ChecklistTypeOption, DateCellData, MultiSelectTypeOption, SelectOptionCellChangeset,
  5. SingleSelectTypeOption, StrCellData, URLCellData,
  6. };
  7. use crate::database::cell_test::script::CellScript::UpdateCell;
  8. use crate::database::cell_test::script::DatabaseCellTest;
  9. use crate::database::field_test::util::make_date_cell_string;
  10. #[tokio::test]
  11. async fn grid_cell_update() {
  12. let mut test = DatabaseCellTest::new().await;
  13. let fields = test.get_fields();
  14. let rows = &test.rows;
  15. let mut scripts = vec![];
  16. for (_, row) in rows.iter().enumerate() {
  17. for field in &fields {
  18. let field_type = FieldType::from(field.field_type);
  19. let cell_changeset = match field_type {
  20. FieldType::RichText => "".to_string(),
  21. FieldType::Number => "123".to_string(),
  22. FieldType::DateTime | FieldType::UpdatedAt | FieldType::CreatedAt => {
  23. make_date_cell_string("123")
  24. },
  25. FieldType::SingleSelect => {
  26. let type_option = field
  27. .get_type_option::<SingleSelectTypeOption>(field.field_type)
  28. .unwrap();
  29. SelectOptionCellChangeset::from_insert_option_id(&type_option.options.first().unwrap().id)
  30. .to_cell_changeset_str()
  31. },
  32. FieldType::MultiSelect => {
  33. let type_option = field
  34. .get_type_option::<MultiSelectTypeOption>(field.field_type)
  35. .unwrap();
  36. SelectOptionCellChangeset::from_insert_option_id(&type_option.options.first().unwrap().id)
  37. .to_cell_changeset_str()
  38. },
  39. FieldType::Checklist => {
  40. let type_option = field
  41. .get_type_option::<ChecklistTypeOption>(field.field_type)
  42. .unwrap();
  43. SelectOptionCellChangeset::from_insert_option_id(&type_option.options.first().unwrap().id)
  44. .to_cell_changeset_str()
  45. },
  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, cell) in cells.into_iter().enumerate() {
  71. let text = StrCellData::from(cell.as_ref());
  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, cell) in cells.into_iter().enumerate() {
  92. let cell = URLCellData::from(cell.as_ref());
  93. if i == 0 {
  94. assert_eq!(cell.url.as_str(), "https://www.appflowy.io/");
  95. }
  96. }
  97. }
  98. #[tokio::test]
  99. async fn update_updated_at_field_on_other_cell_update() {
  100. let mut test = DatabaseCellTest::new().await;
  101. let updated_at_field = test.get_first_field(FieldType::UpdatedAt);
  102. let text_field = test
  103. .fields
  104. .iter()
  105. .find(|&f| FieldType::from(f.field_type) == FieldType::RichText)
  106. .unwrap();
  107. let before_update_timestamp = chrono::offset::Utc::now().timestamp();
  108. test
  109. .run_script(UpdateCell {
  110. changeset: CellChangesetPB {
  111. view_id: test.view_id.clone(),
  112. row_id: test.rows[0].id.to_string(),
  113. field_id: text_field.id.clone(),
  114. cell_changeset: "change".to_string(),
  115. },
  116. is_err: false,
  117. })
  118. .await;
  119. let after_update_timestamp = chrono::offset::Utc::now().timestamp();
  120. let cells = test
  121. .editor
  122. .get_cells_for_field(&test.view_id, &updated_at_field.id)
  123. .await;
  124. assert!(cells.len() > 0);
  125. for (i, cell) in cells.into_iter().enumerate() {
  126. let timestamp = DateCellData::from(cell.as_ref()).timestamp.unwrap();
  127. println!(
  128. "{}, bf: {}, af: {}",
  129. timestamp, before_update_timestamp, after_update_timestamp
  130. );
  131. match i {
  132. 0 => assert!(
  133. timestamp >= before_update_timestamp && timestamp <= after_update_timestamp,
  134. "{} >= {} && {} <= {}",
  135. timestamp,
  136. before_update_timestamp,
  137. timestamp,
  138. after_update_timestamp
  139. ),
  140. 1 => assert!(
  141. timestamp <= before_update_timestamp,
  142. "{} <= {}",
  143. timestamp,
  144. before_update_timestamp
  145. ),
  146. 2 => assert!(
  147. timestamp <= before_update_timestamp,
  148. "{} <= {}",
  149. timestamp,
  150. before_update_timestamp
  151. ),
  152. 3 => assert!(
  153. timestamp <= before_update_timestamp,
  154. "{} <= {}",
  155. timestamp,
  156. before_update_timestamp
  157. ),
  158. 4 => assert!(
  159. timestamp <= before_update_timestamp,
  160. "{} <= {}",
  161. timestamp,
  162. before_update_timestamp
  163. ),
  164. 5 => assert!(
  165. timestamp <= before_update_timestamp,
  166. "{} <= {}",
  167. timestamp,
  168. before_update_timestamp
  169. ),
  170. _ => {},
  171. }
  172. }
  173. }