AppFlowy/frontend/rust-lib/flowy-database/tests/grid/grid_test.rs

382 lines
13 KiB
Rust
Raw Normal View History

Feat/appflowy tauri UI (#1835) * chore: create folders * chore: setup taliwindcss (#1742) * chore: create folders * chore: setup taliwindcss --------- Co-authored-by: nathan <nathan@appflowy.io> Co-authored-by: Nathan.fooo <86001920+appflowy@users.noreply.github.com> * feat: greater to blockquote * fix: local variable 'text' isn't used * feat: #1061 Support markdown to create a blockquote * fix: #1732 the actions of an image look different than the ones of a code block * fix: command of double tilde to strikethrough * feat: callout (#1732) * feat: add callout plugin * refactor: add SelectionMenuItem.node factory makes calloutMenuItem more readable * feat: add color picker * feat: add popover to callout * feat: add emoji to callout * fix: store tint name * fix: remove leading underscores * fix: revert export of editor_entry * refactor: move color tint names to appflowy_editor * fix: #1732 only re-insert text node if it's parent is text node too while deleting * docs: doc comment for SelectionMenuItem.node * fix: disable callout plugin should be re-enabled after #1753 is done * fix: typo --------- Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io> * Feat/http server adapt (#1754) * integrate board plugin into document (#1675) * fix: cursor doesn't blink when opening selection menu * feat: add board plugin * feat: integrate board plugin into document * feat: add i10n and fix known bugs * feat: support jump to board page on document * feat: disable editor scroll only when the board plugin is selected * chore: dart fix * chore: remove unused files * fix: dart lint * Feat/database view (#1765) * chore: rename flowy-database to flowy-sqlite * refactor: rename flowy-grid to flowy-database * refactor: rename grid to database * refactor: rename GridEvent to DatabaseEvent * refactor: rename grid_id to database_id * refactor: rename dart code * fix: #1763 [Bug] Mouse unable to click a certain area * fix: potential async errors (#1772) * feat: Skeleton task (#1775) * chore: change tauri dev npm script * chore: setup prettier * chore: add protobuf type * chore: move test calls to separate component * chore: serve assets from app_flowy folder * chore: import poppins font * chore: install eslint, remove errors * placeholder components * chore: import colors from UI kit, footer panel * chore: reorganise components * chore: redux toolkit, navigation folders and files, navigation hooks * fix: on add folder others close * fix: tauri_dev task * fix: restore grid notification * chore: navigation items events (#1784) * chore: change tauri dev npm script * chore: setup prettier * chore: add protobuf type * chore: move test calls to separate component * chore: serve assets from app_flowy folder * chore: import poppins font * chore: install eslint, remove errors * placeholder components * chore: import colors from UI kit, footer panel * chore: reorganise components * chore: redux toolkit, navigation folders and files, navigation hooks * fix: on add folder others close * fix: tauri_dev task * fix: restore grid notification * chore: shared button * chore: folder/file popup, rename/duplicate/delete items * chore: new page types popup * fix: navitem pages padding * fix: page click mishandle * fix: folder click mishandle * chore: add other page types * fix: stop propagating on button click * fix: one alt * fix: renaming change bg * refactor: brake Navigation Panel into smaller components * chore: header panel folder * chore: focus and select all on rename popup * chore: add classname to popup * chore: navigation panel resize * Feat/appflowy tauri (#1831) * feat:grid view structure * feat:add store and refactor grid page * chore: import icons, resize grid items, change grid items style, add field type icons, reorganize grid toolbar * feat: auth screens(login, signup and confirm-account) ui done * chore: add tailwind class sorter and formatted all files * chore: group svgs into single folder * chore: resolve warnings in svg files * fix: use exported fieldType enum * fix: resolve FieldType referances * chore: auth pages fixes, replace links, replace buttons, svg fixes, navigate between pages, navigate to homepage on main button click --------- Co-authored-by: ascarbek <ascarbek@gmail.com> * ci: wanrings --------- Co-authored-by: Mikias Tilahun Abebe <mikiastilahun@gmail.com> Co-authored-by: Andreas Bichinger <andreas.bichinger@gmail.com> Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io> Co-authored-by: Askarbek Zadauly <ascarbek@gmail.com>
2023-02-10 08:26:14 +00:00
use crate::grid::script::EditorScript::*;
use crate::grid::script::*;
use chrono::NaiveDateTime;
use flowy_database::services::field::{
DateCellContentChangeset, DateCellData, MultiSelectTypeOptionPB, SelectOption, SelectOptionCellContentChangeset,
SingleSelectTypeOption, SELECTION_IDS_SEPARATOR,
};
use flowy_database::services::row::{decode_cell_data_from_type_option_cell_data, CreateRowMetaBuilder};
use grid_model::entities::{
CellChangeset, FieldChangesetParams, FieldType, GridBlockInfoChangeset, GridBlockMetaSnapshot, RowMetaChangeset,
TypeOptionDataFormat,
};
#[tokio::test]
async fn grid_create_field() {
let mut test = GridEditorTest::new().await;
let (text_field_params, text_field_meta) = create_text_field(&test.grid_id);
let (single_select_params, single_select_field) = create_single_select_field(&test.grid_id);
let scripts = vec![
CreateField {
params: text_field_params,
},
AssertFieldEqual {
field_index: test.field_count,
field_meta: text_field_meta,
},
];
test.run_scripts(scripts).await;
let scripts = vec![
CreateField {
params: single_select_params,
},
AssertFieldEqual {
field_index: test.field_count,
field_meta: single_select_field,
},
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_create_duplicate_field() {
let mut test = GridEditorTest::new().await;
let (params, _) = create_text_field(&test.grid_id);
let field_count = test.field_count;
let expected_field_count = field_count + 1;
let scripts = vec![
CreateField { params: params.clone() },
CreateField { params },
AssertFieldCount(expected_field_count),
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_update_field_with_empty_change() {
let mut test = GridEditorTest::new().await;
let (params, field_meta) = create_single_select_field(&test.grid_id);
let changeset = FieldChangesetParams {
field_id: field_meta.id.clone(),
grid_id: test.grid_id.clone(),
..Default::default()
};
let scripts = vec![
CreateField { params },
UpdateField { changeset },
AssertFieldEqual {
field_index: test.field_count,
field_meta,
},
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_update_field() {
let mut test = GridEditorTest::new().await;
let (single_select_params, single_select_field) = create_single_select_field(&test.grid_id);
let mut cloned_field = single_select_field.clone();
let mut single_select_type_option = SingleSelectTypeOption::from(&single_select_field);
single_select_type_option.options.push(SelectOption::new("Unknown"));
let changeset = FieldChangesetParams {
field_id: single_select_field.id.clone(),
grid_id: test.grid_id.clone(),
frozen: Some(true),
width: Some(1000),
type_option_data: Some(single_select_type_option.protobuf_bytes().to_vec()),
..Default::default()
};
cloned_field.frozen = true;
cloned_field.width = 1000;
cloned_field.insert_type_option_entry(&single_select_type_option);
let scripts = vec![
CreateField {
params: single_select_params,
},
UpdateField { changeset },
AssertFieldEqual {
field_index: test.field_count,
field_meta: cloned_field,
},
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_delete_field() {
let mut test = GridEditorTest::new().await;
let expected_field_count = test.field_count;
let (text_params, text_field) = create_text_field(&test.grid_id);
let scripts = vec![
CreateField { params: text_params },
DeleteField { field_meta: text_field },
AssertFieldCount(expected_field_count),
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_create_block() {
let grid_block = GridBlockMetaSnapshot::new();
let scripts = vec![
AssertBlockCount(1),
CreateBlock { block: grid_block },
AssertBlockCount(2),
];
GridEditorTest::new().await.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_update_block() {
let grid_block = GridBlockMetaSnapshot::new();
let mut cloned_grid_block = grid_block.clone();
let changeset = GridBlockInfoChangeset {
block_id: grid_block.block_id.clone(),
start_row_index: Some(2),
row_count: Some(10),
};
cloned_grid_block.start_row_index = 2;
cloned_grid_block.row_count = 10;
let scripts = vec![
AssertBlockCount(1),
CreateBlock { block: grid_block },
UpdateBlock { changeset },
AssertBlockCount(2),
AssertBlockEqual {
block_index: 1,
block: cloned_grid_block,
},
];
GridEditorTest::new().await.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_create_row() {
let scripts = vec![AssertRowCount(3), CreateEmptyRow, CreateEmptyRow, AssertRowCount(5)];
GridEditorTest::new().await.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_create_row2() {
let mut test = GridEditorTest::new().await;
let create_row_context = CreateRowMetaBuilder::new(&test.field_metas).build();
let scripts = vec![
AssertRowCount(3),
CreateRow {
context: create_row_context,
},
AssertRowCount(4),
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_update_row() {
let mut test = GridEditorTest::new().await;
let context = CreateRowMetaBuilder::new(&test.field_metas).build();
let changeset = RowMetaChangeset {
row_id: context.row_id.clone(),
height: None,
visibility: None,
cell_by_field_id: Default::default(),
};
let scripts = vec![
AssertRowCount(3),
CreateRow { context },
UpdateRow {
changeset: changeset.clone(),
},
AssertRow { changeset },
AssertRowCount(4),
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_delete_row() {
let mut test = GridEditorTest::new().await;
let context_1 = CreateRowMetaBuilder::new(&test.field_metas).build();
let context_2 = CreateRowMetaBuilder::new(&test.field_metas).build();
let row_ids = vec![context_1.row_id.clone(), context_2.row_id.clone()];
let scripts = vec![
AssertRowCount(3),
CreateRow { context: context_1 },
CreateRow { context: context_2 },
AssertBlockCount(1),
AssertBlock {
block_index: 0,
row_count: 5,
start_row_index: 0,
},
DeleteRow { 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 = CreateRowMetaBuilder::new(&test.field_metas);
for field in &test.field_metas {
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 = MultiSelectTypeOptionPB::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();
let scripts = vec![CreateRow { context }, AssertGridMetaPad];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_row_add_date_cell_test() {
let mut test = GridEditorTest::new().await;
let mut builder = CreateRowMetaBuilder::new(&test.field_metas);
let mut date_field = None;
let timestamp = 1647390674;
for field in &test.field_metas {
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(&timestamp.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!(
decode_cell_data_from_type_option_cell_data(cell_data.data.clone(), &date_field, &date_field.field_type)
.parse::<DateCellData>()
.unwrap()
.date,
"2022/03/16",
);
let scripts = vec![CreateRow { context }];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_cell_update() {
let mut test = GridEditorTest::new().await;
let field_metas = &test.field_metas;
let row_metas = &test.row_metas;
let grid_blocks = &test.grid_blocks;
assert_eq!(row_metas.len(), 3);
assert_eq!(grid_blocks.len(), 1);
let block_id = &grid_blocks.first().unwrap().block_id;
let mut scripts = vec![];
for (index, row_meta) in row_metas.iter().enumerate() {
for field_meta in field_metas {
if index == 0 {
let data = match field_meta.field_type {
FieldType::RichText => "".to_string(),
FieldType::Number => "123".to_string(),
FieldType::DateTime => make_date_cell_string("123"),
FieldType::SingleSelect => {
let type_option = SingleSelectTypeOption::from(field_meta);
SelectOptionCellContentChangeset::from_insert(&type_option.options.first().unwrap().id).to_str()
}
FieldType::MultiSelect => {
let type_option = MultiSelectTypeOptionPB::from(field_meta);
SelectOptionCellContentChangeset::from_insert(&type_option.options.first().unwrap().id).to_str()
}
FieldType::Checkbox => "1".to_string(),
FieldType::URL => "1".to_string(),
};
scripts.push(UpdateCell {
changeset: CellChangeset {
database_id: block_id.to_string(),
row_id: row_meta.id.clone(),
field_id: field_meta.id.clone(),
cell_content_changeset: Some(data),
},
is_err: false,
});
}
if index == 1 {
let (data, is_err) = match field_meta.field_type {
FieldType::RichText => ("1".to_string().repeat(10001), true),
FieldType::Number => ("abc".to_string(), true),
FieldType::DateTime => ("abc".to_string(), true),
FieldType::SingleSelect => (SelectOptionCellContentChangeset::from_insert("abc").to_str(), false),
FieldType::MultiSelect => (SelectOptionCellContentChangeset::from_insert("abc").to_str(), false),
FieldType::Checkbox => ("2".to_string(), false),
FieldType::URL => ("2".to_string(), false),
};
scripts.push(UpdateCell {
changeset: CellChangeset {
database_id: block_id.to_string(),
row_id: row_meta.id.clone(),
field_id: field_meta.id.clone(),
cell_content_changeset: Some(data),
},
is_err,
});
}
}
}
test.run_scripts(scripts).await;
}
fn make_date_cell_string(s: &str) -> String {
serde_json::to_string(&DateCellContentChangeset {
date: Some(s.to_string()),
time: None,
})
.unwrap()
}