beaver is a library for setting up Rust objects inspired by factory_bot.

Related tags

Testing rust factory test
Overview

logo

beaver is a library for setting up Rust objects inspired by factory_bot.

github workflow status crates docs

Usage | Examples | Docs

Dependencies

[dependencies]
beaver = "1"
serde = { version = "1.0", features = ["derive"] }

If you want to use chrono for your struct fields, Cargo.toml would look like this.

[dependencies]
beaver = "1"
serde = { version = "1.0", features = ["derive"] }
# you need `serde` feature.
chrono = { version = "0.4", features = ["serde"] }

Usage

Quickstart

|_| false, } } fn main() { let post_factory = PostFactory::new(); let post1 = post_factory.build(|_| {}); let post2 = post_factory.build(|_| {}); println!("{:?}", post1); println!("{:?}", post2); }">
use serde::{Deserialize, Serialize};

// `Post` needs both of `Serialize` and `Deserialize`.
#[derive(Serialize, Deserialize, Debug)]
struct Post {
    id: u16,
    title: String,
    approved: bool,
}

beaver::define! {
    PostFactory (Post) {
        id -> |n| n,
        title -> |n| format!("post-{}", n),
        approved -> |_| false,
    }
}

fn main() {
    let post_factory = PostFactory::new();
    let post1 = post_factory.build(|_| {});
    let post2 = post_factory.build(|_| {});
    println!("{:?}", post1);
    println!("{:?}", post2);
}

Define a factory

|_| false, } }">
beaver::define! {
    // [factory name] (struct)
    PostFactory (Post) {
        // `n` is a sequence number.
        id -> |n| n,
        title -> |n| format!("{}", n),
        approved -> |_| false,
    }
}

This define! macro defines a struct, PostFactory as a factory. If you want to use factories outside modules, you need to make both of factories and structs public. For more information, please see this example.

Build structs

// initialize a factory.
let post_factory = PostFactory::new();

// build a `Post`.
post_factory.build(|_| {});

// build a vector of some `Posts`.
post_factory.build_list(3, |_| {});

// override attributes of a factory.
post_factory.build(|post| {
    post.id = 1024;
    post.title = "foo bar".to_string()
});

Examples

Public factory

|_| false, created_at -> |_| NaiveDate::from_ymd(2020, 1, 1).and_hms(0, 0, 0), } } } fn main() { use factory::PostFactory; let post_factory = PostFactory::new(); let post1 = post_factory.build(|_| {}); let post2 = post_factory.build(|_| {}); println!("{:?}\n{:?}", post1, post2); }">
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};

// `Post` needs to be public.
#[derive(Serialize, Deserialize, Debug)]
pub struct Post {
    id: u16,
    title: String,
    approved: bool,
    created_at: NaiveDateTime,
}

mod factory {
    use crate::Post;
    use chrono::NaiveDate;

    beaver::define! {
        // `PostFactory` needs to be public.
        pub PostFactory (Post) {
            id -> |n| n,
            title -> |n| format!("post-{}", n),
            approved -> |_| false,
            created_at -> |_| NaiveDate::from_ymd(2020, 1, 1).and_hms(0, 0, 0),
        }
    }
}

fn main() {
    use factory::PostFactory;

    let post_factory = PostFactory::new();
    let post1 = post_factory.build(|_| {});
    let post2 = post_factory.build(|_| {});
    println!("{:?}\n{:?}", post1, post2);
}

Output:

Post { id: 1, title: "post-1", approved: false, created_at: 2020-01-01T00:00:00 }
Post { id: 2, title: "post-2", approved: false, created_at: 2020-01-01T00:00:00 }

Sub factory vector

|_| true, // use `build_list` tags -> |n| TagFactory::build_list(3, n), } } beaver::define! { TagFactory (Tag) { id -> |n| beaver::sequence(100, n), name -> |n| format!("tag-{}", n), } } } fn main() { use factory::PostFactory; let post_factory = PostFactory::new(); let post1 = post_factory.build(|_| {}); let post2 = post_factory.build(|_| {}); println!("{:?}\n{:?}", post1, post2); let posts = post_factory.build_list(3, |_| {}); for post in posts { println!("{:?}", post); } }">
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct Post {
    id: u16,
    title: String,
    approved: bool,
    tags: Vec<Tag>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Tag {
    id: u16,
    name: String,
}

mod factory {
    use crate::Post;
    use crate::Tag;

    beaver::define! {
        pub PostFactory (Post) {
            id -> |n| n,
            title -> |n| format!("post-{}", n),
            approved -> |_| true,
            // use `build_list`
            tags -> |n| TagFactory::build_list(3, n),
        }
    }

    beaver::define! {
        TagFactory (Tag) {
            id -> |n| beaver::sequence(100, n),
            name -> |n| format!("tag-{}", n),
        }
    }
}

fn main() {
    use factory::PostFactory;

    let post_factory = PostFactory::new();
    let post1 = post_factory.build(|_| {});
    let post2 = post_factory.build(|_| {});
    println!("{:?}\n{:?}", post1, post2);

    let posts = post_factory.build_list(3, |_| {});
    for post in posts {
        println!("{:?}", post);
    }
}

Output:

Post { id: 1, title: "post-1", approved: true, tags: [Tag { id: 1, name: "tag-1" }, Tag { id: 2, name: "tag-2" }, Tag { id: 3, name: "tag-3" }] }
Post { id: 2, title: "post-2", approved: true, tags: [Tag { id: 4, name: "tag-4" }, Tag { id: 5, name: "tag-5" }, Tag { id: 6, name: "tag-6" }] }
Post { id: 3, title: "post-3", approved: true, tags: [Tag { id: 7, name: "tag-7" }, Tag { id: 8, name: "tag-8" }, Tag { id: 9, name: "tag-9" }] }
Post { id: 4, title: "post-4", approved: true, tags: [Tag { id: 10, name: "tag-10" }, Tag { id: 11, name: "tag-11" }, Tag { id: 12, name: "tag-12" }] }
Post { id: 5, title: "post-5", approved: true, tags: [Tag { id: 13, name: "tag-13" }, Tag { id: 14, name: "tag-14" }, Tag { id: 15, name: "tag-15" }] }

Others

Contribution

Contributions, issues and pull requests are welcome!

License

Licensed under MIT license (LICENSE).

You might also like...
A tiny, super simple and portable benchmarking library.

benchmark-simple A tiny benchmarking library for Rust. Trivial to use Works pretty much everywhere, including WebAssembly (WASI, but also in-browser)

This is a tiny (but delightful!) utility library for exhaustive testing.

Exhaustigen This is a tiny (but delightful!) utility library for exhaustive testing. It is based (directly) on the idea and code in the following blog

A minimalist property-based testing library based on the arbitrary crate.

A minimalist property-based testing library based on the arbitrary crate.

A structure-aware HTTP fuzzing library
A structure-aware HTTP fuzzing library

πŸš€ FeroxFuzz πŸš€ A structure-aware HTTP fuzzing library πŸ€” Another ferox? why? πŸ€” Chill, it's not another command-line tool, this one's a library! 😁 M

An unofficial client library for the fuzz-introspector API.

fuzz-introspector-client An unofficial client library for the fuzz-introspector API. Quickstart Add package as a dependency; cargo add fuzz-introspect

Handle some lichess.org/tournament load with Rust, while learning Rust

lila-http Take some of the HTTP load away from lila. WIP! Arena tournaments Clients connected to a tournament page request new data about the tourname

Testing Framework for Rust
Testing Framework for Rust

Polish Polish is Test-Driven Development done right Getting Started Installing the Package The crates.io package is kept up-to-date with all the major

πŸ‡ Fuzzing Rust code with American Fuzzy Lop
πŸ‡ Fuzzing Rust code with American Fuzzy Lop

afl.rs Fuzzing Rust code with AFLplusplus What is it? Fuzz testing is a software testing technique used to find security and stability issues by provi

Travis CI and AppVeyor template to test your Rust crate on 5 architectures and publish binary releases of it for Linux, macOS and Windows

trust Travis CI and AppVeyor template to test your Rust crate on 5 architectures and publish binary releases of it for Linux, macOS and Windows Featur

Releases(v1.0.0)
Owner
Takayuki Maeda
Software Engineer?
Takayuki Maeda
Playwright is a rust library to automate Chromium, Firefox and WebKit built on top of Node.js library.

?? Playwright for Rust Playwright is a rust library to automate Chromium, Firefox and WebKit built on top of Node.js library. Installation [dependenci

octaltree 132 Jan 6, 2023
HTTP mocking library for Rust.

httpmock HTTP mocking library for Rust. Documentation Β· Crate Β· Report Bug Β· Request Feature Β· Changelog Features Simple, expressive, fluent API. Many

Alexander Liesenfeld 320 Dec 21, 2022
A library for generating fake data in Rust.

Fake A Rust library for generating fake data. Installation Default (rand is required): [dependencies] fake = "2.4" rand = "0.8" If you want to use #[d

cksac 552 Dec 25, 2022
Advanced Fuzzing Library - Slot your Fuzzer together in Rust! Scales across cores and machines. For Windows, Android, MacOS, Linux, no_std, ...

LibAFL, the fuzzer library. Advanced Fuzzing Library - Slot your own fuzzers together and extend their features using Rust. LibAFL is written and main

Advanced Fuzzing League ++ 1.2k Dec 29, 2022
QuickCheck bug hunting in Rust standard library data structures

BugHunt, Rust This project is aiming to provide "stateful" QuickCheck models for Rust's standard library. That is, we build up a random list of operat

Brian L. Troutwine 161 Dec 15, 2022
Rust testing library

K9 - Rust Testing Library Snapshot testing + better assertions Available test macros snapshot assert_equal assert_greater_than assert_greater_than_or_

Aaron Abramov 269 Dec 10, 2022
Struct mocking library for Rust

faux   A library to create mocks out of structs. faux allows you to mock the methods of structs for testing without complicating or polluting your cod

Andres 322 Dec 20, 2022
Rustress - stress testing library in Rust. For fun

rustress Simple network stress testing library. To get familiar with Rust Planned features (Subject to change) Multithreaded client/server Throughput

Hakan SΓΆnmez 7 Sep 22, 2022
insta: a snapshot testing library for Rust

insta: a snapshot testing library for Rust Introduction Snapshots tests (also sometimes called approval tests) are tests that assert values against a

Armin Ronacher 1.4k Jan 1, 2023
Simple assertion library for unit testing in python with a fluent API

Simple assertions library for unit testing in Python with a nice fluent API. Supports both Python 2 and 3.

snakedye 19 Sep 10, 2022