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()));
|
2021-08-30 08:18:58 +00:00
|
|
|
builder.setup_default_workspace();
|
|
|
|
builder
|
|
|
|
}
|
2021-08-29 14:00:42 +00:00
|
|
|
|
2021-08-30 08:18:58 +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-08-30 14:44:17 +00:00
|
|
|
fn test(tester: Box<T>) -> Self {
|
2021-08-29 14:00:42 +00:00
|
|
|
Self {
|
|
|
|
tester,
|
|
|
|
user_detail: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:18:12 +00:00
|
|
|
pub fn login(mut self) -> Self {
|
2021-07-17 00:24:17 +00:00
|
|
|
let user_detail = self.tester.login();
|
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) {
|
2021-07-19 08:15:20 +00:00
|
|
|
let user_detail = self.tester.login_if_need();
|
|
|
|
self.user_detail = Some(user_detail);
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:18:12 +00:00
|
|
|
pub fn logout(self) -> Self {
|
2021-07-19 13:05:49 +00:00
|
|
|
// self.tester.logout();
|
2021-07-16 15:18:12 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|