| Crates.io | loco-factory |
| lib.rs | loco-factory |
| version | 0.1.0 |
| created_at | 2025-11-25 16:43:46.330986+00 |
| updated_at | 2025-11-25 16:43:46.330986+00 |
| description | It's a model data generator to sea_orm |
| homepage | |
| repository | https://github.com/ofabianomartins/loco_factory |
| max_upload_size | |
| id | 1950067 |
| size | 109,438 |
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.
sea-orm and your existing test suite.loco-factory to your projectAdd the following to your Cargo.toml file:
[dependencies]
loco-factory = "0.1.0"
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(),
}
}
}
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");
}
}
This project has the following dependencies:
sea-ormtokiouuidchronopasteTo 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
The project includes a comprehensive test suite that demonstrates the macro's functionality and serves as a practical usage guide. The tests cover:
To run the tests, use the following command:
cargo test
The define_factory! macro generates the following:
create_user) that creates and saves a model with default values.CreateUserBuilder) that allows you to customize the model's fields.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.
Contributions are welcome! Please feel free to submit a pull request or open an issue.
This project is licensed under the MIT License.