Crate diesel_factories

source ·
Expand description
#[macro_use]
extern crate diesel;

use diesel::prelude::*;
use diesel::pg::PgConnection;
use diesel_factories::{Factory, InsertFactory, DefaultFactory};

// Tell Diesel what our schema is
table! {
    users (id) {
        id -> Integer,
        name -> Text,
        age -> Integer,
    }
}

// Setup the model. We have to implement `Identifiable`.
#[derive(Queryable, Identifiable)]
pub struct User {
    pub id: i32,
    pub name: String,
    pub age: i32,
}

// On a normal Diesel `Insertable` you can derive `Factory`
#[derive(Insertable, Factory)]
#[table_name = "users"]
// And specify which model type the factory is for
#[factory_model(User)]
pub struct UserFactory {
    name: String,
    age: i32,
}

// Set default values. If you don't implement `Default` it wont work.
impl Default for UserFactory {
    fn default() -> UserFactory {
        UserFactory {
            name: "Bob".into(),
            age: 30,
        }
    }
}

fn main() {
    use self::users::dsl::*;

    // Connect to the database
    let database_url = "postgres://localhost/diesel_factories_test";
    let con = PgConnection::establish(&database_url).unwrap();

    // Create a new user using our factory, overriding the default name
    let user = User::default_factory().name("Alice").insert(&con);
    assert_eq!("Alice", user.name);
    assert_eq!(30, user.age);

    // Verifing that the user is in fact in the database
    let user_from_db = users
            .filter(id.eq(user.id))
            .first::<User>(&con)
            .unwrap();
    assert_eq!("Alice", user_from_db.name);
    assert_eq!(30, user_from_db.age);
}

Traits

Indicate which factory is the default for a given model type.
Method for inserting a factory object into the database.

Derive Macros

See the docs for “diesel_factories” for more info about this.