123 lines
4.1 KiB
Rust
123 lines
4.1 KiB
Rust
use db::{DeploymentConfig, Mutation, StorageError, connect};
|
|
use documents::{
|
|
Contextualize, Document, DocumentContent, DocumentId, DocumentKey, DocumentTree, WorkspaceId,
|
|
};
|
|
use serde_json::json;
|
|
|
|
fn document(workspace: &str, body: serde_json::Value) -> Document {
|
|
Document::new(
|
|
DocumentKey {
|
|
workspace: WorkspaceId::new(workspace).unwrap(),
|
|
document: DocumentId::new("same-id").unwrap(),
|
|
},
|
|
DocumentContent::Json(body),
|
|
)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn workspace_documents_keep_their_identity_and_contents_after_reopen() {
|
|
let folder = tempfile::tempdir().unwrap();
|
|
let path = folder.path().join("documents.sqlite3");
|
|
let mut config = DeploymentConfig {
|
|
sqlite_path: path.to_str().unwrap().into(),
|
|
..DeploymentConfig::default()
|
|
};
|
|
let connection = connect(&config).await.unwrap();
|
|
let first = document(
|
|
&"w".repeat(WorkspaceId::MAX_BYTES),
|
|
json!({"title": "notes"}),
|
|
);
|
|
let second = document("other-workspace", json!(null));
|
|
|
|
assert!(connection.get_document(&first.key).await.unwrap().is_none());
|
|
let first_revision = connection.create_document(first.clone()).await.unwrap();
|
|
let second_revision = connection.create_document(second.clone()).await.unwrap();
|
|
assert!(second_revision > first_revision);
|
|
assert!(matches!(
|
|
connection
|
|
.create_document(document(first.key.workspace.as_str(), json!("overwrite")))
|
|
.await,
|
|
Err(StorageError::DocumentAlreadyExists)
|
|
));
|
|
drop(connection);
|
|
|
|
config.read_only = true;
|
|
let connection = connect(&config).await.unwrap();
|
|
let read = connection.get_document(&first.key).await.unwrap().unwrap();
|
|
assert_eq!(read.document, first);
|
|
assert_eq!(read.revision, first_revision);
|
|
let read = connection.get_document(&second.key).await.unwrap().unwrap();
|
|
assert_eq!(read.document, second);
|
|
assert_eq!(read.revision, second_revision);
|
|
assert!(matches!(
|
|
connection
|
|
.create_document(document("third-workspace", json!(null)))
|
|
.await,
|
|
Err(StorageError::ReadOnly)
|
|
));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn persists_typed_nodes_and_reads_the_original_json_encoding() {
|
|
let folder = tempfile::tempdir().unwrap();
|
|
let path = folder.path().join("documents.sqlite3");
|
|
let connection = connect(&DeploymentConfig {
|
|
sqlite_path: path.to_str().unwrap().into(),
|
|
..DeploymentConfig::default()
|
|
})
|
|
.await
|
|
.unwrap();
|
|
|
|
let page = Document::new(
|
|
document("team", json!(null)).key,
|
|
DocumentContent::Page {
|
|
title: "Planning".into(),
|
|
},
|
|
);
|
|
let mut todo = page.clone();
|
|
todo.key.document = DocumentId::new("todo").unwrap();
|
|
todo.parent_id = Some(page.key.document.clone());
|
|
todo.position = 2;
|
|
todo.content = DocumentContent::Todo {
|
|
text: "Review".into(),
|
|
checked: true,
|
|
};
|
|
|
|
assert!(matches!(
|
|
connection.create_document(todo.clone()).await,
|
|
Err(StorageError::Invalid("document parent"))
|
|
));
|
|
connection.create_document(page.clone()).await.unwrap();
|
|
connection.create_document(todo.clone()).await.unwrap();
|
|
let read = connection.get_document(&todo.key).await.unwrap().unwrap();
|
|
assert_eq!(read.document, todo);
|
|
let tree = DocumentTree::new(page, [read.document]).unwrap();
|
|
assert_eq!(tree.render_context(), "# Planning\n- [x] Review");
|
|
|
|
let legacy = document("legacy", json!({"previous": "body"}));
|
|
connection
|
|
.commit(
|
|
connection.revision().await.unwrap(),
|
|
&[Mutation {
|
|
collection: "bedrock:workspace-documents:v1:legacy".into(),
|
|
id: legacy.key.document.as_str().into(),
|
|
value: Some(json!({"previous": "body"})),
|
|
}],
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
connection
|
|
.get_document(&legacy.key)
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.document,
|
|
legacy
|
|
);
|
|
assert!(matches!(
|
|
connection.create_document(legacy).await,
|
|
Err(StorageError::DocumentAlreadyExists)
|
|
));
|
|
}
|