2022-06-23 15:06:03 +00:00
|
|
|
use crate::grid::field_util::*;
|
|
|
|
use crate::grid::row_util::GridRowTestBuilder;
|
2022-06-20 02:24:43 +00:00
|
|
|
use crate::grid::script::EditorScript::*;
|
|
|
|
use crate::grid::script::*;
|
|
|
|
use chrono::NaiveDateTime;
|
|
|
|
use flowy_grid::services::field::{
|
|
|
|
DateCellData, MultiSelectTypeOption, SingleSelectTypeOption, SELECTION_IDS_SEPARATOR,
|
|
|
|
};
|
2022-06-24 10:13:40 +00:00
|
|
|
use flowy_grid::services::row::{decode_cell_data, CreateRowRevisionBuilder};
|
2022-06-20 02:24:43 +00:00
|
|
|
use flowy_grid_data_model::entities::FieldType;
|
2022-06-24 10:13:40 +00:00
|
|
|
use flowy_grid_data_model::revision::RowMetaChangeset;
|
2022-06-20 02:24:43 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn grid_create_row_count_test() {
|
|
|
|
let test = GridEditorTest::new().await;
|
|
|
|
let scripts = vec![
|
|
|
|
AssertRowCount(3),
|
|
|
|
CreateEmptyRow,
|
|
|
|
CreateEmptyRow,
|
|
|
|
CreateRow {
|
2022-06-23 15:06:03 +00:00
|
|
|
payload: GridRowTestBuilder::new(&test).build(),
|
2022-06-20 02:24:43 +00:00
|
|
|
},
|
|
|
|
AssertRowCount(6),
|
|
|
|
];
|
|
|
|
GridEditorTest::new().await.run_scripts(scripts).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn grid_update_row() {
|
|
|
|
let mut test = GridEditorTest::new().await;
|
2022-06-23 15:06:03 +00:00
|
|
|
let payload = GridRowTestBuilder::new(&test).build();
|
2022-06-20 02:24:43 +00:00
|
|
|
let changeset = RowMetaChangeset {
|
2022-06-23 15:06:03 +00:00
|
|
|
row_id: payload.row_id.clone(),
|
2022-06-20 02:24:43 +00:00
|
|
|
height: None,
|
|
|
|
visibility: None,
|
|
|
|
cell_by_field_id: Default::default(),
|
|
|
|
};
|
|
|
|
|
2022-06-23 15:06:03 +00:00
|
|
|
let scripts = vec![AssertRowCount(3), CreateRow { payload }, UpdateRow { changeset }];
|
2022-06-20 02:24:43 +00:00
|
|
|
test.run_scripts(scripts).await;
|
|
|
|
|
|
|
|
let expected_row = (&*test.row_revs.last().cloned().unwrap()).clone();
|
|
|
|
let scripts = vec![AssertRow { expected_row }, AssertRowCount(4)];
|
|
|
|
test.run_scripts(scripts).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn grid_delete_row() {
|
|
|
|
let mut test = GridEditorTest::new().await;
|
2022-06-23 15:06:03 +00:00
|
|
|
let payload1 = GridRowTestBuilder::new(&test).build();
|
|
|
|
let payload2 = GridRowTestBuilder::new(&test).build();
|
|
|
|
let row_ids = vec![payload1.row_id.clone(), payload2.row_id.clone()];
|
2022-06-20 02:24:43 +00:00
|
|
|
let scripts = vec![
|
|
|
|
AssertRowCount(3),
|
2022-06-23 15:06:03 +00:00
|
|
|
CreateRow { payload: payload1 },
|
|
|
|
CreateRow { payload: payload2 },
|
2022-06-20 02:24:43 +00:00
|
|
|
AssertBlockCount(1),
|
|
|
|
AssertBlock {
|
|
|
|
block_index: 0,
|
|
|
|
row_count: 5,
|
|
|
|
start_row_index: 0,
|
|
|
|
},
|
|
|
|
DeleteRows { row_ids },
|
|
|
|
AssertBlock {
|
|
|
|
block_index: 0,
|
|
|
|
row_count: 3,
|
|
|
|
start_row_index: 0,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
test.run_scripts(scripts).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn grid_row_add_cells_test() {
|
|
|
|
let mut test = GridEditorTest::new().await;
|
|
|
|
let mut builder = CreateRowRevisionBuilder::new(&test.field_revs);
|
|
|
|
for field in &test.field_revs {
|
|
|
|
match field.field_type {
|
|
|
|
FieldType::RichText => {
|
|
|
|
builder.add_cell(&field.id, "hello world".to_owned()).unwrap();
|
|
|
|
}
|
|
|
|
FieldType::Number => {
|
|
|
|
builder.add_cell(&field.id, "18,443".to_owned()).unwrap();
|
|
|
|
}
|
|
|
|
FieldType::DateTime => {
|
|
|
|
builder
|
|
|
|
.add_cell(&field.id, make_date_cell_string("1647251762"))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
FieldType::SingleSelect => {
|
|
|
|
let type_option = SingleSelectTypeOption::from(field);
|
|
|
|
let option = type_option.options.first().unwrap();
|
|
|
|
builder.add_select_option_cell(&field.id, option.id.clone()).unwrap();
|
|
|
|
}
|
|
|
|
FieldType::MultiSelect => {
|
|
|
|
let type_option = MultiSelectTypeOption::from(field);
|
|
|
|
let ops_ids = type_option
|
|
|
|
.options
|
|
|
|
.iter()
|
|
|
|
.map(|option| option.id.clone())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(SELECTION_IDS_SEPARATOR);
|
|
|
|
builder.add_select_option_cell(&field.id, ops_ids).unwrap();
|
|
|
|
}
|
|
|
|
FieldType::Checkbox => {
|
|
|
|
builder.add_cell(&field.id, "false".to_string()).unwrap();
|
|
|
|
}
|
|
|
|
FieldType::URL => {
|
|
|
|
builder.add_cell(&field.id, "1".to_string()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let context = builder.build();
|
2022-06-23 15:06:03 +00:00
|
|
|
let scripts = vec![CreateRow { payload: context }, AssertGridRevisionPad];
|
2022-06-20 02:24:43 +00:00
|
|
|
test.run_scripts(scripts).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn grid_row_add_date_cell_test() {
|
|
|
|
let mut test = GridEditorTest::new().await;
|
|
|
|
let mut builder = CreateRowRevisionBuilder::new(&test.field_revs);
|
|
|
|
let mut date_field = None;
|
|
|
|
let timestamp = 1647390674;
|
|
|
|
for field in &test.field_revs {
|
|
|
|
if field.field_type == FieldType::DateTime {
|
|
|
|
date_field = Some(field.clone());
|
|
|
|
NaiveDateTime::from_timestamp(123, 0);
|
|
|
|
// The data should not be empty
|
|
|
|
assert!(builder.add_cell(&field.id, "".to_string()).is_err());
|
|
|
|
assert!(builder.add_cell(&field.id, make_date_cell_string("123")).is_ok());
|
|
|
|
assert!(builder
|
|
|
|
.add_cell(&field.id, make_date_cell_string(×tamp.to_string()))
|
|
|
|
.is_ok());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let context = builder.build();
|
|
|
|
let date_field = date_field.unwrap();
|
|
|
|
let cell_data = context.cell_by_field_id.get(&date_field.id).unwrap().clone();
|
|
|
|
assert_eq!(
|
2022-06-24 10:13:40 +00:00
|
|
|
decode_cell_data(cell_data.data.clone(), &date_field)
|
2022-06-20 02:24:43 +00:00
|
|
|
.parse::<DateCellData>()
|
|
|
|
.unwrap()
|
|
|
|
.date,
|
|
|
|
"2022/03/16",
|
|
|
|
);
|
2022-06-23 15:06:03 +00:00
|
|
|
let scripts = vec![CreateRow { payload: context }];
|
2022-06-20 02:24:43 +00:00
|
|
|
test.run_scripts(scripts).await;
|
|
|
|
}
|