capy-testing 0.10.0

In-process HTTP testing utilities for Capy applications.
Documentation
use capy_core::Result;
use sea_orm::ConnectionTrait;
use std::future::Future;

/// An application-owned record builder. Implement with native SeaORM operations.
/// Passing a borrowed connection permits both a pool and an existing transaction.
pub trait Factory: Sized {
    /// The record produced by this factory, usually an application model.
    type Model;

    /// Insert through the supplied connection; never acquire a different pool internally.
    fn create<C: ConnectionTrait>(
        self,
        connection: &C,
    ) -> impl Future<Output = Result<Self::Model>> + Send;
}

/// A factory and its explicit database/transaction. Own field defaults in application code.
pub struct FactoryBuilder<'a, F, C> {
    factory: F,
    connection: &'a C,
}

impl<'a, F: Factory, C: ConnectionTrait> FactoryBuilder<'a, F, C> {
    /// Bind an application factory to a database or transaction.
    pub fn new(factory: F, connection: &'a C) -> Self {
        Self {
            factory,
            connection,
        }
    }

    /// Customize ordinary Rust fields or invoke application-owned builder methods.
    pub fn with(mut self, customize: impl FnOnce(&mut F)) -> Self {
        customize(&mut self.factory);
        self
    }

    /// Create the record in the selected database/transaction.
    pub async fn create(self) -> Result<F::Model> {
        self.factory.create(self.connection).await
    }
}