event_builder.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. use crate::FlowySDKTest;
  2. use flowy_user::{entities::UserProfile, errors::FlowyError};
  3. use lib_dispatch::prelude::{EventDispatcher, EventResponse, FromBytes, ModuleRequest, StatusCode, ToBytes, *};
  4. use std::{
  5. convert::TryFrom,
  6. fmt::{Debug, Display},
  7. hash::Hash,
  8. marker::PhantomData,
  9. sync::Arc,
  10. };
  11. pub type FolderEventBuilder = EventBuilder<FlowyError>;
  12. impl FolderEventBuilder {
  13. pub fn new(sdk: FlowySDKTest) -> Self {
  14. EventBuilder::test(TestContext::new(sdk))
  15. }
  16. pub fn user_profile(&self) -> &Option<UserProfile> {
  17. &self.user_profile
  18. }
  19. }
  20. pub type UserModuleEventBuilder = FolderEventBuilder;
  21. #[derive(Clone)]
  22. pub struct EventBuilder<E> {
  23. context: TestContext,
  24. user_profile: Option<UserProfile>,
  25. err_phantom: PhantomData<E>,
  26. }
  27. impl<E> EventBuilder<E>
  28. where
  29. E: FromBytes + Debug,
  30. {
  31. fn test(context: TestContext) -> Self {
  32. Self {
  33. context,
  34. user_profile: None,
  35. err_phantom: PhantomData,
  36. }
  37. }
  38. pub fn payload<P>(mut self, payload: P) -> Self
  39. where
  40. P: ToBytes,
  41. {
  42. match payload.into_bytes() {
  43. Ok(bytes) => {
  44. let module_request = self.get_request();
  45. self.context.request = Some(module_request.payload(bytes))
  46. }
  47. Err(e) => {
  48. log::error!("Set payload failed: {:?}", e);
  49. }
  50. }
  51. self
  52. }
  53. pub fn event<Event>(mut self, event: Event) -> Self
  54. where
  55. Event: Eq + Hash + Debug + Clone + Display,
  56. {
  57. self.context.request = Some(ModuleRequest::new(event));
  58. self
  59. }
  60. pub fn sync_send(mut self) -> Self {
  61. let request = self.get_request();
  62. let resp = EventDispatcher::sync_send(self.dispatch(), request);
  63. self.context.response = Some(resp);
  64. self
  65. }
  66. pub async fn async_send(mut self) -> Self {
  67. let request = self.get_request();
  68. let resp = EventDispatcher::async_send(self.dispatch(), request).await;
  69. self.context.response = Some(resp);
  70. self
  71. }
  72. pub fn parse<R>(self) -> R
  73. where
  74. R: FromBytes,
  75. {
  76. let response = self.get_response();
  77. match response.parse::<R, E>() {
  78. Ok(Ok(data)) => data,
  79. Ok(Err(e)) => {
  80. panic!("parse failed: {:?}", e)
  81. }
  82. Err(e) => panic!("Internal error: {:?}", e),
  83. }
  84. }
  85. pub fn error(self) -> E {
  86. let response = self.get_response();
  87. assert_eq!(response.status_code, StatusCode::Err);
  88. <Data<E>>::try_from(response.payload).unwrap().into_inner()
  89. }
  90. pub fn assert_error(self) -> Self {
  91. // self.context.assert_error();
  92. self
  93. }
  94. pub fn assert_success(self) -> Self {
  95. // self.context.assert_success();
  96. self
  97. }
  98. fn dispatch(&self) -> Arc<EventDispatcher> {
  99. self.context.sdk.dispatcher()
  100. }
  101. fn get_response(&self) -> EventResponse {
  102. self.context
  103. .response
  104. .as_ref()
  105. .expect("must call sync_send first")
  106. .clone()
  107. }
  108. fn get_request(&mut self) -> ModuleRequest {
  109. self.context.request.take().expect("must call event first")
  110. }
  111. }
  112. #[derive(Clone)]
  113. pub struct TestContext {
  114. pub sdk: FlowySDKTest,
  115. request: Option<ModuleRequest>,
  116. response: Option<EventResponse>,
  117. }
  118. impl TestContext {
  119. pub fn new(sdk: FlowySDKTest) -> Self {
  120. Self {
  121. sdk,
  122. request: None,
  123. response: None,
  124. }
  125. }
  126. }