row_test.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. use std::time::Duration;
  2. use flowy_database2::entities::FieldType;
  3. use flowy_database2::services::field::DateCellData;
  4. use lib_infra::util::timestamp;
  5. use crate::database::block_test::script::DatabaseRowTest;
  6. use crate::database::block_test::script::RowScript::*;
  7. // Create a new row at the end of the grid and check the create time is valid.
  8. #[tokio::test]
  9. async fn created_at_field_test() {
  10. let mut test = DatabaseRowTest::new().await;
  11. let row_count = test.rows.len();
  12. test
  13. .run_scripts(vec![CreateEmptyRow, AssertRowCount(row_count + 1)])
  14. .await;
  15. // Get created time of the new row.
  16. let row = test.get_rows().await.last().cloned().unwrap();
  17. let updated_at_field = test.get_first_field(FieldType::CreatedTime);
  18. let cell = test
  19. .editor
  20. .get_cell(&updated_at_field.id, &row.id)
  21. .await
  22. .unwrap();
  23. let created_at_timestamp = DateCellData::from(&cell).timestamp.unwrap();
  24. assert!(created_at_timestamp > 0);
  25. assert!(created_at_timestamp <= timestamp());
  26. }
  27. // Update row and check the update time is valid.
  28. #[tokio::test]
  29. async fn update_at_field_test() {
  30. let mut test = DatabaseRowTest::new().await;
  31. let row = test.get_rows().await.remove(0);
  32. let last_edit_field = test.get_first_field(FieldType::LastEditedTime);
  33. let cell = test
  34. .editor
  35. .get_cell(&last_edit_field.id, &row.id)
  36. .await
  37. .unwrap();
  38. let old_updated_at = DateCellData::from(&cell).timestamp.unwrap();
  39. tokio::time::sleep(Duration::from_millis(500)).await;
  40. test
  41. .run_script(UpdateTextCell {
  42. row_id: row.id.clone(),
  43. content: "test".to_string(),
  44. })
  45. .await;
  46. // Get the updated time of the row.
  47. let row = test.get_rows().await.remove(0);
  48. let last_edit_field = test.get_first_field(FieldType::LastEditedTime);
  49. let cell = test
  50. .editor
  51. .get_cell(&last_edit_field.id, &row.id)
  52. .await
  53. .unwrap();
  54. let new_updated_at = DateCellData::from(&cell).timestamp.unwrap();
  55. assert!(old_updated_at < new_updated_at);
  56. }