mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: add document test (#2932)
This commit is contained in:
parent
8dfbfe3c42
commit
9fb8f221cf
@ -147,7 +147,7 @@ pub struct DocumentDataPB {
|
||||
pub meta: MetaPB,
|
||||
}
|
||||
|
||||
#[derive(Default, ProtoBuf, Debug)]
|
||||
#[derive(Default, ProtoBuf, Debug, Clone)]
|
||||
pub struct BlockPB {
|
||||
#[pb(index = 1)]
|
||||
pub id: String,
|
||||
|
@ -57,6 +57,30 @@ impl DocumentEventTest {
|
||||
OpenDocumentData { id: doc_id, data }
|
||||
}
|
||||
|
||||
pub async fn get_block(&self, doc_id: &str, block_id: &str) -> Option<BlockPB> {
|
||||
let document = self.open_document(doc_id.to_string()).await;
|
||||
document.data.blocks.get(block_id).cloned()
|
||||
}
|
||||
|
||||
pub async fn get_page_id(&self, doc_id: &str) -> String {
|
||||
let data = self.get_document_data(doc_id).await;
|
||||
data.page_id
|
||||
}
|
||||
|
||||
pub async fn get_document_data(&self, doc_id: &str) -> DocumentDataPB {
|
||||
let document = self.open_document(doc_id.to_string()).await;
|
||||
document.data
|
||||
}
|
||||
|
||||
pub async fn get_block_children(&self, doc_id: &str, block_id: &str) -> Option<Vec<String>> {
|
||||
let block = self.get_block(doc_id, block_id).await;
|
||||
block.as_ref()?;
|
||||
let document_data = self.get_document_data(doc_id).await;
|
||||
let children_map = document_data.meta.children_map;
|
||||
let children_id = block.unwrap().children_id;
|
||||
children_map.get(&children_id).map(|c| c.children.clone())
|
||||
}
|
||||
|
||||
pub async fn apply_actions(&self, payload: ApplyActionPayloadPB) {
|
||||
let core = &self.inner;
|
||||
EventBuilder::new(core.clone())
|
3
frontend/rust-lib/flowy-test/src/document/mod.rs
Normal file
3
frontend/rust-lib/flowy-test/src/document/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod document_event;
|
||||
pub mod text_block_event;
|
||||
pub mod utils;
|
125
frontend/rust-lib/flowy-test/src/document/text_block_event.rs
Normal file
125
frontend/rust-lib/flowy-test/src/document/text_block_event.rs
Normal file
@ -0,0 +1,125 @@
|
||||
use crate::document::document_event::DocumentEventTest;
|
||||
use crate::document::utils::{gen_id, gen_text_block_data};
|
||||
use flowy_document2::entities::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
const TEXT_BLOCK_TY: &str = "paragraph";
|
||||
|
||||
pub struct TextBlockEventTest {
|
||||
doc: Arc<DocumentEventTest>,
|
||||
doc_id: String,
|
||||
}
|
||||
|
||||
impl TextBlockEventTest {
|
||||
pub async fn new() -> Self {
|
||||
let doc = DocumentEventTest::new().await;
|
||||
let doc_id = doc.create_document().await.id;
|
||||
Self {
|
||||
doc: Arc::new(doc),
|
||||
doc_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get(&self, block_id: &str) -> Option<BlockPB> {
|
||||
let doc = self.doc.clone();
|
||||
let doc_id = self.doc_id.clone();
|
||||
doc.get_block(&doc_id, block_id).await
|
||||
}
|
||||
|
||||
/// Insert a new text block at the index of parent's children.
|
||||
pub async fn insert_index(&self, text: String, index: usize, parent_id: Option<&str>) -> String {
|
||||
let doc = self.doc.clone();
|
||||
let doc_id = self.doc_id.clone();
|
||||
let page_id = self.doc.get_page_id(&doc_id).await;
|
||||
let parent_id = parent_id
|
||||
.map(|id| id.to_string())
|
||||
.unwrap_or_else(|| page_id);
|
||||
let parent_children = self.doc.get_block_children(&doc_id, &parent_id).await;
|
||||
|
||||
let prev_id = {
|
||||
// If index is 0, then the new block will be the first child of parent.
|
||||
if index == 0 {
|
||||
None
|
||||
} else {
|
||||
parent_children.and_then(|children| {
|
||||
// If index is greater than the length of children, then the new block will be the last child of parent.
|
||||
if index >= children.len() {
|
||||
children.last().cloned()
|
||||
} else {
|
||||
children.get(index - 1).cloned()
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
let new_block_id = gen_id();
|
||||
let data = gen_text_block_data(text);
|
||||
|
||||
let new_block = BlockPB {
|
||||
id: new_block_id.clone(),
|
||||
ty: TEXT_BLOCK_TY.to_string(),
|
||||
data,
|
||||
parent_id: parent_id.clone(),
|
||||
children_id: gen_id(),
|
||||
};
|
||||
let action = BlockActionPB {
|
||||
action: BlockActionTypePB::Insert,
|
||||
payload: BlockActionPayloadPB {
|
||||
block: new_block,
|
||||
prev_id,
|
||||
parent_id: Some(parent_id),
|
||||
},
|
||||
};
|
||||
let payload = ApplyActionPayloadPB {
|
||||
document_id: doc_id,
|
||||
actions: vec![action],
|
||||
};
|
||||
doc.apply_actions(payload).await;
|
||||
new_block_id
|
||||
}
|
||||
|
||||
pub async fn update(&self, block_id: &str, text: String) {
|
||||
let doc = self.doc.clone();
|
||||
let doc_id = self.doc_id.clone();
|
||||
let block = self.get(block_id).await.unwrap();
|
||||
let data = gen_text_block_data(text);
|
||||
let new_block = {
|
||||
let mut new_block = block.clone();
|
||||
new_block.data = data;
|
||||
new_block
|
||||
};
|
||||
let action = BlockActionPB {
|
||||
action: BlockActionTypePB::Update,
|
||||
payload: BlockActionPayloadPB {
|
||||
block: new_block,
|
||||
prev_id: None,
|
||||
parent_id: Some(block.parent_id.clone()),
|
||||
},
|
||||
};
|
||||
let payload = ApplyActionPayloadPB {
|
||||
document_id: doc_id,
|
||||
actions: vec![action],
|
||||
};
|
||||
doc.apply_actions(payload).await;
|
||||
}
|
||||
|
||||
pub async fn delete(&self, block_id: &str) {
|
||||
let doc = self.doc.clone();
|
||||
let doc_id = self.doc_id.clone();
|
||||
let block = self.get(block_id).await.unwrap();
|
||||
let parent_id = block.parent_id.clone();
|
||||
let action = BlockActionPB {
|
||||
action: BlockActionTypePB::Delete,
|
||||
payload: BlockActionPayloadPB {
|
||||
block,
|
||||
prev_id: None,
|
||||
parent_id: Some(parent_id),
|
||||
},
|
||||
};
|
||||
let payload = ApplyActionPayloadPB {
|
||||
document_id: doc_id,
|
||||
actions: vec![action],
|
||||
};
|
||||
doc.apply_actions(payload).await;
|
||||
}
|
||||
}
|
@ -1,12 +1,22 @@
|
||||
use crate::document::document_event::*;
|
||||
use flowy_document2::entities::*;
|
||||
use flowy_test::document_event::OpenDocumentData;
|
||||
use nanoid::nanoid;
|
||||
use serde_json::json;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn gen_id() -> String {
|
||||
nanoid!(10)
|
||||
}
|
||||
|
||||
pub fn gen_text_block_data(text: String) -> String {
|
||||
json!({
|
||||
"delta": [{
|
||||
"insert": text
|
||||
}]
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub struct ParseDocumentData {
|
||||
pub doc_id: String,
|
||||
pub page_id: String,
|
@ -15,7 +15,7 @@ use flowy_user::errors::FlowyError;
|
||||
use crate::event_builder::EventBuilder;
|
||||
use crate::user_event::{async_sign_up, init_user_setting, SignUpContext};
|
||||
|
||||
pub mod document_event;
|
||||
pub mod document;
|
||||
pub mod event_builder;
|
||||
pub mod folder_event;
|
||||
pub mod user_event;
|
||||
|
28
frontend/rust-lib/flowy-test/tests/document/block_test.rs
Normal file
28
frontend/rust-lib/flowy-test/tests/document/block_test.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use flowy_test::document::text_block_event::TextBlockEventTest;
|
||||
use flowy_test::document::utils::gen_text_block_data;
|
||||
|
||||
#[tokio::test]
|
||||
async fn insert_text_block_test() {
|
||||
let test = TextBlockEventTest::new().await;
|
||||
let text = "Hello World".to_string();
|
||||
let block_id = test.insert_index(text.clone(), 1, None).await;
|
||||
let block = test.get(&block_id).await;
|
||||
assert!(block.is_some());
|
||||
let block = block.unwrap();
|
||||
let data = gen_text_block_data(text);
|
||||
assert_eq!(block.data, data);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn update_text_block_test() {
|
||||
let test = TextBlockEventTest::new().await;
|
||||
let insert_text = "Hello World".to_string();
|
||||
let block_id = test.insert_index(insert_text.clone(), 1, None).await;
|
||||
let update_text = "Hello World 2".to_string();
|
||||
test.update(&block_id, update_text.clone()).await;
|
||||
let block = test.get(&block_id).await;
|
||||
assert!(block.is_some());
|
||||
let block = block.unwrap();
|
||||
let update_data = gen_text_block_data(update_text);
|
||||
assert_eq!(block.data, update_data);
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
use crate::document::utils::*;
|
||||
use flowy_document2::entities::*;
|
||||
use flowy_test::document_event::DocumentEventTest;
|
||||
use flowy_test::document::document_event::DocumentEventTest;
|
||||
use flowy_test::document::utils::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_document_event_test() {
|
@ -1,2 +1,2 @@
|
||||
mod test;
|
||||
mod utils;
|
||||
mod block_test;
|
||||
mod document_test;
|
||||
|
Loading…
Reference in New Issue
Block a user