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

145 lines
3.7 KiB
Rust
Raw Normal View History

2021-09-04 08:53:58 +00:00
use flowy_user::entities::UserProfile;
use lib_dispatch::prelude::{EventDispatch, EventResponse, FromBytes, ModuleRequest, StatusCode, ToBytes};
2021-07-16 15:18:12 +00:00
use std::{
fmt::{Debug, Display},
hash::Hash,
};
use crate::FlowyTestSDK;
use lib_dispatch::prelude::*;
use flowy_sdk::*;
use flowy_user::errors::UserError;
2021-07-17 00:24:17 +00:00
use flowy_workspace::errors::WorkspaceError;
use std::{convert::TryFrom, marker::PhantomData, sync::Arc};
2021-07-17 00:24:17 +00:00
2021-09-11 06:26:30 +00:00
pub type FlowyWorkspaceTest = Builder<WorkspaceError>;
impl FlowyWorkspaceTest {
2021-09-04 07:12:53 +00:00
pub fn new(sdk: FlowyTestSDK) -> Self { Builder::test(TestContext::new(sdk)) }
}
pub type UserTest = Builder<UserError>;
impl UserTest {
2021-09-04 07:12:53 +00:00
pub fn new(sdk: FlowyTestSDK) -> Self { Builder::test(TestContext::new(sdk)) }
2021-09-04 08:53:58 +00:00
pub fn user_profile(&self) -> &Option<UserProfile> { &self.user_profile }
}
2021-09-04 07:12:53 +00:00
#[derive(Clone)]
pub struct Builder<E> {
context: TestContext,
2021-09-04 08:53:58 +00:00
user_profile: Option<UserProfile>,
2021-09-04 07:12:53 +00:00
err_phantom: PhantomData<E>,
}
2021-09-04 07:12:53 +00:00
impl<E> Builder<E>
where
2021-09-04 07:12:53 +00:00
E: FromBytes + Debug,
{
2021-09-04 07:12:53 +00:00
pub(crate) fn test(context: TestContext) -> Self {
Self {
context,
2021-09-04 08:53:58 +00:00
user_profile: None,
2021-09-04 07:12:53 +00:00
err_phantom: PhantomData,
}
}
2021-09-04 07:12:53 +00:00
pub fn request<P>(mut self, payload: 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-09-04 07:12:53 +00:00
match payload.into_bytes() {
Ok(bytes) => {
let module_request = self.get_request();
self.context.request = Some(module_request.payload(bytes))
},
Err(e) => {
log::error!("Set payload failed: {:?}", e);
},
}
2021-07-16 15:18:12 +00:00
self
}
2021-09-04 07:12:53 +00:00
pub fn event<Event>(mut self, event: Event) -> Self
2021-07-16 15:18:12 +00:00
where
2021-09-04 07:12:53 +00:00
Event: Eq + Hash + Debug + Clone + Display,
2021-07-16 15:18:12 +00:00
{
2021-09-04 07:12:53 +00:00
self.context.request = Some(ModuleRequest::new(event));
2021-07-16 15:18:12 +00:00
self
}
pub fn sync_send(mut self) -> Self {
2021-09-04 07:12:53 +00:00
let request = self.get_request();
let resp = EventDispatch::sync_send(self.dispatch(), request);
self.context.response = Some(resp);
2021-07-16 15:18:12 +00:00
self
}
pub async fn async_send(mut self) -> Self {
let request = self.get_request();
let resp = EventDispatch::async_send(self.dispatch(), request).await;
self.context.response = Some(resp);
self
}
2021-09-04 07:12:53 +00:00
pub fn parse<R>(self) -> R
2021-07-16 15:18:12 +00:00
where
R: FromBytes,
{
2021-09-04 07:12:53 +00:00
let response = self.get_response();
match response.parse::<R, E>() {
Ok(Ok(data)) => data,
Ok(Err(e)) => {
panic!("parse failed: {:?}", e)
},
Err(e) => panic!("Internal error: {:?}", e),
}
2021-07-16 15:18:12 +00:00
}
2021-09-04 07:12:53 +00:00
pub fn error(self) -> E {
let response = self.get_response();
assert_eq!(response.status_code, StatusCode::Err);
<Data<E>>::try_from(response.payload).unwrap().into_inner()
}
2021-07-16 15:18:12 +00:00
2021-09-04 07:12:53 +00:00
pub fn assert_error(self) -> Self {
// self.context.assert_error();
2021-07-16 15:18:12 +00:00
self
}
2021-09-04 07:12:53 +00:00
pub fn assert_success(self) -> Self {
// self.context.assert_success();
2021-07-16 15:18:12 +00:00
self
}
2021-09-04 07:12:53 +00:00
pub fn sdk(&self) -> FlowySDK { self.context.sdk.clone() }
fn dispatch(&self) -> Arc<EventDispatch> { self.context.sdk.dispatch() }
fn get_response(&self) -> EventResponse {
self.context
.response
.as_ref()
.expect("must call sync_send first")
.clone()
}
2021-09-04 07:12:53 +00:00
fn get_request(&mut self) -> ModuleRequest { self.context.request.take().expect("must call event first") }
2021-07-16 15:18:12 +00:00
}
2021-07-17 00:24:17 +00:00
2021-09-04 07:12:53 +00:00
#[derive(Clone)]
pub struct TestContext {
sdk: FlowyTestSDK,
request: Option<ModuleRequest>,
response: Option<EventResponse>,
2021-07-17 00:24:17 +00:00
}
2021-09-04 07:12:53 +00:00
impl TestContext {
pub fn new(sdk: FlowyTestSDK) -> Self {
2021-07-17 00:24:17 +00:00
Self {
2021-09-04 07:12:53 +00:00
sdk,
request: None,
response: None,
2021-07-17 00:24:17 +00:00
}
}
}