loco-factory

Crates.ioloco-factory
lib.rsloco-factory
version0.1.0
created_at2025-11-25 16:43:46.330986+00
updated_at2025-11-25 16:43:46.330986+00
descriptionIt's a model data generator to sea_orm
homepage
repositoryhttps://github.com/ofabianomartins/loco_factory
max_upload_size
id1950067
size109,438
Fabiano (ofabianomartins)

documentation

README

Loco Factory

loco-factory is a powerful macro for creating test data factories for sea-orm models. It simplifies the process of generating test data by providing a flexible and expressive domain-specific language (DSL) for defining factories.

Features

  • Declarative Factory Definition: Define factories using a simple and intuitive macro.
  • Builder Pattern: Each factory automatically generates a builder pattern for easy customization of model fields.
  • Default Values: Specify default values for each field in your models.
  • Easy Integration: Works seamlessly with sea-orm and your existing test suite.

Getting Started

1. Add loco-factory to your project

Add the following to your Cargo.toml file:

[dependencies]
loco-factory = "0.1.0"

2. Define your factories

Use the define_factory! macro to define factories for your sea-orm models.

use loco_factory::define_factory;
use sea_orm::{entity::prelude::*, ActiveModelTrait, Set};
use uuid::Uuid;

// Your sea-orm model
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "users")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub uuid: Uuid,
    pub name: String,
    pub email: String,
}

// Your sea-orm active model
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

// Define the factory
define_factory! {
    create_user => users::Model {
        active_model: users::ActiveModel,
        fields: {
            name: String = "Test User".to_string(),
            email: String = format!("user_{}@example.com", Uuid::new_v4()),
            uuid: Uuid = Uuid::new_v4(),
        }
    }
}

3. Use your factories in tests

You can now use the generated factory function and builder to create test data.

#[cfg(test)]
mod tests {
    use super::*;
    use sea_orm::{Database, DatabaseConnection};

    async fn setup_test_db() -> DatabaseConnection {
        // Your test database setup
    }

    #[tokio::test]
    async fn test_create_user() {
        let db = setup_test_db().await;

        // Create a user with default values
        let user = create_user(&db).await.unwrap();
        assert_eq!(user.name, "Test User");

        // Create a user with custom values using the builder
        let custom_user = create_user_builder()
            .name("Jane Doe".to_string())
            .email("jane.doe@example.com".to_string())
            .create(&db)
            .await
            .unwrap();
        assert_eq!(custom_user.name, "Jane Doe");
    }
}

Dependencies

This project has the following dependencies:

  • sea-orm
  • tokio
  • uuid
  • chrono
  • paste

To install the dependencies, you need to have Rust and Cargo installed on your system. You can install Rust by following the instructions on the official website: https://www.rust-lang.org/tools/install

Once you have Rust and Cargo installed, you can build the project and install the dependencies by running the following command in the project's root directory:

cargo build

Testing

The project includes a comprehensive test suite that demonstrates the macro's functionality and serves as a practical usage guide. The tests cover:

  • Default Values: Creating models with default factory values.
  • Custom Values: Overriding default values using the builder pattern.
  • Multiple Factories: Defining and using multiple factories.
  • Edge Cases: Handling special characters and ensuring UUID uniqueness.

To run the tests, use the following command:

cargo test

How it Works

The define_factory! macro generates the following:

  • A factory function (e.g., create_user) that creates and saves a model with default values.
  • A builder struct (e.g., CreateUserBuilder) that allows you to customize the model's fields.
  • A helper function (e.g., create_user_builder) to create a new builder instance.

The builder's create method saves the model to the database, while the build method returns an ActiveModel instance without saving it.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

This project is licensed under the MIT License.

Commit count: 0

cargo fmt