diff --git a/.idea/appflowy_client.iml b/.idea/appflowy_client.iml index f33c861988..5c6b5b4d21 100644 --- a/.idea/appflowy_client.iml +++ b/.idea/appflowy_client.iml @@ -83,6 +83,9 @@ + + + diff --git a/rust-lib/flowy-sdk/src/lib.rs b/rust-lib/flowy-sdk/src/lib.rs index db7265177e..32595e2424 100644 --- a/rust-lib/flowy-sdk/src/lib.rs +++ b/rust-lib/flowy-sdk/src/lib.rs @@ -29,7 +29,7 @@ impl FlowySDK { FlowySDK::init_log(&self.root); tracing::info!("🔥 Root path: {}", self.root); - flowy_infra::kv::KVStore::init(&self.root); + let _ = flowy_infra::kv::KVStore::init(&self.root); FlowySDK::init_modules(&self.root, self.server); } diff --git a/rust-lib/flowy-test/Cargo.toml b/rust-lib/flowy-test/Cargo.toml index fc28e6525f..fb52d19629 100644 --- a/rust-lib/flowy-test/Cargo.toml +++ b/rust-lib/flowy-test/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" flowy-sdk = { path = "../flowy-sdk"} flowy-dispatch = { path = "../flowy-dispatch"} flowy-user = { path = "../flowy-user"} +flowy-workspace = { path = "../flowy-workspace"} serde = { version = "1.0", features = ["derive"] } bincode = { version = "1.3"} diff --git a/rust-lib/flowy-test/src/builder.rs b/rust-lib/flowy-test/src/builder.rs index a100872fa0..271971cc80 100644 --- a/rust-lib/flowy-test/src/builder.rs +++ b/rust-lib/flowy-test/src/builder.rs @@ -1,46 +1,52 @@ -use crate::{helper::new_user_after_login, init_sdk, tester::Tester}; -use flowy_dispatch::prelude::{EventDispatch, FromBytes, ModuleRequest, ToBytes}; -use flowy_user::{entities::UserDetail, event::UserEvent::SignOut}; +use flowy_dispatch::prelude::{FromBytes, ToBytes}; +use flowy_user::entities::UserDetail; use std::{ fmt::{Debug, Display}, hash::Hash, }; -pub struct TestBuilder { - login: Option, - inner: Option>, - pub user_detail: Option, -} +use crate::tester::{TesterContext, TesterTrait}; +use flowy_user::errors::UserError; +use flowy_workspace::errors::WorkspaceError; +use std::marker::PhantomData; -impl TestBuilder -where - Error: FromBytes + Debug, -{ +pub type WorkspaceTestBuilder = TestBuilder>; +impl WorkspaceTestBuilder { pub fn new() -> Self { - TestBuilder:: { - login: None, - inner: None, + Self { + tester: Box::new(FixedUserTester::::new()), user_detail: None, } } +} +pub type UserTestBuilder = TestBuilder>; +impl UserTestBuilder { + pub fn new() -> Self { + Self { + tester: Box::new(RandomUserTester::::new()), + user_detail: None, + } + } +} + +pub struct TestBuilder { + pub tester: Box, + pub user_detail: Option, +} + +impl TestBuilder +where + T: TesterTrait, +{ pub fn login(mut self) -> Self { - let user_detail = new_user_after_login(); + let user_detail = self.tester.login(); self.user_detail = Some(user_detail); self } pub fn logout(self) -> Self { - init_sdk(); - let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut)); - self - } - - pub fn event(mut self, event: E) -> Self - where - E: Eq + Hash + Debug + Clone + Display, - { - self.inner = Some(Tester::::new(event)); + self.tester.logout(); self } @@ -48,12 +54,20 @@ where where P: ToBytes, { - self.inner.as_mut().unwrap().set_request(request); + self.tester.set_payload(request); + self + } + + pub fn event(mut self, event: E) -> Self + where + E: Eq + Hash + Debug + Clone + Display, + { + self.tester.set_event(event); self } pub fn sync_send(mut self) -> Self { - self.inner.as_mut().unwrap().sync_send(); + self.tester.sync_send(); self } @@ -61,22 +75,70 @@ where where R: FromBytes, { - let inner = self.inner.take().unwrap(); - inner.parse::() + self.tester.parse::() } - pub fn error(mut self) -> Error { - let inner = self.inner.take().unwrap(); - inner.error() - } + pub fn error(mut self) -> ::Error { self.tester.error() } pub fn assert_error(mut self) -> Self { - self.inner.as_mut().unwrap().assert_error(); + self.tester.assert_error(); self } pub fn assert_success(mut self) -> Self { - self.inner.as_mut().unwrap().assert_success(); + self.tester.assert_success(); self } } + +pub struct RandomUserTester { + context: TesterContext, + err_phantom: PhantomData, +} + +impl RandomUserTester +where + Error: FromBytes + Debug, +{ + pub fn new() -> Self { + Self { + context: TesterContext::default(), + err_phantom: PhantomData, + } + } +} + +impl TesterTrait for RandomUserTester +where + Error: FromBytes + Debug, +{ + type Error = Error; + + fn context(&mut self) -> &mut TesterContext { &mut self.context } +} + +pub struct FixedUserTester { + context: TesterContext, + err_phantom: PhantomData, +} + +impl FixedUserTester +where + Error: FromBytes + Debug, +{ + pub fn new() -> Self { + Self { + context: TesterContext::default(), + err_phantom: PhantomData, + } + } +} + +impl TesterTrait for FixedUserTester +where + Error: FromBytes + Debug, +{ + type Error = Error; + + fn context(&mut self) -> &mut TesterContext { &mut self.context } +} diff --git a/rust-lib/flowy-test/src/helper.rs b/rust-lib/flowy-test/src/helper.rs index 2ea710e809..88298e0a69 100644 --- a/rust-lib/flowy-test/src/helper.rs +++ b/rust-lib/flowy-test/src/helper.rs @@ -1,10 +1,3 @@ -use crate::{init_sdk, tester::Tester}; -use flowy_dispatch::prelude::*; -use flowy_user::{ - entities::{SignInRequest, UserDetail}, - errors::UserError, - event::UserEvent::{SignIn, SignOut}, -}; use std::{fs, path::PathBuf}; pub fn root_dir() -> String { @@ -23,21 +16,6 @@ pub fn root_dir() -> String { root_dir } -pub fn new_user_after_login() -> UserDetail { - init_sdk(); - let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut)); - let request = SignInRequest { - email: valid_email(), - password: valid_password(), - }; - - let mut tester = Tester::::new(SignIn); - tester.set_request(request); - tester.sync_send(); - - tester.parse::() -} - pub(crate) fn valid_email() -> String { "annie@appflowy.io".to_string() } pub(crate) fn valid_password() -> String { "HelloWorld!123".to_string() } diff --git a/rust-lib/flowy-test/src/lib.rs b/rust-lib/flowy-test/src/lib.rs index ebe11e7a66..b67859f336 100644 --- a/rust-lib/flowy-test/src/lib.rs +++ b/rust-lib/flowy-test/src/lib.rs @@ -1,13 +1,17 @@ -mod builder; +pub mod builder; mod helper; mod tester; use crate::helper::root_dir; use flowy_sdk::FlowySDK; -use std::{sync::Once, thread}; +use std::sync::Once; pub mod prelude { - pub use crate::{builder::TestBuilder, tester::Tester}; + pub use crate::{ + builder::{TestBuilder, *}, + helper::*, + tester::Tester, + }; pub use flowy_dispatch::prelude::*; } diff --git a/rust-lib/flowy-test/src/tester.rs b/rust-lib/flowy-test/src/tester.rs index faf8268058..a3807c572f 100644 --- a/rust-lib/flowy-test/src/tester.rs +++ b/rust-lib/flowy-test/src/tester.rs @@ -1,7 +1,14 @@ -use crate::init_sdk; +use crate::{ + helper::{valid_email, valid_password}, + init_sdk, +}; use flowy_dispatch::prelude::*; pub use flowy_sdk::*; -use flowy_user::prelude::*; +use flowy_user::{ + errors::UserError, + event::UserEvent::{SignIn, SignOut}, + prelude::*, +}; use std::{ convert::TryFrom, fmt::{Debug, Display}, @@ -89,32 +96,6 @@ where } } -pub struct RandomUserTester { - context: TesterContext, - err_phantom: PhantomData, -} - -impl RandomUserTester -where - Error: FromBytes + Debug, -{ - pub fn new() -> Self { - Self { - context: TesterContext::default(), - err_phantom: PhantomData, - } - } -} - -impl TesterTrait for RandomUserTester -where - Error: FromBytes + Debug, -{ - type Error = Error; - - fn context(&mut self) -> &mut TesterContext { &mut self.context } -} - pub struct TesterContext { request: Option, status_code: StatusCode, @@ -140,12 +121,20 @@ pub trait TesterTrait { fn assert_success(&mut self) { self.context().status_code = StatusCode::Ok; } + fn set_event(&mut self, event: E) + where + E: Eq + Hash + Debug + Clone + Display, + { + init_sdk(); + self.context().request = Some(ModuleRequest::new(event)); + } + fn set_payload

(&mut self, payload: P) where P: ToBytes, { let bytes = payload.into_bytes().unwrap(); - let mut module_request = self.context().request.take().unwrap(); + let module_request = self.context().request.take().unwrap(); self.context().request = Some(module_request.payload(bytes)); } @@ -173,4 +162,23 @@ pub trait TesterTrait { .unwrap() .into_inner() } + + fn login(&self) -> UserDetail { + init_sdk(); + let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut)); + let request = SignInRequest { + email: valid_email(), + password: valid_password(), + }; + + let mut tester = Tester::::new(SignIn); + tester.set_request(request); + tester.sync_send(); + tester.parse::() + } + + fn logout(&self) { + init_sdk(); + let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut)); + } } diff --git a/rust-lib/flowy-user/tests/event/helper.rs b/rust-lib/flowy-user/tests/event/helper.rs index 692d6c2d42..c415725bf6 100644 --- a/rust-lib/flowy-user/tests/event/helper.rs +++ b/rust-lib/flowy-user/tests/event/helper.rs @@ -1,7 +1,4 @@ -use flowy_test::prelude::TestBuilder; -use flowy_user::errors::UserError; - -pub type UserTestBuilder = TestBuilder; +pub use flowy_test::builder::UserTestBuilder; pub(crate) fn invalid_email_test_case() -> Vec { // https://gist.github.com/cjaoude/fd9910626629b53c4d25 diff --git a/rust-lib/flowy-user/tests/event/sign_in_test.rs b/rust-lib/flowy-user/tests/event/sign_in_test.rs index 67817f874c..2d2793354d 100644 --- a/rust-lib/flowy-user/tests/event/sign_in_test.rs +++ b/rust-lib/flowy-user/tests/event/sign_in_test.rs @@ -1,5 +1,4 @@ use crate::helper::*; - use flowy_user::{errors::UserErrorCode, event::UserEvent::*, prelude::*}; use serial_test::*; diff --git a/rust-lib/flowy-user/tests/event/sign_up_test.rs b/rust-lib/flowy-user/tests/event/sign_up_test.rs index e08b5dd061..9a1427491d 100644 --- a/rust-lib/flowy-user/tests/event/sign_up_test.rs +++ b/rust-lib/flowy-user/tests/event/sign_up_test.rs @@ -1,5 +1,4 @@ use crate::helper::*; - use flowy_user::{errors::*, event::UserEvent::*, prelude::*}; use serial_test::*; diff --git a/rust-lib/flowy-workspace/tests/event/helper.rs b/rust-lib/flowy-workspace/tests/event/helper.rs index c4ecbb3435..9631890bfa 100644 --- a/rust-lib/flowy-workspace/tests/event/helper.rs +++ b/rust-lib/flowy-workspace/tests/event/helper.rs @@ -1,7 +1,4 @@ -use flowy_test::prelude::TestBuilder; -use flowy_workspace::errors::WorkspaceError; - -pub type WorkspaceTestBuilder = TestBuilder; +pub use flowy_test::builder::WorkspaceTestBuilder; pub(crate) fn invalid_workspace_name_test_case() -> Vec { vec!["", "1234".repeat(100).as_str()]