2021-12-14 10:04:51 +00:00
|
|
|
use crate::FlowySDKTest;
|
|
|
|
use flowy_user::{entities::UserProfile, errors::FlowyError};
|
|
|
|
use lib_dispatch::prelude::{EventDispatcher, EventResponse, FromBytes, ModuleRequest, StatusCode, ToBytes, *};
|
2021-07-16 15:18:12 +00:00
|
|
|
use std::{
|
2021-12-14 10:04:51 +00:00
|
|
|
convert::TryFrom,
|
2021-07-16 15:18:12 +00:00
|
|
|
fmt::{Debug, Display},
|
|
|
|
hash::Hash,
|
2021-12-14 10:04:51 +00:00
|
|
|
marker::PhantomData,
|
|
|
|
sync::Arc,
|
2021-07-16 15:18:12 +00:00
|
|
|
};
|
|
|
|
|
2022-01-22 10:48:43 +00:00
|
|
|
pub type FolderEventBuilder = EventBuilder<FlowyError>;
|
|
|
|
impl FolderEventBuilder {
|
2021-12-08 09:33:22 +00:00
|
|
|
pub fn new(sdk: FlowySDKTest) -> Self { EventBuilder::test(TestContext::new(sdk)) }
|
2021-09-04 08:53:58 +00:00
|
|
|
pub fn user_profile(&self) -> &Option<UserProfile> { &self.user_profile }
|
2021-09-03 08:43:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 10:48:43 +00:00
|
|
|
pub type UserModuleEventBuilder = FolderEventBuilder;
|
2021-12-14 10:04:51 +00:00
|
|
|
|
2021-09-04 07:12:53 +00:00
|
|
|
#[derive(Clone)]
|
2021-12-08 09:33:22 +00:00
|
|
|
pub struct EventBuilder<E> {
|
2021-09-04 07:12:53 +00:00
|
|
|
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-03 08:43:03 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 09:33:22 +00:00
|
|
|
impl<E> EventBuilder<E>
|
2021-09-03 08:43:03 +00:00
|
|
|
where
|
2021-09-04 07:12:53 +00:00
|
|
|
E: FromBytes + Debug,
|
2021-09-03 08:43:03 +00:00
|
|
|
{
|
2021-12-08 09:33:22 +00:00
|
|
|
fn test(context: TestContext) -> Self {
|
2021-09-04 07:12:53 +00:00
|
|
|
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-03 08:43:03 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2021-12-04 03:26:17 +00:00
|
|
|
let resp = EventDispatcher::sync_send(self.dispatch(), request);
|
2021-09-04 07:12:53 +00:00
|
|
|
self.context.response = Some(resp);
|
2021-07-16 15:18:12 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-28 07:29:29 +00:00
|
|
|
pub async fn async_send(mut self) -> Self {
|
|
|
|
let request = self.get_request();
|
2021-12-04 03:26:17 +00:00
|
|
|
let resp = EventDispatcher::async_send(self.dispatch(), request).await;
|
2021-09-28 07:29:29 +00:00
|
|
|
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
|
|
|
|
2021-12-04 03:26:17 +00:00
|
|
|
fn dispatch(&self) -> Arc<EventDispatcher> { self.context.sdk.dispatcher() }
|
2021-09-04 07:12:53 +00:00
|
|
|
|
2021-09-28 07:29:29 +00:00
|
|
|
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 {
|
2021-12-08 09:33:22 +00:00
|
|
|
pub sdk: FlowySDKTest,
|
2021-09-04 07:12:53 +00:00
|
|
|
request: Option<ModuleRequest>,
|
|
|
|
response: Option<EventResponse>,
|
2021-07-17 00:24:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 07:12:53 +00:00
|
|
|
impl TestContext {
|
2021-12-08 09:33:22 +00:00
|
|
|
pub fn new(sdk: FlowySDKTest) -> 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|