From 687ead563f15d31a2ef0f2b58a5a124e62bfc814 Mon Sep 17 00:00:00 2001 From: appflowy Date: Wed, 14 Jul 2021 08:23:35 +0800 Subject: [PATCH] generic the EventTester with specify error type --- rust-lib/flowy-test/src/lib.rs | 26 ++++++++++++++++--- rust-lib/flowy-user/tests/event/helper.rs | 5 ++++ .../flowy-user/tests/event/sign_in_test.rs | 12 ++++----- .../flowy-user/tests/event/sign_up_test.rs | 12 ++++----- .../tests/event/user_status_test.rs | 10 +++---- .../flowy-workspace/tests/event/helper.rs | 4 +++ rust-lib/flowy-workspace/tests/event/main.rs | 1 + .../tests/event/workspace_test.rs | 7 ++--- 8 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 rust-lib/flowy-workspace/tests/event/helper.rs diff --git a/rust-lib/flowy-test/src/lib.rs b/rust-lib/flowy-test/src/lib.rs index 3a8a831c14..741597e01a 100644 --- a/rust-lib/flowy-test/src/lib.rs +++ b/rust-lib/flowy-test/src/lib.rs @@ -5,6 +5,7 @@ use std::{ fmt::{Debug, Display}, fs, hash::Hash, + marker::PhantomData, path::PathBuf, sync::Once, thread, @@ -42,13 +43,17 @@ fn root_dir() -> String { root_dir } -pub struct EventTester { +pub struct EventTester { inner_request: Option, assert_status_code: Option, response: Option, + phantom: PhantomData, } -impl EventTester { +impl EventTester +where + ErrType: FromBytes + Debug, +{ pub fn new(event: E) -> Self where E: Eq + Hash + Debug + Clone + Display, @@ -63,6 +68,7 @@ impl EventTester { inner_request: Some(ModuleRequest::new(event)), assert_status_code: None, response: None, + phantom: PhantomData, } } @@ -111,10 +117,22 @@ impl EventTester { { let response = self.response.unwrap(); if response.status_code == StatusCode::Err { - dbg!(&response); + let error = >::try_from(response.payload) + .unwrap() + .into_inner(); + dbg!(&error); + panic!("") + } else { + >::try_from(response.payload).unwrap().into_inner() } + } - >::try_from(response.payload).unwrap().into_inner() + pub fn error(self) -> ErrType { + let response = self.response.unwrap(); + assert_eq!(response.status_code, StatusCode::Err); + >::try_from(response.payload) + .unwrap() + .into_inner() } } diff --git a/rust-lib/flowy-user/tests/event/helper.rs b/rust-lib/flowy-user/tests/event/helper.rs index 0a35e22008..bc1a6503f1 100644 --- a/rust-lib/flowy-user/tests/event/helper.rs +++ b/rust-lib/flowy-user/tests/event/helper.rs @@ -1,3 +1,8 @@ +use flowy_test::EventTester; +use flowy_user::errors::UserError; + +pub type UserEventTester = EventTester; + pub(crate) fn invalid_email_test_case() -> Vec { // https://gist.github.com/cjaoude/fd9910626629b53c4d25 vec![ 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 4fee57f93c..3edd5a5011 100644 --- a/rust-lib/flowy-user/tests/event/sign_in_test.rs +++ b/rust-lib/flowy-user/tests/event/sign_in_test.rs @@ -10,13 +10,13 @@ use serial_test::*; #[test] #[serial] fn sign_in_success() { - let _ = EventTester::new(SignOut).sync_send(); + let _ = UserEventTester::new(SignOut).sync_send(); let request = SignInRequest { email: valid_email(), password: valid_password(), }; - let response = EventTester::new(SignIn) + let response = UserEventTester::new(SignIn) .request(request) .sync_send() .parse::(); @@ -32,10 +32,10 @@ fn sign_in_with_invalid_email() { }; assert_eq!( - EventTester::new(SignIn) + UserEventTester::new(SignIn) .request(request) .sync_send() - .parse::() + .error() .code, UserErrorCode::EmailInvalid ); @@ -51,10 +51,10 @@ fn sign_in_with_invalid_password() { }; assert_eq!( - EventTester::new(SignIn) + UserEventTester::new(SignIn) .request(request) .sync_send() - .parse::() + .error() .code, UserErrorCode::PasswordInvalid ); 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 f17ef9bdd0..23efc4fb60 100644 --- a/rust-lib/flowy-user/tests/event/sign_up_test.rs +++ b/rust-lib/flowy-user/tests/event/sign_up_test.rs @@ -6,14 +6,14 @@ use serial_test::*; #[test] #[serial] fn sign_up_success() { - let _ = EventTester::new(SignOut).sync_send(); + let _ = UserEventTester::new(SignOut).sync_send(); let request = SignUpRequest { email: valid_email(), name: valid_name(), password: valid_password(), }; - let _response = EventTester::new(SignUp).request(request).sync_send(); + let _response = UserEventTester::new(SignUp).request(request).sync_send(); // .parse::(); // dbg!(&response); } @@ -28,10 +28,10 @@ fn sign_up_with_invalid_email() { }; assert_eq!( - EventTester::new(SignUp) + UserEventTester::new(SignUp) .request(request) .sync_send() - .parse::() + .error() .code, UserErrorCode::EmailInvalid ); @@ -47,10 +47,10 @@ fn sign_up_with_invalid_password() { }; assert_eq!( - EventTester::new(SignUp) + UserEventTester::new(SignUp) .request(request) .sync_send() - .parse::() + .error() .code, UserErrorCode::PasswordInvalid ); diff --git a/rust-lib/flowy-user/tests/event/user_status_test.rs b/rust-lib/flowy-user/tests/event/user_status_test.rs index c151180d3d..102a7225c3 100644 --- a/rust-lib/flowy-user/tests/event/user_status_test.rs +++ b/rust-lib/flowy-user/tests/event/user_status_test.rs @@ -6,8 +6,8 @@ use serial_test::*; #[should_panic] #[serial] fn user_status_not_found_before_login() { - let _ = EventTester::new(SignOut).sync_send(); - let _ = EventTester::new(GetStatus) + let _ = UserEventTester::new(SignOut).sync_send(); + let _ = UserEventTester::new(GetStatus) .sync_send() .parse::(); } @@ -15,19 +15,19 @@ fn user_status_not_found_before_login() { #[test] #[serial] fn user_status_did_found_after_login() { - let _ = EventTester::new(SignOut).sync_send(); + let _ = UserEventTester::new(SignOut).sync_send(); let request = SignInRequest { email: valid_email(), password: valid_password(), }; - let response = EventTester::new(SignIn) + let response = UserEventTester::new(SignIn) .request(request) .sync_send() .parse::(); dbg!(&response); - let _ = EventTester::new(GetStatus) + let _ = UserEventTester::new(GetStatus) .sync_send() .parse::(); } diff --git a/rust-lib/flowy-workspace/tests/event/helper.rs b/rust-lib/flowy-workspace/tests/event/helper.rs new file mode 100644 index 0000000000..2aa50ccba6 --- /dev/null +++ b/rust-lib/flowy-workspace/tests/event/helper.rs @@ -0,0 +1,4 @@ +use flowy_test::EventTester; +use flowy_workspace::errors::WorkspaceError; + +pub type WorkspaceEventTester = EventTester; diff --git a/rust-lib/flowy-workspace/tests/event/main.rs b/rust-lib/flowy-workspace/tests/event/main.rs index 15f76da505..190cfd57f1 100644 --- a/rust-lib/flowy-workspace/tests/event/main.rs +++ b/rust-lib/flowy-workspace/tests/event/main.rs @@ -1 +1,2 @@ +mod helper; mod workspace_test; diff --git a/rust-lib/flowy-workspace/tests/event/workspace_test.rs b/rust-lib/flowy-workspace/tests/event/workspace_test.rs index 8ba648d074..44183b642e 100644 --- a/rust-lib/flowy-workspace/tests/event/workspace_test.rs +++ b/rust-lib/flowy-workspace/tests/event/workspace_test.rs @@ -1,4 +1,4 @@ -use flowy_test::EventTester; +use crate::helper::WorkspaceEventTester; use flowy_workspace::{ entities::workspace::{CreateWorkspaceRequest, WorkspaceDetail}, event::WorkspaceEvent::*, @@ -6,13 +6,14 @@ use flowy_workspace::{ }; #[test] +#[should_panic] fn workspace_create_test() { let request = CreateWorkspaceRequest { - name: "123workspace".to_owned(), + name: "".to_owned(), desc: "".to_owned(), }; - let response = EventTester::new(CreateWorkspace) + let response = WorkspaceEventTester::new(CreateWorkspace) .request(request) .sync_send() .parse::();