AppFlowy/rust-lib/flowy-test/src/builder.rs

163 lines
3.7 KiB
Rust
Raw Normal View History

2021-07-17 00:24:17 +00:00
use flowy_dispatch::prelude::{FromBytes, ToBytes};
use flowy_user::entities::UserDetail;
2021-07-16 15:18:12 +00:00
use std::{
fmt::{Debug, Display},
hash::Hash,
};
2021-07-17 02:26:05 +00:00
use crate::{
2021-08-29 14:00:42 +00:00
helper::{create_default_workspace_if_need, valid_email},
2021-07-17 02:26:05 +00:00
tester::{TesterContext, TesterTrait},
};
2021-07-17 00:24:17 +00:00
use flowy_user::errors::UserError;
use flowy_workspace::errors::WorkspaceError;
2021-08-29 14:00:42 +00:00
use std::marker::PhantomData;
2021-07-17 00:24:17 +00:00
2021-08-31 15:01:46 +00:00
pub type AnnieTestBuilder = Builder<FlowyAnnie<WorkspaceError>>;
impl AnnieTestBuilder {
2021-07-17 00:24:17 +00:00
pub fn new() -> Self {
2021-08-31 15:01:46 +00:00
let mut builder = Builder::test(Box::new(FlowyAnnie::<WorkspaceError>::new()));
builder.setup_default_workspace();
builder
}
2021-08-29 14:00:42 +00:00
pub fn setup_default_workspace(&mut self) {
self.login_if_need();
let user_id = self.user_detail.as_ref().unwrap().id.clone();
2021-08-29 14:00:42 +00:00
let _ = create_default_workspace_if_need(&user_id);
2021-07-17 00:24:17 +00:00
}
2021-07-16 15:18:12 +00:00
}
2021-08-31 15:01:46 +00:00
pub type TestBuilder = Builder<RandomUserTester<UserError>>;
impl TestBuilder {
pub fn new() -> Self { Builder::test(Box::new(RandomUserTester::<UserError>::new())) }
2021-07-17 00:24:17 +00:00
}
2021-07-16 15:18:12 +00:00
2021-08-31 15:01:46 +00:00
pub struct Builder<T: TesterTrait> {
2021-07-17 00:24:17 +00:00
pub tester: Box<T>,
pub user_detail: Option<UserDetail>,
}
2021-08-31 15:01:46 +00:00
impl<T> Builder<T>
2021-07-17 00:24:17 +00:00
where
T: TesterTrait,
{
2021-09-03 04:44:48 +00:00
fn test(tester: Box<T>) -> Self { Self { tester, user_detail: None } }
2021-08-29 14:00:42 +00:00
2021-09-01 09:57:06 +00:00
pub fn sign_up(self) -> SignUpContext {
let (user_detail, password) = self.tester.sign_up();
2021-09-03 04:44:48 +00:00
SignUpContext { user_detail, password }
2021-09-01 09:57:06 +00:00
}
pub fn sign_in(mut self) -> Self {
let user_detail = self.tester.sign_in();
2021-07-16 15:18:12 +00:00
self.user_detail = Some(user_detail);
self
}
2021-08-30 14:44:17 +00:00
fn login_if_need(&mut self) {
let user_detail = self.tester.login_if_need();
self.user_detail = Some(user_detail);
}
2021-07-16 15:18:12 +00:00
2021-07-17 00:24:17 +00:00
pub fn request<P>(mut self, request: P) -> Self
2021-07-16 15:18:12 +00:00
where
2021-07-17 00:24:17 +00:00
P: ToBytes,
2021-07-16 15:18:12 +00:00
{
2021-07-17 00:24:17 +00:00
self.tester.set_payload(request);
2021-07-16 15:18:12 +00:00
self
}
2021-07-17 00:24:17 +00:00
pub fn event<E>(mut self, event: E) -> Self
2021-07-16 15:18:12 +00:00
where
2021-07-17 00:24:17 +00:00
E: Eq + Hash + Debug + Clone + Display,
2021-07-16 15:18:12 +00:00
{
2021-07-17 00:24:17 +00:00
self.tester.set_event(event);
2021-07-16 15:18:12 +00:00
self
}
pub fn sync_send(mut self) -> Self {
2021-07-17 00:24:17 +00:00
self.tester.sync_send();
2021-07-16 15:18:12 +00:00
self
}
pub fn parse<R>(mut self) -> R
where
R: FromBytes,
{
2021-07-17 00:24:17 +00:00
self.tester.parse::<R>()
2021-07-16 15:18:12 +00:00
}
2021-07-17 00:24:17 +00:00
pub fn error(mut self) -> <T as TesterTrait>::Error { self.tester.error() }
2021-07-16 15:18:12 +00:00
pub fn assert_error(mut self) -> Self {
2021-07-17 00:24:17 +00:00
self.tester.assert_error();
2021-07-16 15:18:12 +00:00
self
}
pub fn assert_success(mut self) -> Self {
2021-07-17 00:24:17 +00:00
self.tester.assert_success();
2021-07-16 15:18:12 +00:00
self
}
}
2021-07-17 00:24:17 +00:00
pub struct RandomUserTester<Error> {
context: TesterContext,
err_phantom: PhantomData<Error>,
}
impl<Error> RandomUserTester<Error>
where
Error: FromBytes + Debug,
{
pub fn new() -> Self {
Self {
context: TesterContext::default(),
err_phantom: PhantomData,
}
}
}
impl<Error> TesterTrait for RandomUserTester<Error>
where
Error: FromBytes + Debug,
{
type Error = Error;
2021-07-17 02:26:05 +00:00
fn mut_context(&mut self) -> &mut TesterContext { &mut self.context }
fn context(&self) -> &TesterContext { &self.context }
2021-07-17 00:24:17 +00:00
}
2021-08-31 15:01:46 +00:00
pub struct FlowyAnnie<Error> {
2021-07-17 00:24:17 +00:00
context: TesterContext,
err_phantom: PhantomData<Error>,
}
2021-08-31 15:01:46 +00:00
impl<Error> FlowyAnnie<Error>
2021-07-17 00:24:17 +00:00
where
Error: FromBytes + Debug,
{
pub fn new() -> Self {
Self {
2021-07-17 02:26:05 +00:00
context: TesterContext::new(valid_email()),
2021-07-17 00:24:17 +00:00
err_phantom: PhantomData,
}
}
}
2021-08-31 15:01:46 +00:00
impl<Error> TesterTrait for FlowyAnnie<Error>
2021-07-17 00:24:17 +00:00
where
Error: FromBytes + Debug,
{
type Error = Error;
2021-07-17 02:26:05 +00:00
fn mut_context(&mut self) -> &mut TesterContext { &mut self.context }
fn context(&self) -> &TesterContext { &self.context }
2021-07-17 00:24:17 +00:00
}
2021-09-01 09:57:06 +00:00
pub struct SignUpContext {
pub user_detail: UserDetail,
pub password: String,
}