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},
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<ErrType> {
inner_request: Option<ModuleRequest>,
assert_status_code: Option<StatusCode>,
response: Option<EventResponse>,
phantom: PhantomData<ErrType>,
}
impl EventTester {
impl<ErrType> EventTester<ErrType>
where
ErrType: FromBytes + Debug,
{
pub fn new<E>(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 = <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> {
// https://gist.github.com/cjaoude/fd9910626629b53c4d25
vec![

View File

@ -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::<UserDetail>();
@ -32,10 +32,10 @@ fn sign_in_with_invalid_email() {
};
assert_eq!(
EventTester::new(SignIn)
UserEventTester::new(SignIn)
.request(request)
.sync_send()
.parse::<UserError>()
.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::<UserError>()
.error()
.code,
UserErrorCode::PasswordInvalid
);

View File

@ -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::<SignUpResponse>();
// 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::<UserError>()
.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::<UserError>()
.error()
.code,
UserErrorCode::PasswordInvalid
);

View File

@ -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::<UserDetail>();
}
@ -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::<UserDetail>();
dbg!(&response);
let _ = EventTester::new(GetStatus)
let _ = UserEventTester::new(GetStatus)
.sync_send()
.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;

View File

@ -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::<WorkspaceDetail>();