2021-07-08 13:23:44 +00:00
|
|
|
use flowy_dispatch::prelude::*;
|
2021-07-06 06:14:47 +00:00
|
|
|
pub use flowy_sdk::*;
|
2021-07-14 15:00:58 +00:00
|
|
|
use flowy_user::{
|
|
|
|
errors::UserError,
|
|
|
|
event::UserEvent::{SignIn, SignOut},
|
|
|
|
prelude::*,
|
|
|
|
};
|
2021-07-06 06:14:47 +00:00
|
|
|
use std::{
|
2021-07-12 05:53:32 +00:00
|
|
|
convert::TryFrom,
|
2021-07-06 06:14:47 +00:00
|
|
|
fmt::{Debug, Display},
|
|
|
|
fs,
|
|
|
|
hash::Hash,
|
2021-07-14 00:23:35 +00:00
|
|
|
marker::PhantomData,
|
2021-07-06 06:14:47 +00:00
|
|
|
path::PathBuf,
|
|
|
|
sync::Once,
|
2021-07-09 06:02:42 +00:00
|
|
|
thread,
|
2021-07-06 06:14:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub mod prelude {
|
2021-07-14 15:00:58 +00:00
|
|
|
pub use crate::Tester;
|
2021-07-08 13:23:44 +00:00
|
|
|
pub use flowy_dispatch::prelude::*;
|
2021-07-06 06:14:47 +00:00
|
|
|
pub use std::convert::TryFrom;
|
|
|
|
}
|
|
|
|
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
pub fn init_sdk() {
|
|
|
|
let root_dir = root_dir();
|
|
|
|
|
|
|
|
INIT.call_once(|| {
|
2021-07-14 13:12:52 +00:00
|
|
|
FlowySDK::new(&root_dir).construct();
|
2021-07-06 06:14:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn root_dir() -> String {
|
|
|
|
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
|
|
|
|
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or("./".to_owned());
|
|
|
|
let mut path_buf = fs::canonicalize(&PathBuf::from(&manifest_dir)).unwrap();
|
|
|
|
path_buf.pop(); // rust-lib
|
|
|
|
path_buf.push("flowy-test");
|
|
|
|
path_buf.push("temp");
|
|
|
|
path_buf.push("flowy");
|
|
|
|
|
|
|
|
let root_dir = path_buf.to_str().unwrap().to_string();
|
|
|
|
if !std::path::Path::new(&root_dir).exists() {
|
|
|
|
std::fs::create_dir_all(&root_dir).unwrap();
|
|
|
|
}
|
|
|
|
root_dir
|
|
|
|
}
|
|
|
|
|
2021-07-14 15:00:58 +00:00
|
|
|
pub struct TestBuilder<Error> {
|
|
|
|
login: Option<bool>,
|
|
|
|
inner: Option<Tester<Error>>,
|
|
|
|
pub user_detail: Option<UserDetail>,
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:00:58 +00:00
|
|
|
impl<Error> TestBuilder<Error>
|
|
|
|
where
|
|
|
|
Error: FromBytes + Debug,
|
|
|
|
{
|
|
|
|
pub fn new() -> Self {
|
|
|
|
TestBuilder::<Error> {
|
|
|
|
login: None,
|
|
|
|
inner: None,
|
|
|
|
user_detail: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn login(mut self) -> Self {
|
|
|
|
let user_detail = new_user_after_login();
|
|
|
|
self.user_detail = Some(user_detail);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn logout(self) -> Self {
|
|
|
|
init_sdk();
|
|
|
|
let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn event<E>(mut self, event: E) -> Self
|
|
|
|
where
|
|
|
|
E: Eq + Hash + Debug + Clone + Display,
|
|
|
|
{
|
|
|
|
self.inner = Some(Tester::<Error>::new(event));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn request<P>(mut self, request: P) -> Self
|
|
|
|
where
|
|
|
|
P: ToBytes,
|
|
|
|
{
|
|
|
|
let mut inner = self.inner.unwrap();
|
|
|
|
self.inner = Some(inner.request(request));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sync_send(mut self) -> Self {
|
|
|
|
let inner = self.inner.take().unwrap();
|
|
|
|
self.inner = Some(inner.sync_send());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse<R>(mut self) -> R
|
|
|
|
where
|
|
|
|
R: FromBytes,
|
|
|
|
{
|
|
|
|
let inner = self.inner.take().unwrap();
|
|
|
|
inner.parse::<R>()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error(mut self) -> Error {
|
|
|
|
let inner = self.inner.take().unwrap();
|
|
|
|
inner.error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Tester<Error> {
|
2021-07-09 06:02:42 +00:00
|
|
|
inner_request: Option<ModuleRequest>,
|
2021-07-06 06:14:47 +00:00
|
|
|
assert_status_code: Option<StatusCode>,
|
2021-07-06 13:20:53 +00:00
|
|
|
response: Option<EventResponse>,
|
2021-07-14 13:12:52 +00:00
|
|
|
err_phantom: PhantomData<Error>,
|
2021-07-14 15:00:58 +00:00
|
|
|
user_detail: Option<UserDetail>,
|
2021-07-06 06:14:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:00:58 +00:00
|
|
|
impl<Error> Tester<Error>
|
2021-07-14 00:23:35 +00:00
|
|
|
where
|
2021-07-14 13:12:52 +00:00
|
|
|
Error: FromBytes + Debug,
|
2021-07-14 00:23:35 +00:00
|
|
|
{
|
2021-07-06 06:14:47 +00:00
|
|
|
pub fn new<E>(event: E) -> Self
|
|
|
|
where
|
|
|
|
E: Eq + Hash + Debug + Clone + Display,
|
|
|
|
{
|
|
|
|
init_sdk();
|
2021-07-09 06:02:42 +00:00
|
|
|
log::trace!(
|
|
|
|
"{:?} thread started: thread_id= {}",
|
|
|
|
thread::current(),
|
|
|
|
thread_id::get()
|
|
|
|
);
|
2021-07-14 13:12:52 +00:00
|
|
|
|
2021-07-06 06:14:47 +00:00
|
|
|
Self {
|
2021-07-09 06:02:42 +00:00
|
|
|
inner_request: Some(ModuleRequest::new(event)),
|
2021-07-06 06:14:47 +00:00
|
|
|
assert_status_code: None,
|
2021-07-06 13:20:53 +00:00
|
|
|
response: None,
|
2021-07-14 13:12:52 +00:00
|
|
|
err_phantom: PhantomData,
|
2021-07-14 15:00:58 +00:00
|
|
|
user_detail: None,
|
2021-07-06 06:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 06:02:42 +00:00
|
|
|
pub fn request<P>(mut self, request: P) -> Self
|
2021-07-06 06:14:47 +00:00
|
|
|
where
|
|
|
|
P: ToBytes,
|
|
|
|
{
|
2021-07-09 06:02:42 +00:00
|
|
|
let mut inner_request = self.inner_request.take().unwrap();
|
|
|
|
let bytes = request.into_bytes().unwrap();
|
|
|
|
inner_request = inner_request.payload(bytes);
|
|
|
|
self.inner_request = Some(inner_request);
|
2021-07-06 06:14:47 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assert_status_code(mut self, status_code: StatusCode) -> Self {
|
|
|
|
self.assert_status_code = Some(status_code);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-07-09 06:02:42 +00:00
|
|
|
pub fn assert_error(mut self) -> Self {
|
|
|
|
self.assert_status_code = Some(StatusCode::Err);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-07-06 13:20:53 +00:00
|
|
|
pub async fn async_send(mut self) -> Self {
|
2021-07-07 14:24:26 +00:00
|
|
|
let resp =
|
2021-07-09 06:02:42 +00:00
|
|
|
EventDispatch::async_send(self.inner_request.take().unwrap(), |_| Box::pin(async {}))
|
|
|
|
.await;
|
2021-07-07 14:24:26 +00:00
|
|
|
|
2021-07-09 06:02:42 +00:00
|
|
|
check(&resp, &self.assert_status_code);
|
2021-07-06 13:20:53 +00:00
|
|
|
self.response = Some(resp);
|
|
|
|
self
|
2021-07-06 06:14:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 13:20:53 +00:00
|
|
|
pub fn sync_send(mut self) -> Self {
|
2021-07-09 06:02:42 +00:00
|
|
|
let resp = EventDispatch::sync_send(self.inner_request.take().unwrap());
|
|
|
|
check(&resp, &self.assert_status_code);
|
2021-07-06 13:20:53 +00:00
|
|
|
self.response = Some(resp);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse<R>(self) -> R
|
2021-07-06 06:14:47 +00:00
|
|
|
where
|
|
|
|
R: FromBytes,
|
|
|
|
{
|
2021-07-06 13:20:53 +00:00
|
|
|
let response = self.response.unwrap();
|
2021-07-11 07:33:19 +00:00
|
|
|
if response.status_code == StatusCode::Err {
|
2021-07-14 13:12:52 +00:00
|
|
|
let error = <Data<Error>>::try_from(response.payload)
|
2021-07-14 00:23:35 +00:00
|
|
|
.unwrap()
|
|
|
|
.into_inner();
|
|
|
|
dbg!(&error);
|
|
|
|
panic!("")
|
|
|
|
} else {
|
|
|
|
<Data<R>>::try_from(response.payload).unwrap().into_inner()
|
2021-07-11 07:33:19 +00:00
|
|
|
}
|
2021-07-14 00:23:35 +00:00
|
|
|
}
|
2021-07-11 13:54:55 +00:00
|
|
|
|
2021-07-14 13:12:52 +00:00
|
|
|
pub fn error(self) -> Error {
|
2021-07-14 00:23:35 +00:00
|
|
|
let response = self.response.unwrap();
|
|
|
|
assert_eq!(response.status_code, StatusCode::Err);
|
2021-07-14 13:12:52 +00:00
|
|
|
<Data<Error>>::try_from(response.payload)
|
2021-07-14 00:23:35 +00:00
|
|
|
.unwrap()
|
|
|
|
.into_inner()
|
2021-07-06 06:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-09 06:02:42 +00:00
|
|
|
|
|
|
|
fn check(response: &EventResponse, status_code: &Option<StatusCode>) {
|
|
|
|
if let Some(ref status_code) = status_code {
|
|
|
|
if &response.status_code != status_code {
|
|
|
|
eprintln!("{:#?}", response);
|
|
|
|
}
|
|
|
|
assert_eq!(&response.status_code, status_code)
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 15:00:58 +00:00
|
|
|
|
|
|
|
fn new_user_after_login() -> UserDetail {
|
|
|
|
init_sdk();
|
|
|
|
let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut));
|
|
|
|
let request = SignInRequest {
|
|
|
|
email: valid_email(),
|
|
|
|
password: valid_password(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_detail = Tester::<UserError>::new(SignIn)
|
|
|
|
.request(request)
|
|
|
|
.sync_send()
|
|
|
|
.parse::<UserDetail>();
|
|
|
|
|
|
|
|
user_detail
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn valid_email() -> String { "annie@appflowy.io".to_string() }
|
|
|
|
|
|
|
|
pub(crate) fn valid_password() -> String { "HelloWorld!123".to_string() }
|