helper.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_define::CollabType;
  8. use flowy_database2::entities::{DatabasePB, DatabaseViewIdPB, RepeatedDatabaseSnapshotPB};
  9. use flowy_database2::event_map::DatabaseEvent::*;
  10. use flowy_folder2::entities::ViewPB;
  11. use flowy_test::event_builder::EventBuilder;
  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 cloud_service = self.database_manager.get_cloud_service().clone();
  65. let remote_updates = cloud_service
  66. .get_collab_update(database_id, CollabType::Database)
  67. .await
  68. .unwrap();
  69. if remote_updates.is_empty() {
  70. return vec![];
  71. }
  72. let updates = remote_updates
  73. .iter()
  74. .map(|update| update.as_ref())
  75. .collect::<Vec<&[u8]>>();
  76. merge_updates_v1(&updates).unwrap()
  77. }
  78. }
  79. pub fn assert_database_collab_content(
  80. database_id: &str,
  81. collab_update: &[u8],
  82. expected: JsonValue,
  83. ) {
  84. let collab = MutexCollab::new(CollabOrigin::Server, database_id, vec![]);
  85. collab.lock().with_origin_transact_mut(|txn| {
  86. let update = Update::decode_v1(collab_update).unwrap();
  87. txn.apply_update(update);
  88. });
  89. let json = collab.to_json_value();
  90. assert_json_eq!(json, expected);
  91. }
  92. impl Deref for FlowySupabaseDatabaseTest {
  93. type Target = FlowySupabaseTest;
  94. fn deref(&self) -> &Self::Target {
  95. &self.inner
  96. }
  97. }