add test script

This commit is contained in:
appflowy 2021-09-04 09:00:15 +08:00
parent d27e2b9475
commit a6346dffae
20 changed files with 86 additions and 68 deletions

View File

@ -1,6 +1,7 @@
extend = [ extend = [
{ path = "scripts/makefile/desktop.toml" }, { path = "scripts/makefile/desktop.toml" },
{ path = "scripts/makefile/protobuf.toml" }, { path = "scripts/makefile/protobuf.toml" },
{ path = "scripts/makefile/tests.toml" },
] ]
[env] [env]

View File

@ -1,5 +1,5 @@
use crate::helper::*; use crate::helper::*;
use flowy_test::builder::{TestBuilder, UserTestBuilder}; use flowy_test::builder::UserTestBuilder;
#[test] #[test]
fn file_create_test() { fn file_create_test() {

View File

@ -1,4 +1,4 @@
use flowy_test::builder::{AnnieTestBuilder, DocTestBuilder, TestBuilder}; use flowy_test::builder::DocTestBuilder;
use flowy_document::{entities::doc::*, event::EditorEvent::*}; use flowy_document::{entities::doc::*, event::EditorEvent::*};
use flowy_infra::uuid; use flowy_infra::uuid;

View File

@ -21,4 +21,12 @@ tokio = { version = "1", features = ["full"]}
futures-util = "0.3.15" futures-util = "0.3.15"
thread-id = "3.3.0" thread-id = "3.3.0"
log = "0.4" log = "0.4"
bytes = "1.0" bytes = "1.0"
[dev-dependencies]
quickcheck = "0.9.2"
quickcheck_macros = "0.9.1"
fake = "~2.3.0"
claim = "0.4.0"
futures = "0.3.15"
serial_test = "0.5.1"

View File

@ -27,7 +27,7 @@ pub fn root_dir() -> String {
pub fn random_email() -> String { format!("{}@appflowy.io", uuid()) } pub fn random_email() -> String { format!("{}@appflowy.io", uuid()) }
pub fn login_email() -> String { "annie@appflowy.io".to_string() } pub fn login_email() -> String { "annie2@appflowy.io".to_string() }
pub fn login_password() -> String { "HelloWorld!123".to_string() } pub fn login_password() -> String { "HelloWorld!123".to_string() }

View File

@ -101,7 +101,7 @@ pub trait TesterTrait {
fn sign_up(&self) -> (UserDetail, String) { fn sign_up(&self) -> (UserDetail, String) {
let password = login_password(); let password = login_password();
let payload = SignUpRequest { let payload = SignUpRequest {
email: random_email(), email: login_email(),
name: "app flowy".to_string(), name: "app flowy".to_string(),
password: password.clone(), password: password.clone(),
} }

View File

@ -49,4 +49,4 @@ futures = "0.3.15"
serial_test = "0.5.1" serial_test = "0.5.1"
[features] [features]
http_server = [] mock_server = []

View File

@ -20,9 +20,9 @@ pub trait UserServerAPI {
} }
pub(crate) fn construct_user_server() -> Arc<dyn UserServerAPI + Send + Sync> { pub(crate) fn construct_user_server() -> Arc<dyn UserServerAPI + Send + Sync> {
if cfg!(feature = "http_server") { if cfg!(feature = "mock_server") {
Arc::new(UserServer {})
} else {
Arc::new(UserServerMock {}) Arc::new(UserServerMock {})
} else {
Arc::new(UserServer {})
} }
} }

View File

@ -83,22 +83,16 @@ impl UserSession {
} }
pub async fn sign_up(&self, params: SignUpParams) -> Result<UserDetail, UserError> { pub async fn sign_up(&self, params: SignUpParams) -> Result<UserDetail, UserError> {
// if self.is_login(&params.email) { if self.is_login(&params.email) {
// self.user_detail().await self.user_detail().await
// } else { } else {
// let resp = self.server.sign_up(params).await?; let resp = self.server.sign_up(params).await?;
// let session = Session::new(&resp.user_id, &resp.token, &resp.email); let session = Session::new(&resp.user_id, &resp.token, &resp.email);
// let _ = self.set_session(Some(session))?; let _ = self.set_session(Some(session))?;
// let user_table = self.save_user(resp.into()).await?; let user_table = self.save_user(resp.into()).await?;
// let user_detail = UserDetail::from(user_table); let user_detail = UserDetail::from(user_table);
// Ok(user_detail) Ok(user_detail)
// } }
let resp = self.server.sign_up(params).await?;
let session = Session::new(&resp.user_id, &resp.token, &resp.email);
let _ = self.set_session(Some(session))?;
let user_table = self.save_user(resp.into()).await?;
let user_detail = UserDetail::from(user_table);
Ok(user_detail)
} }
pub async fn sign_out(&self) -> Result<(), UserError> { pub async fn sign_out(&self) -> Result<(), UserError> {
@ -218,13 +212,6 @@ pub async fn update_user(_server: Server, pool: Arc<ConnectionPool>, params: Upd
Ok(()) Ok(())
} }
pub fn current_user_id() -> Result<String, UserError> {
match KV::get_str(SESSION_CACHE_KEY) {
None => Err(ErrorBuilder::new(ErrorCode::UserNotLoginYet).build()),
Some(user_id) => Ok(user_id),
}
}
impl UserDatabaseConnection for UserSession { impl UserDatabaseConnection for UserSession {
fn get_connection(&self) -> Result<DBConnection, String> { self.get_db_connection().map_err(|e| format!("{:?}", e)) } fn get_connection(&self) -> Result<DBConnection, String> { self.get_db_connection().map_err(|e| format!("{:?}", e)) }
} }

View File

@ -44,7 +44,6 @@ fn sign_up_with_invalid_password() {
#[serial] #[serial]
fn sign_in_success() { fn sign_in_success() {
let context = UserTestBuilder::new().sign_up(); let context = UserTestBuilder::new().sign_up();
let _ = UserTestBuilder::new().event(SignOut).sync_send(); let _ = UserTestBuilder::new().event(SignOut).sync_send();
let request = SignInRequest { let request = SignInRequest {

View File

@ -35,5 +35,6 @@ bytes = { version = "1.0" }
flowy-test = { path = "../flowy-test" } flowy-test = { path = "../flowy-test" }
serial_test = "0.5.1" serial_test = "0.5.1"
[features] [features]
http_server = [] mock_server = []

View File

@ -39,11 +39,6 @@ pub(crate) async fn open_workspace(
controller: Unit<Arc<WorkspaceController>>, controller: Unit<Arc<WorkspaceController>>,
) -> DataResult<Workspace, WorkspaceError> { ) -> DataResult<Workspace, WorkspaceError> {
let params: QueryWorkspaceParams = data.into_inner().try_into()?; let params: QueryWorkspaceParams = data.into_inner().try_into()?;
match params.workspace_id { let workspaces = controller.open_workspace(params).await?;
None => Err(ErrorBuilder::new(ErrorCode::WorkspaceIdInvalid).build()), data_result(workspaces)
Some(workspace_id) => {
let workspaces = controller.open_workspace(&workspace_id).await?;
data_result(workspaces)
},
}
} }

View File

@ -51,9 +51,9 @@ pub trait WorkspaceServerAPI {
} }
pub(crate) fn construct_workspace_server() -> Arc<dyn WorkspaceServerAPI + Send + Sync> { pub(crate) fn construct_workspace_server() -> Arc<dyn WorkspaceServerAPI + Send + Sync> {
if cfg!(feature = "http_server") { if cfg!(feature = "mock_server") {
Arc::new(WorkspaceServer {})
} else {
Arc::new(WorkspaceServerMock {}) Arc::new(WorkspaceServerMock {})
} else {
Arc::new(WorkspaceServer {})
} }
} }

View File

@ -60,24 +60,29 @@ impl WorkspaceController {
Ok(()) Ok(())
} }
pub(crate) async fn open_workspace(&self, workspace_id: &str) -> Result<Workspace, WorkspaceError> { pub(crate) async fn open_workspace(&self, params: QueryWorkspaceParams) -> Result<Workspace, WorkspaceError> {
let user_id = self.user.user_id()?; let user_id = self.user.user_id()?;
let result = self.read_workspace_table(Some(workspace_id.to_owned()), user_id).await?; if let Some(workspace_id) = params.workspace_id.clone() {
self.read_workspaces_on_server(params.clone());
match result.first() { let result = self.read_workspace_table(Some(workspace_id), user_id)?;
None => Err(ErrorBuilder::new(ErrorCode::RecordNotFound).build()), match result.first() {
Some(workspace_table) => { None => Err(ErrorBuilder::new(ErrorCode::RecordNotFound).build()),
let workspace: Workspace = workspace_table.clone().into(); Some(workspace_table) => {
set_current_workspace(&workspace.id); let workspace: Workspace = workspace_table.clone().into();
Ok(workspace) set_current_workspace(&workspace.id);
}, Ok(workspace)
},
}
} else {
return Err(ErrorBuilder::new(ErrorCode::WorkspaceIdInvalid)
.msg("Opened workspace id should not be empty")
.build());
} }
} }
pub(crate) async fn read_workspaces(&self, params: QueryWorkspaceParams) -> Result<RepeatedWorkspace, WorkspaceError> { pub(crate) async fn read_workspaces(&self, params: QueryWorkspaceParams) -> Result<RepeatedWorkspace, WorkspaceError> {
self.read_workspaces_on_server(params.clone());
let user_id = self.user.user_id()?; let user_id = self.user.user_id()?;
let workspace_tables = self.read_workspace_table(params.workspace_id.clone(), user_id).await?; let workspace_tables = self.read_workspace_table(params.workspace_id.clone(), user_id)?;
let mut workspaces = vec![]; let mut workspaces = vec![];
for table in workspace_tables { for table in workspace_tables {
let apps = self.read_apps(&table.id).await?; let apps = self.read_apps(&table.id).await?;
@ -122,20 +127,11 @@ impl WorkspaceController {
Ok(apps) Ok(apps)
} }
fn read_workspace_table( fn read_workspace_table(&self, workspace_id: Option<String>, user_id: String) -> Result<Vec<WorkspaceTable>, WorkspaceError> {
&self,
workspace_id: Option<String>,
user_id: String,
) -> DispatchFuture<Result<Vec<WorkspaceTable>, WorkspaceError>> {
let sql = self.sql.clone(); let sql = self.sql.clone();
let workspace_id = workspace_id.to_owned(); let workspace_id = workspace_id.to_owned();
DispatchFuture { let workspace = sql.read_workspaces(workspace_id, &user_id)?;
fut: Box::pin(async move { Ok(workspace)
let workspace = sql.read_workspaces(workspace_id, &user_id)?;
// TODO: fetch workspace from remote server
Ok(workspace)
}),
}
} }
} }

View File

@ -0,0 +1,31 @@
[tasks.test_local]
category = "Build"
dependencies = ["rm_cache"]
description = "Build desktop targets."
script = '''
cd rust-lib
cargo test --features "flowy-workspace/mock_server","flowy-user/mock_server"
'''
[tasks.test_remote]
dependencies = ["rm_cache", "run_server"]
[tasks.run_test_remote_command]
script = """
cd rust-lib
cargo test
"""
[tasks.run_server]
script = """
cd backend
cargo run
"""
[tasks.rm_cache]
script = """
rm -rf rust-lib/flowy-test/temp
"""