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

124 lines
3.3 KiB
Rust
Raw Normal View History

2021-07-17 00:24:17 +00:00
use crate::{
2021-07-17 02:26:05 +00:00
helper::{random_valid_email, valid_password},
init_test_sdk,
2021-07-17 00:24:17 +00:00
};
2021-07-16 15:18:12 +00:00
use flowy_dispatch::prelude::*;
pub use flowy_sdk::*;
2021-07-17 00:24:17 +00:00
use flowy_user::{
errors::UserError,
event::UserEvent::{SignIn, SignOut},
prelude::*,
};
2021-07-16 15:18:12 +00:00
use std::{
convert::TryFrom,
fmt::{Debug, Display},
hash::Hash,
2021-07-17 02:26:05 +00:00
sync::Arc,
2021-07-16 15:18:12 +00:00
};
2021-07-17 02:26:05 +00:00
pub struct TesterContext {
request: Option<ModuleRequest>,
2021-07-16 15:18:12 +00:00
response: Option<EventResponse>,
2021-07-17 02:26:05 +00:00
status_code: StatusCode,
server: ArcFlowyServer,
user_email: String,
2021-07-16 15:18:12 +00:00
}
2021-07-17 02:26:05 +00:00
impl TesterContext {
pub fn new(email: String) -> Self {
let mut ctx = TesterContext::default();
ctx.user_email = email;
ctx
2021-07-16 15:18:12 +00:00
}
}
impl std::default::Default for TesterContext {
fn default() -> Self {
Self {
request: None,
status_code: StatusCode::Ok,
response: None,
2021-07-17 02:26:05 +00:00
server: Arc::new(FlowyServerMocker {}),
user_email: random_valid_email(),
2021-07-16 15:18:12 +00:00
}
}
}
pub trait TesterTrait {
type Error: FromBytes + Debug;
2021-07-17 02:26:05 +00:00
fn mut_context(&mut self) -> &mut TesterContext;
2021-07-16 15:18:12 +00:00
2021-07-17 02:26:05 +00:00
fn context(&self) -> &TesterContext;
2021-07-16 15:18:12 +00:00
2021-07-17 02:26:05 +00:00
fn assert_error(&mut self) { self.mut_context().status_code = StatusCode::Err; }
fn assert_success(&mut self) { self.mut_context().status_code = StatusCode::Ok; }
2021-07-16 15:18:12 +00:00
2021-07-17 00:24:17 +00:00
fn set_event<E>(&mut self, event: E)
where
E: Eq + Hash + Debug + Clone + Display,
{
2021-07-17 02:26:05 +00:00
init_test_sdk(self.context().server.clone());
self.mut_context().request = Some(ModuleRequest::new(event));
2021-07-17 00:24:17 +00:00
}
2021-07-16 15:18:12 +00:00
fn set_payload<P>(&mut self, payload: P)
where
P: ToBytes,
{
let bytes = payload.into_bytes().unwrap();
2021-07-17 02:26:05 +00:00
let module_request = self.mut_context().request.take().unwrap();
self.mut_context().request = Some(module_request.payload(bytes));
2021-07-16 15:18:12 +00:00
}
fn sync_send(&mut self) {
2021-07-17 02:26:05 +00:00
let resp = EventDispatch::sync_send(self.mut_context().request.take().unwrap());
self.mut_context().response = Some(resp);
2021-07-16 15:18:12 +00:00
}
fn parse<R>(&mut self) -> R
where
R: FromBytes,
{
2021-07-17 02:26:05 +00:00
let response = self.mut_context().response.clone().unwrap();
2021-07-16 15:18:12 +00:00
match response.parse::<R, Self::Error>() {
Ok(Ok(data)) => data,
Ok(Err(e)) => panic!("parse failed: {:?}", e),
Err(e) => panic!("Internal error: {:?}", e),
}
}
fn error(&mut self) -> Self::Error {
2021-07-17 02:26:05 +00:00
let response = self.mut_context().response.clone().unwrap();
2021-07-16 15:18:12 +00:00
assert_eq!(response.status_code, StatusCode::Err);
<Data<Self::Error>>::try_from(response.payload)
.unwrap()
.into_inner()
}
2021-07-17 00:24:17 +00:00
fn login(&self) -> UserDetail {
2021-07-17 02:26:05 +00:00
init_test_sdk(self.context().server.clone());
self.logout();
let payload = SignInRequest {
email: self.context().user_email.clone(),
2021-07-17 00:24:17 +00:00
password: valid_password(),
2021-07-17 02:26:05 +00:00
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(SignIn).payload(payload);
let user_detail = EventDispatch::sync_send(request)
.parse::<UserDetail, UserError>()
.unwrap()
.unwrap();
2021-07-17 00:24:17 +00:00
2021-07-17 02:26:05 +00:00
user_detail
2021-07-17 00:24:17 +00:00
}
fn logout(&self) {
2021-07-17 02:26:05 +00:00
init_test_sdk(self.context().server.clone());
2021-07-17 00:24:17 +00:00
let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut));
}
2021-07-16 15:18:12 +00:00
}