text-document-common 1.4.1

Shared entities, database, events, and undo/redo infrastructure for text-document
Documentation
// Generated by Qleany v1.7.3 from transaction_tests.tera
#![cfg(test)]
#![allow(dead_code)]
#![allow(unused_imports)]

use anyhow::Result;
use common::database::{db_context::DbContext, transactions::Transaction};
use common::snapshot::EntityTreeSnapshot;
use common::types::Savepoint;

#[test]
fn test_savepoint_roundtrip() -> Result<()> {
    let db_context = DbContext::new()?;

    let mut txn = Transaction::begin_write_transaction(&db_context)?;
    let savepoint = txn.create_savepoint()?;

    txn.restore_to_savepoint(savepoint)?;

    txn.commit()?;
    Ok(())
}

#[test]
fn test_read_transaction_rejects_savepoint() {
    let db_context = DbContext::new().unwrap();
    let txn = Transaction::begin_read_transaction(&db_context).unwrap();

    assert!(txn.create_savepoint().is_err());
}

#[test]
fn test_write_transaction_commit_and_rollback() -> Result<()> {
    let db_context = DbContext::new()?;

    let mut txn = Transaction::begin_write_transaction(&db_context)?;
    txn.commit()?;

    let mut txn = Transaction::begin_write_transaction(&db_context)?;
    txn.rollback()?;

    Ok(())
}

#[test]
fn test_read_transaction_rejects_commit() {
    let db_context = DbContext::new().unwrap();
    let mut txn = Transaction::begin_read_transaction(&db_context).unwrap();

    assert!(txn.commit().is_err());
    assert!(txn.rollback().is_err());
}

#[test]
fn test_store_snapshot_roundtrip() -> Result<()> {
    let db_context = DbContext::new()?;
    let txn = Transaction::begin_write_transaction(&db_context)?;

    let snap = txn.snapshot_store();
    txn.restore_store(&snap);

    Ok(())
}

#[test]
fn test_drop_without_commit_restores_savepoint() -> Result<()> {
    let db_context = DbContext::new()?;

    // Begin a write transaction (auto-savepoint created)
    {
        let _txn = Transaction::begin_write_transaction(&db_context)?;
        // Drop without commit — auto-savepoint should be restored
    }

    // Should be able to start a new transaction without issue
    let mut txn = Transaction::begin_write_transaction(&db_context)?;
    txn.commit()?;
    Ok(())
}

#[test]
fn test_entity_tree_snapshot_default() {
    let snap = EntityTreeSnapshot::default();
    assert!(snap.store_snapshot.is_none());
}