AppFlowy/frontend/rust-lib/flowy-grid/tests/grid/script.rs

368 lines
14 KiB
Rust
Raw Normal View History

2022-06-29 08:55:52 +00:00
#![cfg_attr(rustfmt, rustfmt::skip)]
2022-03-15 11:00:28 +00:00
use bytes::Bytes;
2022-03-12 13:06:15 +00:00
use flowy_grid::services::field::*;
2022-06-20 01:37:52 +00:00
use flowy_grid::services::grid_editor::{GridPadBuilder, GridRevisionEditor};
use flowy_grid::services::row::CreateRowRevisionPayload;
2022-06-22 09:11:56 +00:00
use flowy_grid::services::setting::GridSettingChangesetBuilder;
use flowy_grid_data_model::entities::*;
use flowy_grid_data_model::revision::*;
2022-03-19 08:52:28 +00:00
use flowy_revision::REVISION_WRITE_INTERVAL_IN_MILLIS;
use flowy_sync::client_grid::GridBuilder;
2022-03-12 13:06:15 +00:00
use flowy_test::helper::ViewTest;
use flowy_test::FlowySDKTest;
use std::collections::HashMap;
2022-03-12 13:06:15 +00:00
use std::sync::Arc;
2022-03-12 14:52:24 +00:00
use std::time::Duration;
2022-03-16 02:02:37 +00:00
use strum::EnumCount;
2022-03-12 14:52:24 +00:00
use tokio::time::sleep;
2022-03-12 13:06:15 +00:00
pub enum EditorScript {
2022-03-14 09:24:25 +00:00
CreateField {
params: InsertFieldParams,
2022-03-14 09:24:25 +00:00
},
UpdateField {
2022-04-01 14:49:26 +00:00
changeset: FieldChangesetParams,
2022-03-14 09:24:25 +00:00
},
DeleteField {
field_rev: FieldRevision,
2022-03-14 09:24:25 +00:00
},
2022-03-12 14:52:24 +00:00
AssertFieldCount(usize),
2022-03-14 09:24:25 +00:00
AssertFieldEqual {
field_index: usize,
field_rev: FieldRevision,
2022-03-14 09:24:25 +00:00
},
CreateBlock {
2022-06-26 07:14:24 +00:00
block: GridBlockMetaRevision,
2022-03-14 09:24:25 +00:00
},
UpdateBlock {
2022-06-26 07:14:24 +00:00
changeset: GridBlockMetaRevisionChangeset,
2022-03-14 09:24:25 +00:00
},
2022-03-13 03:06:28 +00:00
AssertBlockCount(usize),
2022-03-14 09:24:25 +00:00
AssertBlock {
block_index: usize,
row_count: i32,
start_row_index: i32,
},
AssertBlockEqual {
block_index: usize,
2022-06-26 07:14:24 +00:00
block: GridBlockMetaRevision,
2022-03-14 09:24:25 +00:00
},
CreateEmptyRow,
CreateRow {
2022-06-23 15:06:03 +00:00
payload: CreateRowRevisionPayload,
2022-03-14 09:24:25 +00:00
},
UpdateRow {
changeset: RowMetaChangeset,
},
AssertRow {
expected_row: RowRevision,
2022-03-14 09:24:25 +00:00
},
DeleteRows {
2022-03-14 09:24:25 +00:00
row_ids: Vec<String>,
},
UpdateCell {
changeset: CellChangeset,
2022-03-16 02:02:37 +00:00
is_err: bool,
2022-03-14 09:24:25 +00:00
},
2022-03-13 03:06:28 +00:00
AssertRowCount(usize),
2022-06-29 08:55:52 +00:00
#[allow(dead_code)]
UpdateGridSetting {
params: GridSettingChangesetParams,
},
2022-06-22 09:11:56 +00:00
InsertGridTableFilter {
payload: CreateGridFilterPayload,
},
AssertTableFilterCount {
count: i32,
},
DeleteGridTableFilter {
filter_id: String,
},
2022-06-29 08:55:52 +00:00
#[allow(dead_code)]
AssertGridSetting {
2022-06-22 09:11:56 +00:00
expected_setting: GridSetting,
},
AssertGridRevisionPad,
2022-03-12 13:06:15 +00:00
}
pub struct GridEditorTest {
pub sdk: FlowySDKTest,
pub grid_id: String,
2022-06-20 01:37:52 +00:00
pub editor: Arc<GridRevisionEditor>,
pub field_revs: Vec<FieldRevision>,
2022-06-29 05:44:15 +00:00
pub block_meta_revs: Vec<Arc<GridBlockMetaRevision>>,
pub row_revs: Vec<Arc<RowRevision>>,
2022-03-15 11:00:28 +00:00
pub field_count: usize,
2022-03-18 13:04:01 +00:00
pub row_order_by_row_id: HashMap<String, RowOrder>,
2022-03-12 13:06:15 +00:00
}
impl GridEditorTest {
pub async fn new() -> Self {
let sdk = FlowySDKTest::default();
let _ = sdk.init_user().await;
2022-06-23 15:06:03 +00:00
let build_context = make_all_field_test_grid();
let view_data: Bytes = build_context.into();
2022-03-15 11:00:28 +00:00
let test = ViewTest::new_grid_view(&sdk, view_data.to_vec()).await;
2022-03-12 13:06:15 +00:00
let editor = sdk.grid_manager.open_grid(&test.view.id).await.unwrap();
let field_revs = editor.get_field_revs::<FieldOrder>(None).await.unwrap();
2022-06-29 05:44:15 +00:00
let block_meta_revs = editor.get_block_meta_revs().await.unwrap();
let row_revs = editor.grid_block_snapshots(None).await.unwrap().pop().unwrap().row_revs;
assert_eq!(row_revs.len(), 3);
2022-06-29 05:44:15 +00:00
assert_eq!(block_meta_revs.len(), 1);
// It seems like you should add the field in the make_test_grid() function.
// Because we assert the initialize count of the fields is equal to FieldType::COUNT.
assert_eq!(field_revs.len(), FieldType::COUNT);
2022-03-14 09:24:25 +00:00
2022-03-12 13:06:15 +00:00
let grid_id = test.view.id;
2022-03-14 09:24:25 +00:00
Self {
sdk,
grid_id,
editor,
2022-06-20 01:37:52 +00:00
field_revs,
2022-06-29 05:44:15 +00:00
block_meta_revs,
2022-06-20 01:37:52 +00:00
row_revs,
2022-03-15 11:00:28 +00:00
field_count: FieldType::COUNT,
2022-03-18 13:04:01 +00:00
row_order_by_row_id: HashMap::default(),
2022-03-14 09:24:25 +00:00
}
2022-03-12 13:06:15 +00:00
}
pub async fn run_scripts(&mut self, scripts: Vec<EditorScript>) {
for script in scripts {
self.run_script(script).await;
}
}
pub async fn run_script(&mut self, script: EditorScript) {
let grid_manager = self.sdk.grid_manager.clone();
let pool = self.sdk.user_session.db_pool().unwrap();
let rev_manager = self.editor.rev_manager();
2022-03-14 15:16:25 +00:00
let _cache = rev_manager.revision_cache().await;
2022-03-12 13:06:15 +00:00
match script {
EditorScript::CreateField { params } => {
if !self.editor.contain_field(&params.field.id).await {
2022-03-15 11:00:28 +00:00
self.field_count += 1;
}
self.editor.insert_field(params).await.unwrap();
self.field_revs = self.editor.get_field_revs::<FieldOrder>(None).await.unwrap();
assert_eq!(self.field_count, self.field_revs.len());
2022-03-12 13:06:15 +00:00
}
2022-03-14 09:24:25 +00:00
EditorScript::UpdateField { changeset: change } => {
2022-03-12 14:52:24 +00:00
self.editor.update_field(change).await.unwrap();
self.field_revs = self.editor.get_field_revs::<FieldOrder>(None).await.unwrap();
2022-03-12 14:52:24 +00:00
}
EditorScript::DeleteField { field_rev } => {
if self.editor.contain_field(&field_rev.id).await {
2022-03-15 11:00:28 +00:00
self.field_count -= 1;
}
self.editor.delete_field(&field_rev.id).await.unwrap();
self.field_revs = self.editor.get_field_revs::<FieldOrder>(None).await.unwrap();
assert_eq!(self.field_count, self.field_revs.len());
2022-03-13 03:06:28 +00:00
}
2022-03-12 14:52:24 +00:00
EditorScript::AssertFieldCount(count) => {
2022-04-07 07:34:00 +00:00
assert_eq!(
self.editor.get_field_revs::<FieldOrder>(None).await.unwrap().len(),
2022-04-07 07:34:00 +00:00
count
);
2022-03-12 14:52:24 +00:00
}
EditorScript::AssertFieldEqual { field_index, field_rev } => {
let field_revs = self.editor.get_field_revs::<FieldOrder>(None).await.unwrap();
assert_eq!(field_revs[field_index].clone(), field_rev);
2022-03-12 14:52:24 +00:00
}
2022-03-13 03:06:28 +00:00
EditorScript::CreateBlock { block } => {
self.editor.create_block(block).await.unwrap();
2022-06-29 05:44:15 +00:00
self.block_meta_revs = self.editor.get_block_meta_revs().await.unwrap();
2022-03-13 03:06:28 +00:00
}
2022-03-14 09:24:25 +00:00
EditorScript::UpdateBlock { changeset: change } => {
2022-03-13 03:06:28 +00:00
self.editor.update_block(change).await.unwrap();
}
EditorScript::AssertBlockCount(count) => {
2022-06-26 07:14:24 +00:00
assert_eq!(self.editor.get_block_meta_revs().await.unwrap().len(), count);
2022-03-13 03:06:28 +00:00
}
2022-03-14 09:24:25 +00:00
EditorScript::AssertBlock {
block_index,
row_count,
start_row_index,
} => {
2022-06-29 05:44:15 +00:00
assert_eq!(self.block_meta_revs[block_index].row_count, row_count);
assert_eq!(self.block_meta_revs[block_index].start_row_index, start_row_index);
2022-03-14 09:24:25 +00:00
}
2022-03-13 03:06:28 +00:00
EditorScript::AssertBlockEqual { block_index, block } => {
2022-06-26 07:14:24 +00:00
let blocks = self.editor.get_block_meta_revs().await.unwrap();
2022-03-13 03:06:28 +00:00
let compared_block = blocks[block_index].clone();
2022-06-29 05:44:15 +00:00
assert_eq!(compared_block, Arc::new(block));
2022-03-13 03:06:28 +00:00
}
2022-03-14 09:24:25 +00:00
EditorScript::CreateEmptyRow => {
2022-03-18 13:04:01 +00:00
let row_order = self.editor.create_row(None).await.unwrap();
self.row_order_by_row_id.insert(row_order.row_id.clone(), row_order);
self.row_revs = self.get_row_revs().await;
2022-06-29 05:44:15 +00:00
self.block_meta_revs = self.editor.get_block_meta_revs().await.unwrap();
2022-03-14 09:24:25 +00:00
}
2022-06-23 15:06:03 +00:00
EditorScript::CreateRow { payload: context } => {
2022-03-18 13:04:01 +00:00
let row_orders = self.editor.insert_rows(vec![context]).await.unwrap();
for row_order in row_orders {
self.row_order_by_row_id.insert(row_order.row_id.clone(), row_order);
}
self.row_revs = self.get_row_revs().await;
2022-06-29 05:44:15 +00:00
self.block_meta_revs = self.editor.get_block_meta_revs().await.unwrap();
2022-03-14 09:24:25 +00:00
}
EditorScript::UpdateRow { changeset: change } => self.editor.update_row(change).await.unwrap(),
EditorScript::DeleteRows { row_ids } => {
2022-03-18 13:04:01 +00:00
let row_orders = row_ids
.into_iter()
.map(|row_id| self.row_order_by_row_id.get(&row_id).unwrap().clone())
.collect::<Vec<RowOrder>>();
self.editor.delete_rows(row_orders).await.unwrap();
self.row_revs = self.get_row_revs().await;
2022-06-29 05:44:15 +00:00
self.block_meta_revs = self.editor.get_block_meta_revs().await.unwrap();
2022-03-14 09:24:25 +00:00
}
EditorScript::AssertRow { expected_row } => {
let row = &*self
.row_revs
.iter()
.find(|row| row.id == expected_row.id)
.cloned()
.unwrap();
assert_eq!(&expected_row, row);
// if let Some(visibility) = changeset.visibility {
// assert_eq!(row.visibility, visibility);
// }
//
// if let Some(height) = changeset.height {
// assert_eq!(row.height, height);
// }
2022-03-14 09:24:25 +00:00
}
2022-03-16 02:02:37 +00:00
EditorScript::UpdateCell { changeset, is_err } => {
let result = self.editor.update_cell(changeset).await;
if is_err {
assert!(result.is_err())
} else {
let _ = result.unwrap();
self.row_revs = self.get_row_revs().await;
2022-03-16 02:02:37 +00:00
}
2022-03-13 03:06:28 +00:00
}
EditorScript::AssertRowCount(expected_row_count) => {
assert_eq!(expected_row_count, self.row_revs.len());
}
EditorScript::UpdateGridSetting { params } => {
let _ = self.editor.update_grid_setting(params).await.unwrap();
2022-03-13 03:06:28 +00:00
}
2022-06-22 09:11:56 +00:00
EditorScript::InsertGridTableFilter { payload } => {
let params: CreateGridFilterParams = payload.try_into().unwrap();
let layout_type = GridLayoutType::Table;
let params = GridSettingChangesetBuilder::new(&self.grid_id, &layout_type)
.insert_filter(params)
.build();
let _ = self.editor.update_grid_setting(params).await.unwrap();
}
EditorScript::AssertTableFilterCount { count } => {
let layout_type = GridLayoutType::Table;
let filters = self.editor.get_grid_filter(&layout_type).await.unwrap();
assert_eq!(count as usize, filters.len());
}
EditorScript::DeleteGridTableFilter { filter_id } => {
let layout_type = GridLayoutType::Table;
let params = GridSettingChangesetBuilder::new(&self.grid_id, &layout_type)
.delete_filter(&filter_id)
.build();
let _ = self.editor.update_grid_setting(params).await.unwrap();
}
EditorScript::AssertGridSetting { expected_setting } => {
let setting = self.editor.get_grid_setting().await.unwrap();
assert_eq!(expected_setting, setting);
}
EditorScript::AssertGridRevisionPad => {
2022-03-12 14:52:24 +00:00
sleep(Duration::from_millis(2 * REVISION_WRITE_INTERVAL_IN_MILLIS)).await;
2022-03-12 13:06:15 +00:00
let mut grid_rev_manager = grid_manager.make_grid_rev_manager(&self.grid_id, pool.clone()).unwrap();
let grid_pad = grid_rev_manager.load::<GridPadBuilder>(None).await.unwrap();
println!("{}", grid_pad.delta_str());
}
}
}
2022-03-18 09:14:46 +00:00
async fn get_row_revs(&self) -> Vec<Arc<RowRevision>> {
self.editor
.grid_block_snapshots(None)
.await
.unwrap()
.pop()
.unwrap()
.row_revs
2022-03-18 09:14:46 +00:00
}
2022-06-21 08:56:50 +00:00
2022-06-22 09:11:56 +00:00
pub async fn grid_filters(&self) -> Vec<GridFilter> {
let layout_type = GridLayoutType::Table;
self.editor.get_grid_filter(&layout_type).await.unwrap()
}
pub fn text_field(&self) -> &FieldRevision {
self.field_revs
.iter()
.filter(|field_rev| field_rev.field_type == FieldType::RichText)
.collect::<Vec<_>>()
.pop()
.unwrap()
2022-06-21 08:56:50 +00:00
}
2022-03-18 09:14:46 +00:00
}
2022-06-23 15:06:03 +00:00
fn make_all_field_test_grid() -> BuildGridContext {
2022-03-24 09:09:05 +00:00
let text_field = FieldBuilder::new(RichTextTypeOptionBuilder::default())
2022-03-15 11:00:28 +00:00
.name("Name")
.visibility(true)
.build();
// Single Select
2022-03-24 09:09:05 +00:00
let single_select = SingleSelectTypeOptionBuilder::default()
2022-03-15 11:00:28 +00:00
.option(SelectOption::new("Live"))
.option(SelectOption::new("Completed"))
.option(SelectOption::new("Planned"))
.option(SelectOption::new("Paused"));
2022-03-24 09:09:05 +00:00
let single_select_field = FieldBuilder::new(single_select).name("Status").visibility(true).build();
2022-03-15 11:00:28 +00:00
// MultiSelect
2022-03-24 09:09:05 +00:00
let multi_select = MultiSelectTypeOptionBuilder::default()
2022-03-15 11:00:28 +00:00
.option(SelectOption::new("Google"))
.option(SelectOption::new("Facebook"))
.option(SelectOption::new("Twitter"));
let multi_select_field = FieldBuilder::new(multi_select)
.name("Platform")
.visibility(true)
.build();
// Number
2022-03-24 09:09:05 +00:00
let number = NumberTypeOptionBuilder::default().set_format(NumberFormat::USD);
let number_field = FieldBuilder::new(number).name("Price").visibility(true).build();
2022-03-15 11:00:28 +00:00
// Date
2022-03-24 09:09:05 +00:00
let date = DateTypeOptionBuilder::default()
2022-03-15 11:00:28 +00:00
.date_format(DateFormat::US)
.time_format(TimeFormat::TwentyFourHour);
2022-03-24 09:09:05 +00:00
let date_field = FieldBuilder::new(date).name("Time").visibility(true).build();
2022-03-15 11:00:28 +00:00
// Checkbox
2022-03-24 09:09:05 +00:00
let checkbox = CheckboxTypeOptionBuilder::default();
let checkbox_field = FieldBuilder::new(checkbox).name("is done").visibility(true).build();
2022-03-15 11:00:28 +00:00
2022-05-27 12:19:22 +00:00
// URL
let url = URLTypeOptionBuilder::default();
let url_field = FieldBuilder::new(url).name("link").visibility(true).build();
2022-03-15 11:00:28 +00:00
GridBuilder::default()
.add_field(text_field)
.add_field(single_select_field)
.add_field(multi_select_field)
.add_field(number_field)
.add_field(date_field)
.add_field(checkbox_field)
2022-05-27 12:19:22 +00:00
.add_field(url_field)
2022-03-15 11:00:28 +00:00
.add_empty_row()
.add_empty_row()
.add_empty_row()
.build()
}