lib.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. use flowy_dispatch::prelude::*;
  2. pub use flowy_sdk::*;
  3. use std::{
  4. convert::TryFrom,
  5. fmt::{Debug, Display},
  6. fs,
  7. hash::Hash,
  8. marker::PhantomData,
  9. path::PathBuf,
  10. sync::Once,
  11. thread,
  12. };
  13. pub mod prelude {
  14. pub use crate::EventTester;
  15. pub use flowy_dispatch::prelude::*;
  16. pub use std::convert::TryFrom;
  17. }
  18. static INIT: Once = Once::new();
  19. pub fn init_sdk() {
  20. let root_dir = root_dir();
  21. INIT.call_once(|| {
  22. FlowySDK::init_log(&root_dir);
  23. FlowySDK::init(&root_dir);
  24. });
  25. }
  26. fn root_dir() -> String {
  27. // https://doc.rust-lang.org/cargo/reference/environment-variables.html
  28. let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or("./".to_owned());
  29. let mut path_buf = fs::canonicalize(&PathBuf::from(&manifest_dir)).unwrap();
  30. path_buf.pop(); // rust-lib
  31. path_buf.push("flowy-test");
  32. path_buf.push("temp");
  33. path_buf.push("flowy");
  34. let root_dir = path_buf.to_str().unwrap().to_string();
  35. if !std::path::Path::new(&root_dir).exists() {
  36. std::fs::create_dir_all(&root_dir).unwrap();
  37. }
  38. root_dir
  39. }
  40. pub struct EventTester<ErrType> {
  41. inner_request: Option<ModuleRequest>,
  42. assert_status_code: Option<StatusCode>,
  43. response: Option<EventResponse>,
  44. phantom: PhantomData<ErrType>,
  45. }
  46. impl<ErrType> EventTester<ErrType>
  47. where
  48. ErrType: FromBytes + Debug,
  49. {
  50. pub fn new<E>(event: E) -> Self
  51. where
  52. E: Eq + Hash + Debug + Clone + Display,
  53. {
  54. init_sdk();
  55. log::trace!(
  56. "{:?} thread started: thread_id= {}",
  57. thread::current(),
  58. thread_id::get()
  59. );
  60. Self {
  61. inner_request: Some(ModuleRequest::new(event)),
  62. assert_status_code: None,
  63. response: None,
  64. phantom: PhantomData,
  65. }
  66. }
  67. pub fn request<P>(mut self, request: P) -> Self
  68. where
  69. P: ToBytes,
  70. {
  71. let mut inner_request = self.inner_request.take().unwrap();
  72. let bytes = request.into_bytes().unwrap();
  73. inner_request = inner_request.payload(bytes);
  74. self.inner_request = Some(inner_request);
  75. self
  76. }
  77. pub fn assert_status_code(mut self, status_code: StatusCode) -> Self {
  78. self.assert_status_code = Some(status_code);
  79. self
  80. }
  81. pub fn assert_error(mut self) -> Self {
  82. self.assert_status_code = Some(StatusCode::Err);
  83. self
  84. }
  85. #[allow(dead_code)]
  86. pub async fn async_send(mut self) -> Self {
  87. let resp =
  88. EventDispatch::async_send(self.inner_request.take().unwrap(), |_| Box::pin(async {}))
  89. .await;
  90. check(&resp, &self.assert_status_code);
  91. self.response = Some(resp);
  92. self
  93. }
  94. pub fn sync_send(mut self) -> Self {
  95. let resp = EventDispatch::sync_send(self.inner_request.take().unwrap());
  96. check(&resp, &self.assert_status_code);
  97. self.response = Some(resp);
  98. self
  99. }
  100. pub fn parse<R>(self) -> R
  101. where
  102. R: FromBytes,
  103. {
  104. let response = self.response.unwrap();
  105. if response.status_code == StatusCode::Err {
  106. let error = <Data<ErrType>>::try_from(response.payload)
  107. .unwrap()
  108. .into_inner();
  109. dbg!(&error);
  110. panic!("")
  111. } else {
  112. <Data<R>>::try_from(response.payload).unwrap().into_inner()
  113. }
  114. }
  115. pub fn error(self) -> ErrType {
  116. let response = self.response.unwrap();
  117. assert_eq!(response.status_code, StatusCode::Err);
  118. <Data<ErrType>>::try_from(response.payload)
  119. .unwrap()
  120. .into_inner()
  121. }
  122. }
  123. fn check(response: &EventResponse, status_code: &Option<StatusCode>) {
  124. if let Some(ref status_code) = status_code {
  125. if &response.status_code != status_code {
  126. eprintln!("{:#?}", response);
  127. }
  128. assert_eq!(&response.status_code, status_code)
  129. }
  130. }