From fc498fbc8e5304a0ef9d9534f81e2c244a75726a Mon Sep 17 00:00:00 2001 From: appflowy Date: Mon, 18 Oct 2021 22:46:05 +0800 Subject: [PATCH] [client]: add test --- .../presentation/widgets/menu/menu.dart | 5 +--- backend/tests/api/workspace.rs | 19 +++++++++++-- backend/tests/helper.rs | 8 ++---- rust-lib/flowy-test/src/workspace.rs | 28 ++++++++++++------- .../src/entities/app/app_query.rs | 2 +- .../tests/workspace/app_test.rs | 8 +++--- .../tests/workspace/view_test.rs | 25 +++++++++++++++-- .../tests/workspace/workspace_test.rs | 6 ++-- 8 files changed, 68 insertions(+), 33 deletions(-) diff --git a/app_flowy/lib/workspace/presentation/widgets/menu/menu.dart b/app_flowy/lib/workspace/presentation/widgets/menu/menu.dart index e4376d88f1..fb82f53bfb 100644 --- a/app_flowy/lib/workspace/presentation/widgets/menu/menu.dart +++ b/app_flowy/lib/workspace/presentation/widgets/menu/menu.dart @@ -38,10 +38,7 @@ import 'widget/menu_trash.dart'; // │ │ └────────┘ // │ ┌────────┐ │ // └───│MenuApp │──┤ -// └────────┘ │ -// │ ┌──────────────┐ -// └─▶│AppListenBloc │ Receive view changes -// └──────────────┘ +// └────────┘ class HomeMenu extends StatelessWidget { final Function(HomeStackContext) pageContextChanged; diff --git a/backend/tests/api/workspace.rs b/backend/tests/api/workspace.rs index a8c4ac1169..d7708b6121 100644 --- a/backend/tests/api/workspace.rs +++ b/backend/tests/api/workspace.rs @@ -1,6 +1,7 @@ use crate::helper::*; use flowy_workspace::entities::{ app::{AppIdentifier, DeleteAppParams, UpdateAppParams}, + trash::{TrashIdentifier, TrashIdentifiers, TrashType}, view::{UpdateViewParams, ViewIdentifier}, workspace::{CreateWorkspaceParams, DeleteWorkspaceParams, QueryWorkspaceParams, UpdateWorkspaceParams}, }; @@ -173,11 +174,25 @@ async fn view_delete() { } #[actix_rt::test] -async fn view_delete_and_then_delete_the_trash_record() { +async fn trash_delete() { let test = ViewTest::new().await; test.server.create_view_trash(&test.view.id).await; - test.server.delete_view_trash(&test.view.id).await; + let identifier = TrashIdentifier { + id: test.view.id.clone(), + ty: TrashType::View, + }; + test.server.delete_view_trash(vec![identifier].into()).await; + + assert_eq!(test.server.read_trash().await.is_empty(), true); +} + +#[actix_rt::test] +async fn trash_delete_all() { + let test = ViewTest::new().await; + test.server.create_view_trash(&test.view.id).await; + + test.server.delete_view_trash(TrashIdentifiers::all()).await; assert_eq!(test.server.read_trash().await.is_empty(), true); } diff --git a/backend/tests/helper.rs b/backend/tests/helper.rs index 802b602aa6..896e2ee959 100644 --- a/backend/tests/helper.rs +++ b/backend/tests/helper.rs @@ -133,14 +133,10 @@ impl TestUserServer { .unwrap(); } - pub async fn delete_view_trash(&self, trash_id: &str) { + pub async fn delete_view_trash(&self, trash_identifiers: TrashIdentifiers) { let url = format!("{}/api/trash", self.http_addr()); - let identifier = TrashIdentifier { - id: trash_id.to_string(), - ty: TrashType::View, - }; - delete_trash_request(self.user_token(), vec![identifier].into(), &url) + delete_trash_request(self.user_token(), trash_identifiers, &url) .await .unwrap(); } diff --git a/rust-lib/flowy-test/src/workspace.rs b/rust-lib/flowy-test/src/workspace.rs index a00c575147..027e9fac1f 100644 --- a/rust-lib/flowy-test/src/workspace.rs +++ b/rust-lib/flowy-test/src/workspace.rs @@ -82,12 +82,17 @@ impl ViewTest { } } - pub async fn delete(&self) { - let request = DeleteViewRequest { - view_ids: vec![self.view.id.clone()], - }; + pub async fn delete_view(&self, view_ids: Vec) { + let request = DeleteViewRequest { view_ids }; delete_view(&self.sdk, request).await; } + + pub async fn delete_all_view(&self) { + FlowyWorkspaceTest::new(self.sdk.clone()) + .event(DeleteAll) + .async_send() + .await; + } } pub fn invalid_workspace_name_test_case() -> Vec { @@ -123,11 +128,12 @@ async fn open_workspace(sdk: &FlowyTestSDK, workspace_id: &str) { .await; } -pub fn read_workspace(sdk: &FlowyTestSDK, request: QueryWorkspaceRequest) -> Option { +pub async fn read_workspace(sdk: &FlowyTestSDK, request: QueryWorkspaceRequest) -> Option { let mut repeated_workspace = FlowyWorkspaceTest::new(sdk.clone()) .event(ReadWorkspaces) .request(request.clone()) - .sync_send() + .async_send() + .await .parse::(); let mut workspaces; @@ -162,7 +168,7 @@ pub async fn create_app(sdk: &FlowyTestSDK, name: &str, desc: &str, workspace_id app } -pub fn delete_app(sdk: &FlowyTestSDK, app_id: &str) { +pub async fn delete_app(sdk: &FlowyTestSDK, app_id: &str) { let delete_app_request = DeleteAppRequest { app_id: app_id.to_string(), }; @@ -170,7 +176,8 @@ pub fn delete_app(sdk: &FlowyTestSDK, app_id: &str) { FlowyWorkspaceTest::new(sdk.clone()) .event(DeleteApp) .request(delete_app_request) - .sync_send(); + .async_send() + .await; } pub async fn update_app(sdk: &FlowyTestSDK, request: UpdateAppRequest) { @@ -181,11 +188,12 @@ pub async fn update_app(sdk: &FlowyTestSDK, request: UpdateAppRequest) { .await; } -pub fn read_app(sdk: &FlowyTestSDK, request: QueryAppRequest) -> App { +pub async fn read_app(sdk: &FlowyTestSDK, request: QueryAppRequest) -> App { let app = FlowyWorkspaceTest::new(sdk.clone()) .event(ReadApp) .request(request) - .sync_send() + .async_send() + .await .parse::(); app diff --git a/rust-lib/flowy-workspace/src/entities/app/app_query.rs b/rust-lib/flowy-workspace/src/entities/app/app_query.rs index 2352f76b5e..899cfe5908 100644 --- a/rust-lib/flowy-workspace/src/entities/app/app_query.rs +++ b/rust-lib/flowy-workspace/src/entities/app/app_query.rs @@ -2,7 +2,7 @@ use crate::{entities::app::parser::AppId, errors::*}; use flowy_derive::ProtoBuf; use std::convert::TryInto; -#[derive(Default, ProtoBuf)] +#[derive(Default, ProtoBuf, Clone)] pub struct QueryAppRequest { #[pb(index = 1)] pub app_id: String, diff --git a/rust-lib/flowy-workspace/tests/workspace/app_test.rs b/rust-lib/flowy-workspace/tests/workspace/app_test.rs index 1914c823e1..d7e4fadd99 100644 --- a/rust-lib/flowy-workspace/tests/workspace/app_test.rs +++ b/rust-lib/flowy-workspace/tests/workspace/app_test.rs @@ -5,16 +5,16 @@ use flowy_workspace::entities::{app::QueryAppRequest, view::*}; #[should_panic] async fn app_delete() { let test = AppTest::new().await; - delete_app(&test.sdk, &test.app.id); + delete_app(&test.sdk, &test.app.id).await; let query = QueryAppRequest::new(&test.app.id); - let _ = read_app(&test.sdk, query); + let _ = read_app(&test.sdk, query).await; } #[tokio::test] async fn app_read() { let test = AppTest::new().await; let query = QueryAppRequest::new(&test.app.id); - let app_from_db = read_app(&test.sdk, query); + let app_from_db = read_app(&test.sdk, query).await; assert_eq!(app_from_db, test.app); } @@ -41,7 +41,7 @@ async fn app_create_with_view() { let view_b = create_view_with_request(&test.sdk, request_b).await; let query = QueryAppRequest::new(&test.app.id); - let view_from_db = read_app(&test.sdk, query); + let view_from_db = read_app(&test.sdk, query).await; assert_eq!(view_from_db.belongings[0], view_a); assert_eq!(view_from_db.belongings[1], view_b); diff --git a/rust-lib/flowy-workspace/tests/workspace/view_test.rs b/rust-lib/flowy-workspace/tests/workspace/view_test.rs index 0d51418df3..e4c947d4b8 100644 --- a/rust-lib/flowy-workspace/tests/workspace/view_test.rs +++ b/rust-lib/flowy-workspace/tests/workspace/view_test.rs @@ -1,8 +1,10 @@ use flowy_test::{workspace::*, FlowyTest}; use flowy_workspace::entities::{ + app::QueryAppRequest, trash::{TrashIdentifier, TrashType}, view::*, }; +use tokio::time::{sleep, Duration}; #[tokio::test] #[should_panic] @@ -11,18 +13,18 @@ async fn view_delete() { let _ = test.init_user(); let test = ViewTest::new(&test).await; - test.delete().await; + test.delete_view(vec![test.view.id.clone()]).await; let query = QueryViewRequest::new(&test.view.id); let _ = read_view(&test.sdk, query).await; } #[tokio::test] -async fn view_delete_and_putback() { +async fn view_delete_then_putback() { let test = FlowyTest::setup(); let _ = test.init_user(); let test = ViewTest::new(&test).await; - test.delete().await; + test.delete_view(vec![test.view.id.clone()]).await; putback_trash( &test.sdk, TrashIdentifier { @@ -37,6 +39,23 @@ async fn view_delete_and_putback() { assert_eq!(&view, &test.view); } +#[tokio::test] +async fn view_delete_all() { + let test = FlowyTest::setup(); + let _ = test.init_user(); + + let test = ViewTest::new(&test).await; + let _ = create_view(&test.sdk, &test.app.id).await; + let _ = create_view(&test.sdk, &test.app.id).await; + let query = QueryAppRequest::new(&test.app.id); + let app = read_app(&test.sdk, query.clone()).await; + assert_eq!(app.belongings.len(), 3); + test.delete_all_view().await; + + sleep(Duration::from_secs(1)).await; + assert_eq!(read_app(&test.sdk, query).await.belongings.len(), 0); +} + #[tokio::test] async fn view_open_doc() { let test = FlowyTest::setup(); diff --git a/rust-lib/flowy-workspace/tests/workspace/workspace_test.rs b/rust-lib/flowy-workspace/tests/workspace/workspace_test.rs index c3a54f5b15..d5bef1caf5 100644 --- a/rust-lib/flowy-workspace/tests/workspace/workspace_test.rs +++ b/rust-lib/flowy-workspace/tests/workspace/workspace_test.rs @@ -8,7 +8,7 @@ use flowy_workspace::{ #[tokio::test] async fn workspace_read_all() { let test = WorkspaceTest::new().await; - let workspace = read_workspace(&test.sdk, QueryWorkspaceRequest::new()).unwrap(); + let workspace = read_workspace(&test.sdk, QueryWorkspaceRequest::new()).await.unwrap(); assert_eq!(test.workspace, workspace); } @@ -16,7 +16,7 @@ async fn workspace_read_all() { async fn workspace_read() { let test = WorkspaceTest::new().await; let request = QueryWorkspaceRequest::new().workspace_id(&test.workspace.id); - let workspace = read_workspace(&test.sdk, request).unwrap(); + let workspace = read_workspace(&test.sdk, request).await.unwrap(); assert_eq!(test.workspace, workspace); } @@ -25,7 +25,7 @@ async fn workspace_create_with_apps() { let test = WorkspaceTest::new().await; let app = create_app(&test.sdk, "App A", "AppFlowy Github Project", &test.workspace.id).await; let request = QueryWorkspaceRequest::new().workspace_id(&test.workspace.id); - let workspace_from_db = read_workspace(&test.sdk, request).unwrap(); + let workspace_from_db = read_workspace(&test.sdk, request).await.unwrap(); assert_eq!(&app, workspace_from_db.apps.first_or_crash()); }