Add direct database tests and restructure code
This commit is contained in:
parent
77196fca0b
commit
6673059d3a
6 changed files with 44 additions and 9 deletions
|
@ -6,7 +6,7 @@ use anyhow::{bail, Result};
|
|||
use sqlx::{sqlite::SqliteConnectOptions, ConnectOptions, SqlitePool};
|
||||
|
||||
/// Represents a document persisted in database storage.
|
||||
#[derive(sqlx::FromRow)]
|
||||
#[derive(sqlx::FromRow, PartialEq, Eq, Clone, Debug)]
|
||||
pub struct PersistedDocument {
|
||||
/// Text content of the document.
|
||||
pub text: String,
|
||||
|
|
|
@ -12,10 +12,9 @@ use serde::Serialize;
|
|||
use tokio::time::{self, Instant};
|
||||
use warp::{filters::BoxedFilter, ws::Ws, Filter, Rejection, Reply};
|
||||
|
||||
pub use crate::database::Database;
|
||||
use crate::rustpad::Rustpad;
|
||||
use crate::{database::Database, rustpad::Rustpad};
|
||||
|
||||
mod database;
|
||||
pub mod database;
|
||||
mod ot;
|
||||
mod rustpad;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use rustpad_server::{server, Database, ServerConfig};
|
||||
use rustpad_server::{server, database::Database, ServerConfig};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
|
|
@ -13,7 +13,7 @@ impl JsonSocket {
|
|||
pub async fn recv(&mut self) -> Result<Value> {
|
||||
let msg = self.0.recv().await?;
|
||||
let msg = msg.to_str().map_err(|_| anyhow!("non-string message"))?;
|
||||
Ok(serde_json::from_str(&msg)?)
|
||||
Ok(serde_json::from_str(msg)?)
|
||||
}
|
||||
|
||||
pub async fn recv_closed(&mut self) -> Result<()> {
|
||||
|
|
|
@ -5,7 +5,10 @@ use std::time::Duration;
|
|||
use anyhow::Result;
|
||||
use common::*;
|
||||
use operational_transform::OperationSeq;
|
||||
use rustpad_server::{server, Database, ServerConfig};
|
||||
use rustpad_server::{
|
||||
database::{Database, PersistedDocument},
|
||||
server, ServerConfig,
|
||||
};
|
||||
use serde_json::json;
|
||||
use tempfile::NamedTempFile;
|
||||
use tokio::time;
|
||||
|
@ -23,6 +26,39 @@ fn temp_sqlite_uri() -> Result<String> {
|
|||
))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_database() -> Result<()> {
|
||||
pretty_env_logger::try_init().ok();
|
||||
|
||||
let database = Database::new(&temp_sqlite_uri()?).await?;
|
||||
|
||||
assert!(database.load("hello").await.is_err());
|
||||
assert!(database.load("world").await.is_err());
|
||||
|
||||
let doc1 = PersistedDocument {
|
||||
text: "Hello Text".into(),
|
||||
language: None,
|
||||
};
|
||||
|
||||
assert!(database.store("hello", &doc1).await.is_ok());
|
||||
assert_eq!(database.load("hello").await?, doc1);
|
||||
assert!(database.load("world").await.is_err());
|
||||
|
||||
let doc2 = PersistedDocument {
|
||||
text: "print('World Text :)')".into(),
|
||||
language: Some("python".into()),
|
||||
};
|
||||
|
||||
assert!(database.store("world", &doc2).await.is_ok());
|
||||
assert_eq!(database.load("hello").await?, doc1);
|
||||
assert_eq!(database.load("world").await?, doc2);
|
||||
|
||||
assert!(database.store("hello", &doc2).await.is_ok());
|
||||
assert_eq!(database.load("hello").await?, doc2);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_persist() -> Result<()> {
|
||||
pretty_env_logger::try_init().ok();
|
||||
|
|
|
@ -53,13 +53,13 @@ async fn test_lost_wakeups() -> Result<()> {
|
|||
let mut total = 0;
|
||||
while total < num_edits {
|
||||
let msg = client.recv().await?;
|
||||
total += num_ops(&msg).ok_or(anyhow!("missing json key"))?;
|
||||
total += num_ops(&msg).ok_or_else(|| anyhow!("missing json key"))?;
|
||||
}
|
||||
|
||||
let mut total2 = 0;
|
||||
while total2 < num_edits {
|
||||
let msg = client2.recv().await?;
|
||||
total2 += num_ops(&msg).ok_or(anyhow!("missing json key"))?;
|
||||
total2 += num_ops(&msg).ok_or_else(|| anyhow!("missing json key"))?;
|
||||
}
|
||||
|
||||
info!("took {} ms", start.elapsed().as_millis());
|
||||
|
|
Loading…
Add table
Reference in a new issue