test.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. use flowy_database2::entities::{CellChangesetPB, FieldType};
  2. use flowy_database2::services::cell::ToCellChangeset;
  3. use flowy_database2::services::field::{
  4. ChecklistTypeOption, MultiSelectTypeOption, SelectOptionCellChangeset, SingleSelectTypeOption,
  5. 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 => make_date_cell_string("123"),
  23. FieldType::SingleSelect => {
  24. let type_option = field
  25. .get_type_option::<SingleSelectTypeOption>(field.field_type)
  26. .unwrap();
  27. SelectOptionCellChangeset::from_insert_option_id(&type_option.options.first().unwrap().id)
  28. .to_cell_changeset_str()
  29. },
  30. FieldType::MultiSelect => {
  31. let type_option = field
  32. .get_type_option::<MultiSelectTypeOption>(field.field_type)
  33. .unwrap();
  34. SelectOptionCellChangeset::from_insert_option_id(&type_option.options.first().unwrap().id)
  35. .to_cell_changeset_str()
  36. },
  37. FieldType::Checklist => {
  38. let type_option = field
  39. .get_type_option::<ChecklistTypeOption>(field.field_type)
  40. .unwrap();
  41. SelectOptionCellChangeset::from_insert_option_id(&type_option.options.first().unwrap().id)
  42. .to_cell_changeset_str()
  43. },
  44. FieldType::Checkbox => "1".to_string(),
  45. FieldType::URL => "1".to_string(),
  46. };
  47. scripts.push(UpdateCell {
  48. changeset: CellChangesetPB {
  49. view_id: test.view_id.clone(),
  50. row_id: row.id.clone().into(),
  51. field_id: field.id.clone(),
  52. cell_changeset,
  53. },
  54. is_err: false,
  55. });
  56. }
  57. }
  58. test.run_scripts(scripts).await;
  59. }
  60. #[tokio::test]
  61. async fn text_cell_date_test() {
  62. let test = DatabaseCellTest::new().await;
  63. let text_field = test.get_first_field(FieldType::RichText);
  64. let cells = test
  65. .editor
  66. .get_cells_for_field(&test.view_id, &text_field.id)
  67. .await;
  68. for (i, cell) in cells.into_iter().enumerate() {
  69. let text = StrCellData::from(cell.as_ref());
  70. match i {
  71. 0 => assert_eq!(text.as_str(), "A"),
  72. 1 => assert_eq!(text.as_str(), ""),
  73. 2 => assert_eq!(text.as_str(), "C"),
  74. 3 => assert_eq!(text.as_str(), "DA"),
  75. 4 => assert_eq!(text.as_str(), "AE"),
  76. 5 => assert_eq!(text.as_str(), "AE"),
  77. _ => {},
  78. }
  79. }
  80. }
  81. #[tokio::test]
  82. async fn url_cell_date_test() {
  83. let test = DatabaseCellTest::new().await;
  84. let url_field = test.get_first_field(FieldType::URL);
  85. let cells = test
  86. .editor
  87. .get_cells_for_field(&test.view_id, &url_field.id)
  88. .await;
  89. for (i, cell) in cells.into_iter().enumerate() {
  90. let cell = URLCellData::from(cell.as_ref());
  91. if i == 0 {
  92. assert_eq!(cell.url.as_str(), "https://www.appflowy.io/");
  93. }
  94. }
  95. }