AppFlowy/rust-lib/flowy-sdk/tests/sdk/helper.rs

80 lines
1.8 KiB
Rust
Raw Normal View History

2021-06-30 15:11:27 +00:00
use flowy_sdk::module::build_modules;
pub use flowy_sdk::*;
use flowy_sys::prelude::*;
use std::{
fmt::{Debug, Display},
2021-07-03 06:14:10 +00:00
fs,
2021-06-30 15:11:27 +00:00
hash::Hash,
sync::Once,
};
static INIT: Once = Once::new();
2021-07-03 06:14:10 +00:00
pub fn init_sdk() {
let root_dir = root_dir();
2021-06-30 15:11:27 +00:00
2021-07-02 12:47:52 +00:00
INIT.call_once(|| {
2021-07-03 06:14:10 +00:00
FlowySDK::init_log(&root_dir);
2021-06-30 15:11:27 +00:00
});
2021-07-03 06:14:10 +00:00
FlowySDK::init(&root_dir);
}
2021-06-30 15:11:27 +00:00
2021-07-03 06:14:10 +00:00
fn root_dir() -> String {
let mut path = fs::canonicalize(".").unwrap();
path.push("tests/temp/flowy/");
let path_str = path.to_str().unwrap().to_string();
if !std::path::Path::new(&path).exists() {
std::fs::create_dir_all(path).unwrap();
}
path_str
2021-06-30 15:11:27 +00:00
}
2021-07-03 06:14:10 +00:00
pub struct EventTester {
request: DispatchRequest,
2021-06-30 15:11:27 +00:00
}
2021-07-03 06:14:10 +00:00
impl EventTester {
2021-07-03 14:24:02 +00:00
pub fn new<E>(event: E, payload: Payload) -> Self
2021-06-30 15:11:27 +00:00
where
E: Eq + Hash + Debug + Clone + Display,
{
2021-07-03 14:24:02 +00:00
init_sdk();
2021-06-30 15:11:27 +00:00
Self {
2021-07-03 14:24:02 +00:00
request: DispatchRequest::new(event, payload),
2021-06-30 15:11:27 +00:00
}
}
#[allow(dead_code)]
pub fn bytes_payload<T>(mut self, payload: T) -> Self
where
T: serde::Serialize,
{
let bytes: Vec<u8> = bincode::serialize(&payload).unwrap();
self.request = self.request.payload(Payload::Bytes(bytes));
self
}
#[allow(dead_code)]
pub fn protobuf_payload<T>(mut self, payload: T) -> Self
where
T: ::protobuf::Message,
{
let bytes: Vec<u8> = payload.write_to_bytes().unwrap();
self.request = self.request.payload(Payload::Bytes(bytes));
self
}
2021-07-03 06:14:10 +00:00
#[allow(dead_code)]
2021-07-02 12:47:52 +00:00
pub async fn async_send(self) -> EventResponse {
2021-07-03 06:14:10 +00:00
let resp = async_send(self.request).await;
2021-07-02 12:47:52 +00:00
dbg!(&resp);
resp
2021-06-30 15:11:27 +00:00
}
2021-07-03 06:14:10 +00:00
#[allow(dead_code)]
2021-07-02 12:47:52 +00:00
pub fn sync_send(self) -> EventResponse {
2021-07-03 06:14:10 +00:00
let resp = sync_send(self.request);
2021-07-02 12:47:52 +00:00
dbg!(&resp);
resp
2021-06-30 15:11:27 +00:00
}
}