omry-integration 0.3.0

Helpers for Omry client applications that run integration tests against the Omry backend.
Documentation
use omry_db::Database;
use std::fs::File;
use std::ops::Deref;
use tempfile::NamedTempFile;

/// Data structure to keep the temporary file holding the database alive,
/// because it will get unlinked on drop.
pub struct TempDb {
    db: Database,

    // We just need to store the temporary file here so that it sticks around
    // while TempDb is alive.
    #[allow(dead_code)]
    temp_db_file: NamedTempFile<File>,
}

impl TempDb {
    /// Creates a new database at a temporary path.
    ///
    /// ## Errors
    /// Returns [`anyhow::Error`] if we can't:
    ///
    /// - Create a new [`NamedTempFile`] for the database
    /// - Initialize the database at the temporary path
    pub async fn new() -> anyhow::Result<Self> {
        let temp_db_file = NamedTempFile::with_suffix(".db")?;
        let db = Database::new_with_records_table(temp_db_file.path()).await?;
        Ok(Self { db, temp_db_file })
    }
}

impl Deref for TempDb {
    type Target = Database;

    fn deref(&self) -> &Self::Target {
        &self.db
    }
}