factory-m8 0.9.0

Core traits for test data factories with automatic FK resolution
Documentation
  • Coverage
  • 100%
    8 out of 8 items documented1 out of 5 items with examples
  • Size
  • Source code size: 14.7 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.3 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 15s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • YegorMy/factory-m8
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • YegorMy

factory-m8

Core traits for test data factories with automatic FK resolution.

This crate provides the foundational traits used by factory-m8-derive to generate test factories that automatically create database dependencies.

Installation

[dev-dependencies]
factory-m8 = "0.1"
factory-m8-derive = "0.1"

Traits

FactoryCreate<Pool>

The main trait for factories that create entities in a database. Generic over the connection pool type, so it works with any database backend.

use factory_m8::{FactoryCreate, FactoryResult};
use async_trait::async_trait;
use sqlx::PgPool;

#[async_trait]
impl FactoryCreate<PgPool> for UserFactory {
    type Entity = User;

    async fn create(self, pool: &PgPool) -> FactoryResult<User> {
        let entity = self.build_with_fks(pool).await?;

        sqlx::query_as!(User,
            "INSERT INTO users (name, email) VALUES ($1, $2) RETURNING *",
            entity.name,
            entity.email,
        )
        .fetch_one(pool)
        .await
        .map_err(Into::into)
    }
}

Sentinel

Trait for detecting "sentinel" values that trigger auto-creation of FK dependencies.

use factory_m8::Sentinel;

#[derive(Clone, Copy, Default)]
pub struct UserId(pub i64);

impl Sentinel for UserId {
    fn sentinel() -> Self {
        UserId(0)  // 0 means "not set"
    }

    fn is_sentinel(&self) -> bool {
        self.0 == 0
    }
}

Built-in implementations for: i16, i32, i64, u32, u64, String, and Option<T>.

Database Backends

FactoryCreate is generic over the pool type, supporting any database:

Backend Pool Type
PostgreSQL sqlx::PgPool
SQLite sqlx::SqlitePool
MySQL sqlx::MySqlPool
MongoDB mongodb::Database

License

MIT License - see LICENSE for details.