persist.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //! Tests to ensure that documents are persisted with SQLite.
  2. use std::time::Duration;
  3. use anyhow::Result;
  4. use common::*;
  5. use operational_transform::OperationSeq;
  6. use rustpad_server::{
  7. database::{Database, PersistedDocument},
  8. server, ServerConfig,
  9. };
  10. use serde_json::json;
  11. use tempfile::NamedTempFile;
  12. use tokio::time;
  13. pub mod common;
  14. fn temp_sqlite_uri() -> Result<String> {
  15. Ok(format!(
  16. "sqlite://{}",
  17. NamedTempFile::new()?
  18. .into_temp_path()
  19. .as_os_str()
  20. .to_str()
  21. .expect("failed to get name of tempfile as &str")
  22. ))
  23. }
  24. #[tokio::test]
  25. async fn test_database() -> Result<()> {
  26. pretty_env_logger::try_init().ok();
  27. let database = Database::new(&temp_sqlite_uri()?).await?;
  28. assert!(database.load("hello").await.is_err());
  29. assert!(database.load("world").await.is_err());
  30. let doc1 = PersistedDocument {
  31. text: "Hello Text".into(),
  32. language: None,
  33. };
  34. assert!(database.store("hello", &doc1).await.is_ok());
  35. assert_eq!(database.load("hello").await?, doc1);
  36. assert!(database.load("world").await.is_err());
  37. let doc2 = PersistedDocument {
  38. text: "print('World Text :)')".into(),
  39. language: Some("python".into()),
  40. };
  41. assert!(database.store("world", &doc2).await.is_ok());
  42. assert_eq!(database.load("hello").await?, doc1);
  43. assert_eq!(database.load("world").await?, doc2);
  44. assert!(database.store("hello", &doc2).await.is_ok());
  45. assert_eq!(database.load("hello").await?, doc2);
  46. Ok(())
  47. }
  48. #[tokio::test]
  49. async fn test_persist() -> Result<()> {
  50. pretty_env_logger::try_init().ok();
  51. let filter = server(ServerConfig {
  52. expiry_days: 2,
  53. database: Some(Database::new(&temp_sqlite_uri()?).await?),
  54. });
  55. expect_text(&filter, "persist", "").await;
  56. let mut client = connect(&filter, "persist").await?;
  57. let msg = client.recv().await?;
  58. assert_eq!(msg, json!({ "Identity": 0 }));
  59. let mut operation = OperationSeq::default();
  60. operation.insert("hello");
  61. let msg = json!({
  62. "Edit": {
  63. "revision": 0,
  64. "operation": operation
  65. }
  66. });
  67. client.send(&msg).await;
  68. let msg = client.recv().await?;
  69. msg.get("History")
  70. .expect("should receive history operation");
  71. expect_text(&filter, "persist", "hello").await;
  72. let hour = Duration::from_secs(3600);
  73. time::pause();
  74. time::advance(47 * hour).await;
  75. expect_text(&filter, "persist", "hello").await;
  76. // Give SQLite some time to actually update the database.
  77. time::resume();
  78. time::sleep(Duration::from_millis(50)).await;
  79. time::pause();
  80. time::advance(3 * hour).await;
  81. expect_text(&filter, "persist", "hello").await;
  82. for _ in 0..50 {
  83. time::advance(10000 * hour).await;
  84. expect_text(&filter, "persist", "hello").await;
  85. }
  86. Ok(())
  87. }