helper.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. use std::ops::Deref;
  2. use assert_json_diff::assert_json_eq;
  3. use collab::core::collab::MutexCollab;
  4. use collab::core::origin::CollabOrigin;
  5. use collab::preclude::updates::decoder::Decode;
  6. use collab::preclude::{merge_updates_v1, JsonValue, Update};
  7. use collab_entity::CollabType;
  8. use event_integration::event_builder::EventBuilder;
  9. use flowy_database2::entities::{DatabasePB, DatabaseViewIdPB, RepeatedDatabaseSnapshotPB};
  10. use flowy_database2::event_map::DatabaseEvent::*;
  11. use flowy_folder2::entities::ViewPB;
  12. use crate::util::FlowySupabaseTest;
  13. pub struct FlowySupabaseDatabaseTest {
  14. pub uuid: String,
  15. inner: FlowySupabaseTest,
  16. }
  17. impl FlowySupabaseDatabaseTest {
  18. #[allow(dead_code)]
  19. pub async fn new_with_user(uuid: String) -> Option<Self> {
  20. let inner = FlowySupabaseTest::new()?;
  21. inner.supabase_sign_up_with_uuid(&uuid, None).await.unwrap();
  22. Some(Self { uuid, inner })
  23. }
  24. pub async fn new_with_new_user() -> Option<Self> {
  25. let inner = FlowySupabaseTest::new()?;
  26. let uuid = uuid::Uuid::new_v4().to_string();
  27. let _ = inner.supabase_sign_up_with_uuid(&uuid, None).await.unwrap();
  28. Some(Self { uuid, inner })
  29. }
  30. pub async fn create_database(&self) -> (ViewPB, DatabasePB) {
  31. let current_workspace = self.inner.get_current_workspace().await;
  32. let view = self
  33. .inner
  34. .create_grid(
  35. &current_workspace.workspace.id,
  36. "my database".to_string(),
  37. vec![],
  38. )
  39. .await;
  40. let database = self.inner.get_database(&view.id).await;
  41. (view, database)
  42. }
  43. pub async fn get_collab_json(&self, database_id: &str) -> JsonValue {
  44. let database_editor = self
  45. .database_manager
  46. .get_database(database_id)
  47. .await
  48. .unwrap();
  49. // let address = Arc::into_raw(database_editor.clone());
  50. let database = database_editor.get_mutex_database().lock();
  51. database.get_mutex_collab().to_json_value()
  52. }
  53. pub async fn get_database_snapshots(&self, view_id: &str) -> RepeatedDatabaseSnapshotPB {
  54. EventBuilder::new(self.inner.deref().clone())
  55. .event(GetDatabaseSnapshots)
  56. .payload(DatabaseViewIdPB {
  57. value: view_id.to_string(),
  58. })
  59. .async_send()
  60. .await
  61. .parse::<RepeatedDatabaseSnapshotPB>()
  62. }
  63. pub async fn get_database_collab_update(&self, database_id: &str) -> Vec<u8> {
  64. let workspace_id = self.user_manager.workspace_id().unwrap();
  65. let cloud_service = self.database_manager.get_cloud_service().clone();
  66. let remote_updates = cloud_service
  67. .get_collab_update(database_id, CollabType::Database, &workspace_id)
  68. .await
  69. .unwrap();
  70. if remote_updates.is_empty() {
  71. return vec![];
  72. }
  73. let updates = remote_updates
  74. .iter()
  75. .map(|update| update.as_ref())
  76. .collect::<Vec<&[u8]>>();
  77. merge_updates_v1(&updates).unwrap()
  78. }
  79. }
  80. pub fn assert_database_collab_content(
  81. database_id: &str,
  82. collab_update: &[u8],
  83. expected: JsonValue,
  84. ) {
  85. let collab = MutexCollab::new(CollabOrigin::Server, database_id, vec![]);
  86. collab.lock().with_origin_transact_mut(|txn| {
  87. let update = Update::decode_v1(collab_update).unwrap();
  88. txn.apply_update(update);
  89. });
  90. let json = collab.to_json_value();
  91. assert_json_eq!(json, expected);
  92. }
  93. impl Deref for FlowySupabaseDatabaseTest {
  94. type Target = FlowySupabaseTest;
  95. fn deref(&self) -> &Self::Target {
  96. &self.inner
  97. }
  98. }