generic the EventTester with specify error type

This commit is contained in:
appflowy 2021-07-14 08:23:35 +08:00
parent 0e26706176
commit 687ead563f
8 changed files with 53 additions and 24 deletions

View File

@ -5,6 +5,7 @@ use std::{
fmt::{Debug, Display}, fmt::{Debug, Display},
fs, fs,
hash::Hash, hash::Hash,
marker::PhantomData,
path::PathBuf, path::PathBuf,
sync::Once, sync::Once,
thread, thread,
@ -42,13 +43,17 @@ fn root_dir() -> String {
root_dir root_dir
} }
pub struct EventTester { pub struct EventTester<ErrType> {
inner_request: Option<ModuleRequest>, inner_request: Option<ModuleRequest>,
assert_status_code: Option<StatusCode>, assert_status_code: Option<StatusCode>,
response: Option<EventResponse>, response: Option<EventResponse>,
phantom: PhantomData<ErrType>,
} }
impl EventTester { impl<ErrType> EventTester<ErrType>
where
ErrType: FromBytes + Debug,
{
pub fn new<E>(event: E) -> Self pub fn new<E>(event: E) -> Self
where where
E: Eq + Hash + Debug + Clone + Display, E: Eq + Hash + Debug + Clone + Display,
@ -63,6 +68,7 @@ impl EventTester {
inner_request: Some(ModuleRequest::new(event)), inner_request: Some(ModuleRequest::new(event)),
assert_status_code: None, assert_status_code: None,
response: None, response: None,
phantom: PhantomData,
} }
} }
@ -111,10 +117,22 @@ impl EventTester {
{ {
let response = self.response.unwrap(); let response = self.response.unwrap();
if response.status_code == StatusCode::Err { if response.status_code == StatusCode::Err {
dbg!(&response); let error = <Data<ErrType>>::try_from(response.payload)
.unwrap()
.into_inner();
dbg!(&error);
panic!("")
} else {
<Data<R>>::try_from(response.payload).unwrap().into_inner()
}
} }
<Data<R>>::try_from(response.payload).unwrap().into_inner() pub fn error(self) -> ErrType {
let response = self.response.unwrap();
assert_eq!(response.status_code, StatusCode::Err);
<Data<ErrType>>::try_from(response.payload)
.unwrap()
.into_inner()
} }
} }

View File

@ -1,3 +1,8 @@
use flowy_test::EventTester;
use flowy_user::errors::UserError;
pub type UserEventTester = EventTester<UserError>;
pub(crate) fn invalid_email_test_case() -> Vec<String> { pub(crate) fn invalid_email_test_case() -> Vec<String> {
// https://gist.github.com/cjaoude/fd9910626629b53c4d25 // https://gist.github.com/cjaoude/fd9910626629b53c4d25
vec![ vec![

View File

@ -10,13 +10,13 @@ use serial_test::*;
#[test] #[test]
#[serial] #[serial]
fn sign_in_success() { fn sign_in_success() {
let _ = EventTester::new(SignOut).sync_send(); let _ = UserEventTester::new(SignOut).sync_send();
let request = SignInRequest { let request = SignInRequest {
email: valid_email(), email: valid_email(),
password: valid_password(), password: valid_password(),
}; };
let response = EventTester::new(SignIn) let response = UserEventTester::new(SignIn)
.request(request) .request(request)
.sync_send() .sync_send()
.parse::<UserDetail>(); .parse::<UserDetail>();
@ -32,10 +32,10 @@ fn sign_in_with_invalid_email() {
}; };
assert_eq!( assert_eq!(
EventTester::new(SignIn) UserEventTester::new(SignIn)
.request(request) .request(request)
.sync_send() .sync_send()
.parse::<UserError>() .error()
.code, .code,
UserErrorCode::EmailInvalid UserErrorCode::EmailInvalid
); );
@ -51,10 +51,10 @@ fn sign_in_with_invalid_password() {
}; };
assert_eq!( assert_eq!(
EventTester::new(SignIn) UserEventTester::new(SignIn)
.request(request) .request(request)
.sync_send() .sync_send()
.parse::<UserError>() .error()
.code, .code,
UserErrorCode::PasswordInvalid UserErrorCode::PasswordInvalid
); );

View File

@ -6,14 +6,14 @@ use serial_test::*;
#[test] #[test]
#[serial] #[serial]
fn sign_up_success() { fn sign_up_success() {
let _ = EventTester::new(SignOut).sync_send(); let _ = UserEventTester::new(SignOut).sync_send();
let request = SignUpRequest { let request = SignUpRequest {
email: valid_email(), email: valid_email(),
name: valid_name(), name: valid_name(),
password: valid_password(), password: valid_password(),
}; };
let _response = EventTester::new(SignUp).request(request).sync_send(); let _response = UserEventTester::new(SignUp).request(request).sync_send();
// .parse::<SignUpResponse>(); // .parse::<SignUpResponse>();
// dbg!(&response); // dbg!(&response);
} }
@ -28,10 +28,10 @@ fn sign_up_with_invalid_email() {
}; };
assert_eq!( assert_eq!(
EventTester::new(SignUp) UserEventTester::new(SignUp)
.request(request) .request(request)
.sync_send() .sync_send()
.parse::<UserError>() .error()
.code, .code,
UserErrorCode::EmailInvalid UserErrorCode::EmailInvalid
); );
@ -47,10 +47,10 @@ fn sign_up_with_invalid_password() {
}; };
assert_eq!( assert_eq!(
EventTester::new(SignUp) UserEventTester::new(SignUp)
.request(request) .request(request)
.sync_send() .sync_send()
.parse::<UserError>() .error()
.code, .code,
UserErrorCode::PasswordInvalid UserErrorCode::PasswordInvalid
); );

View File

@ -6,8 +6,8 @@ use serial_test::*;
#[should_panic] #[should_panic]
#[serial] #[serial]
fn user_status_not_found_before_login() { fn user_status_not_found_before_login() {
let _ = EventTester::new(SignOut).sync_send(); let _ = UserEventTester::new(SignOut).sync_send();
let _ = EventTester::new(GetStatus) let _ = UserEventTester::new(GetStatus)
.sync_send() .sync_send()
.parse::<UserDetail>(); .parse::<UserDetail>();
} }
@ -15,19 +15,19 @@ fn user_status_not_found_before_login() {
#[test] #[test]
#[serial] #[serial]
fn user_status_did_found_after_login() { fn user_status_did_found_after_login() {
let _ = EventTester::new(SignOut).sync_send(); let _ = UserEventTester::new(SignOut).sync_send();
let request = SignInRequest { let request = SignInRequest {
email: valid_email(), email: valid_email(),
password: valid_password(), password: valid_password(),
}; };
let response = EventTester::new(SignIn) let response = UserEventTester::new(SignIn)
.request(request) .request(request)
.sync_send() .sync_send()
.parse::<UserDetail>(); .parse::<UserDetail>();
dbg!(&response); dbg!(&response);
let _ = EventTester::new(GetStatus) let _ = UserEventTester::new(GetStatus)
.sync_send() .sync_send()
.parse::<UserDetail>(); .parse::<UserDetail>();
} }

View File

@ -0,0 +1,4 @@
use flowy_test::EventTester;
use flowy_workspace::errors::WorkspaceError;
pub type WorkspaceEventTester = EventTester<WorkspaceError>;

View File

@ -1 +1,2 @@
mod helper;
mod workspace_test; mod workspace_test;

View File

@ -1,4 +1,4 @@
use flowy_test::EventTester; use crate::helper::WorkspaceEventTester;
use flowy_workspace::{ use flowy_workspace::{
entities::workspace::{CreateWorkspaceRequest, WorkspaceDetail}, entities::workspace::{CreateWorkspaceRequest, WorkspaceDetail},
event::WorkspaceEvent::*, event::WorkspaceEvent::*,
@ -6,13 +6,14 @@ use flowy_workspace::{
}; };
#[test] #[test]
#[should_panic]
fn workspace_create_test() { fn workspace_create_test() {
let request = CreateWorkspaceRequest { let request = CreateWorkspaceRequest {
name: "123workspace".to_owned(), name: "".to_owned(),
desc: "".to_owned(), desc: "".to_owned(),
}; };
let response = EventTester::new(CreateWorkspace) let response = WorkspaceEventTester::new(CreateWorkspace)
.request(request) .request(request)
.sync_send() .sync_send()
.parse::<WorkspaceDetail>(); .parse::<WorkspaceDetail>();