test.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. use std::time::Duration;
  2. use flowy_database2::entities::{
  3. DatabaseSnapshotStatePB, DatabaseSyncStatePB, FieldChangesetPB, FieldType,
  4. };
  5. use flowy_database2::notification::DatabaseNotification::DidUpdateDatabaseSnapshotState;
  6. use crate::database::supabase_test::helper::{
  7. assert_database_collab_content, FlowySupabaseDatabaseTest,
  8. };
  9. use crate::util::receive_with_timeout;
  10. #[tokio::test]
  11. async fn supabase_initial_database_snapshot_test() {
  12. if let Some(test) = FlowySupabaseDatabaseTest::new_with_new_user().await {
  13. let (view, database) = test.create_database().await;
  14. let mut rx = test
  15. .notification_sender
  16. .subscribe::<DatabaseSnapshotStatePB>(&database.id, DidUpdateDatabaseSnapshotState);
  17. receive_with_timeout(&mut rx, Duration::from_secs(30))
  18. .await
  19. .unwrap();
  20. let expected = test.get_collab_json(&database.id).await;
  21. let snapshots = test.get_database_snapshots(&view.id).await;
  22. assert_eq!(snapshots.items.len(), 1);
  23. assert_database_collab_content(&database.id, &snapshots.items[0].data, expected);
  24. }
  25. }
  26. #[tokio::test]
  27. async fn supabase_edit_database_test() {
  28. if let Some(test) = FlowySupabaseDatabaseTest::new_with_new_user().await {
  29. let (view, database) = test.create_database().await;
  30. let existing_fields = test.get_all_database_fields(&view.id).await;
  31. for field in existing_fields.items {
  32. if !field.is_primary {
  33. test.delete_field(&view.id, &field.id).await;
  34. }
  35. }
  36. let field = test.create_field(&view.id, FieldType::Checklist).await;
  37. test
  38. .update_field(FieldChangesetPB {
  39. field_id: field.id.clone(),
  40. view_id: view.id.clone(),
  41. name: Some("hello world".to_string()),
  42. ..Default::default()
  43. })
  44. .await;
  45. // wait all updates are send to the remote
  46. let mut rx = test
  47. .notification_sender
  48. .subscribe_with_condition::<DatabaseSyncStatePB, _>(&database.id, |pb| pb.is_finish);
  49. receive_with_timeout(&mut rx, Duration::from_secs(30))
  50. .await
  51. .unwrap();
  52. assert_eq!(test.get_all_database_fields(&view.id).await.items.len(), 2);
  53. let expected = test.get_collab_json(&database.id).await;
  54. let update = test.get_database_collab_update(&database.id).await;
  55. assert_database_collab_content(&database.id, &update, expected);
  56. }
  57. }
  58. // #[tokio::test]
  59. // async fn cloud_test_supabase_login_sync_database_test() {
  60. // if let Some(test) = FlowySupabaseDatabaseTest::new_with_new_user().await {
  61. // let uuid = test.uuid.clone();
  62. // let (view, database) = test.create_database().await;
  63. // // wait all updates are send to the remote
  64. // let mut rx = test
  65. // .notification_sender
  66. // .subscribe_with_condition::<DatabaseSyncStatePB, _>(&database.id, |pb| pb.is_finish);
  67. // receive_with_timeout(&mut rx, Duration::from_secs(30))
  68. // .await
  69. // .unwrap();
  70. // let expected = test.get_collab_json(&database.id).await;
  71. // test.sign_out().await;
  72. // // Drop the test will cause the test resources to be dropped, which will
  73. // // delete the user data folder.
  74. // drop(test);
  75. //
  76. // let new_test = FlowySupabaseDatabaseTest::new_with_user(uuid)
  77. // .await
  78. // .unwrap();
  79. // // let actual = new_test.get_collab_json(&database.id).await;
  80. // // assert_json_eq!(actual, json!(""));
  81. //
  82. // new_test.open_database(&view.id).await;
  83. //
  84. // // wait all updates are synced from the remote
  85. // let mut rx = new_test
  86. // .notification_sender
  87. // .subscribe_with_condition::<DatabaseSyncStatePB, _>(&database.id, |pb| pb.is_finish);
  88. // receive_with_timeout(&mut rx, Duration::from_secs(30))
  89. // .await
  90. // .unwrap();
  91. //
  92. // // when the new sync is finished, the database should be the same as the old one
  93. // let actual = new_test.get_collab_json(&database.id).await;
  94. // assert_json_eq!(actual, expected);
  95. // }
  96. // }