Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/run flutter test in flowy editor branch #3

Open
wants to merge 4 commits into
base: feat/flowy_editor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/dart_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
pull_request:
branches:
- 'main'
- 'feat/flowy_editor'

env:
CARGO_TERM_COLOR: always
Expand Down Expand Up @@ -71,3 +72,9 @@ jobs:
flutter pub get
flutter test

- name: Run FlowyEditor tests
working-directory: frontend/app_flowy/packages/flowy_editor
run: |
flutter pub get
flutter test

2 changes: 1 addition & 1 deletion frontend/rust-lib/flowy-grid/src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ pub(crate) async fn get_select_option_handler(
let any_cell_data: AnyCellData = match cell_rev {
None => AnyCellData {
data: "".to_string(),
field_type: field_rev.field_type_rev.clone().into(),
field_type: field_rev.field_type_rev.into(),
},
Some(cell_rev) => cell_rev.try_into()?,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::grid::script::EditorScript::*;
use crate::grid::script::*;
use crate::grid::block_test::script::GridRowTest;
use crate::grid::block_test::script::RowScript::*;

use flowy_grid_data_model::revision::{GridBlockMetaRevision, GridBlockMetaRevisionChangeset};

#[tokio::test]
Expand All @@ -10,7 +11,7 @@ async fn grid_create_block() {
CreateBlock { block: block_meta_rev },
AssertBlockCount(2),
];
GridEditorTest::new().await.run_scripts(scripts).await;
GridRowTest::new().await.run_scripts(scripts).await;
}

#[tokio::test]
Expand All @@ -36,5 +37,5 @@ async fn grid_update_block() {
block: cloned_grid_block,
},
];
GridEditorTest::new().await.run_scripts(scripts).await;
GridRowTest::new().await.run_scripts(scripts).await;
}
5 changes: 5 additions & 0 deletions frontend/rust-lib/flowy-grid/tests/grid/block_test/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![allow(clippy::module_inception)]
mod block_test;
mod row_test;
mod script;
pub mod util;
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::grid::field_util::*;
use crate::grid::row_util::GridRowTestBuilder;
use crate::grid::script::EditorScript::*;
use crate::grid::script::*;
use crate::grid::block_test::script::GridRowTest;
use crate::grid::block_test::script::RowScript::*;
use crate::grid::block_test::util::GridRowTestBuilder;
use chrono::NaiveDateTime;
use flowy_grid::entities::FieldType;
use flowy_grid::services::cell::decode_any_cell_data;
use flowy_grid::services::field::select_option::SELECTION_IDS_SEPARATOR;
use flowy_grid::services::field::{DateCellData, MultiSelectTypeOption, SingleSelectTypeOption};
use flowy_grid::services::row::CreateRowRevisionBuilder;

use crate::grid::field_test::util::make_date_cell_string;
use flowy_grid_data_model::revision::RowMetaChangeset;

#[tokio::test]
async fn grid_create_row_count_test() {
let test = GridEditorTest::new().await;
let mut test = GridRowTest::new().await;
let scripts = vec![
AssertRowCount(3),
CreateEmptyRow,
Expand All @@ -22,12 +22,12 @@ async fn grid_create_row_count_test() {
},
AssertRowCount(6),
];
GridEditorTest::new().await.run_scripts(scripts).await;
test.run_scripts(scripts).await;
}

#[tokio::test]
async fn grid_update_row() {
let mut test = GridEditorTest::new().await;
let mut test = GridRowTest::new().await;
let payload = GridRowTestBuilder::new(&test).build();
let changeset = RowMetaChangeset {
row_id: payload.row_id.clone(),
Expand All @@ -39,14 +39,14 @@ async fn grid_update_row() {
let scripts = vec![AssertRowCount(3), CreateRow { payload }, UpdateRow { changeset }];
test.run_scripts(scripts).await;

let expected_row = (&*test.row_revs.last().cloned().unwrap()).clone();
let expected_row = test.last_row().unwrap();
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;
let mut test = GridRowTest::new().await;
let payload1 = GridRowTestBuilder::new(&test).build();
let payload2 = GridRowTestBuilder::new(&test).build();
let row_ids = vec![payload1.row_id.clone(), payload2.row_id.clone()];
Expand All @@ -72,9 +72,9 @@ async fn grid_delete_row() {

#[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 {
let mut test = GridRowTest::new().await;
let mut builder = test.builder();
for field in test.field_revs() {
let field_type: FieldType = field.field_type_rev.into();
match field_type {
FieldType::RichText => {
Expand Down Expand Up @@ -112,17 +112,17 @@ async fn grid_row_add_cells_test() {
}
}
let context = builder.build();
let scripts = vec![CreateRow { payload: context }, AssertGridRevisionPad];
let scripts = vec![CreateRow { payload: context }];
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 test = GridRowTest::new().await;
let mut builder = test.builder();
let mut date_field = None;
let timestamp = 1647390674;
for field in &test.field_revs {
for field in test.field_revs() {
let field_type: FieldType = field.field_type_rev.into();
if field_type == FieldType::DateTime {
date_field = Some(field.clone());
Expand Down
150 changes: 150 additions & 0 deletions frontend/rust-lib/flowy-grid/tests/grid/block_test/script.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use crate::grid::grid_editor::GridEditorTest;
use flowy_grid::entities::RowInfo;
use flowy_grid::services::row::{CreateRowRevisionBuilder, CreateRowRevisionPayload};
use flowy_grid_data_model::revision::{
FieldRevision, GridBlockMetaRevision, GridBlockMetaRevisionChangeset, RowMetaChangeset, RowRevision,
};
use std::sync::Arc;

pub enum RowScript {
CreateEmptyRow,
CreateRow {
payload: CreateRowRevisionPayload,
},
UpdateRow {
changeset: RowMetaChangeset,
},
AssertRow {
expected_row: RowRevision,
},
DeleteRows {
row_ids: Vec<String>,
},
AssertRowCount(usize),
CreateBlock {
block: GridBlockMetaRevision,
},
UpdateBlock {
changeset: GridBlockMetaRevisionChangeset,
},
AssertBlockCount(usize),
AssertBlock {
block_index: usize,
row_count: i32,
start_row_index: i32,
},
AssertBlockEqual {
block_index: usize,
block: GridBlockMetaRevision,
},
}

pub struct GridRowTest {
inner: GridEditorTest,
}

impl GridRowTest {
pub async fn new() -> Self {
let editor_test = GridEditorTest::new().await;
Self { inner: editor_test }
}

pub fn field_revs(&self) -> &Vec<Arc<FieldRevision>> {
&self.field_revs
}

pub fn last_row(&self) -> Option<RowRevision> {
self.row_revs.last().map(|a| a.clone().as_ref().clone())
}

pub async fn run_scripts(&mut self, scripts: Vec<RowScript>) {
for script in scripts {
self.run_script(script).await;
}
}

pub fn builder(&self) -> CreateRowRevisionBuilder {
CreateRowRevisionBuilder::new(&self.field_revs)
}

pub async fn run_script(&mut self, script: RowScript) {
match script {
RowScript::CreateEmptyRow => {
let row_order = self.editor.create_row(None).await.unwrap();
self.row_order_by_row_id
.insert(row_order.row_id().to_owned(), row_order);
self.row_revs = self.get_row_revs().await;
self.block_meta_revs = self.editor.get_block_meta_revs().await.unwrap();
}
RowScript::CreateRow { payload: context } => {
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().to_owned(), row_order);
}
self.row_revs = self.get_row_revs().await;
self.block_meta_revs = self.editor.get_block_meta_revs().await.unwrap();
}
RowScript::UpdateRow { changeset: change } => self.editor.update_row(change).await.unwrap(),
RowScript::DeleteRows { row_ids } => {
let row_orders = row_ids
.into_iter()
.map(|row_id| self.row_order_by_row_id.get(&row_id).unwrap().clone())
.collect::<Vec<RowInfo>>();

self.editor.delete_rows(row_orders).await.unwrap();
self.row_revs = self.get_row_revs().await;
self.block_meta_revs = self.editor.get_block_meta_revs().await.unwrap();
}
RowScript::AssertRow { expected_row } => {
let row = &*self
.row_revs
.iter()
.find(|row| row.id == expected_row.id)
.cloned()
.unwrap();
assert_eq!(&expected_row, row);
}
RowScript::AssertRowCount(expected_row_count) => {
assert_eq!(expected_row_count, self.row_revs.len());
}
RowScript::CreateBlock { block } => {
self.editor.create_block(block).await.unwrap();
self.block_meta_revs = self.editor.get_block_meta_revs().await.unwrap();
}
RowScript::UpdateBlock { changeset: change } => {
self.editor.update_block(change).await.unwrap();
}
RowScript::AssertBlockCount(count) => {
assert_eq!(self.editor.get_block_meta_revs().await.unwrap().len(), count);
}
RowScript::AssertBlock {
block_index,
row_count,
start_row_index,
} => {
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);
}
RowScript::AssertBlockEqual { block_index, block } => {
let blocks = self.editor.get_block_meta_revs().await.unwrap();
let compared_block = blocks[block_index].clone();
assert_eq!(compared_block, Arc::new(block));
}
}
}
}

impl std::ops::Deref for GridRowTest {
type Target = GridEditorTest;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl std::ops::DerefMut for GridRowTest {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use crate::grid::script::GridEditorTest;
use crate::grid::block_test::script::GridRowTest;

use flowy_grid::entities::FieldType;
use flowy_grid::services::field::DateCellChangeset;
use flowy_grid::services::row::{CreateRowRevisionBuilder, CreateRowRevisionPayload};
use flowy_grid_data_model::revision::FieldRevision;
use strum::EnumCount;

pub struct GridRowTestBuilder<'a> {
test: &'a GridEditorTest,
test: &'a GridRowTest,
inner_builder: CreateRowRevisionBuilder<'a>,
}

impl<'a> GridRowTestBuilder<'a> {
pub fn new(test: &'a GridEditorTest) -> Self {
assert_eq!(test.field_revs.len(), FieldType::COUNT);
pub fn new(test: &'a GridRowTest) -> Self {
assert_eq!(test.field_revs().len(), FieldType::COUNT);

let inner_builder = CreateRowRevisionBuilder::new(&test.field_revs);
let inner_builder = CreateRowRevisionBuilder::new(test.field_revs());
Self { test, inner_builder }
}
#[allow(dead_code)]
Expand Down Expand Up @@ -59,7 +60,7 @@ impl<'a> GridRowTestBuilder<'a> {

pub fn field_rev_with_type(&self, field_type: &FieldType) -> FieldRevision {
self.test
.field_revs
.field_revs()
.iter()
.find(|field_rev| {
let t_field_type: FieldType = field_rev.field_type_rev.into();
Expand Down
2 changes: 2 additions & 0 deletions frontend/rust-lib/flowy-grid/tests/grid/cell_test/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod script;
mod test;
Loading