1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! # Factories
//!
//! Factories generate test data by creating model instances with sensible
//! defaults. They provide a fluent builder API for overriding specific fields
//! while keeping the rest at their default values.
//!
//! ## Creating Models
//!
//! Use `Model::factory()` to get a factory, configure it with setter methods,
//! then call `create()` to persist the instance:
//!
//! ```rust
//! # extern crate fabrique;
//! # extern crate sqlx;
//! # extern crate tokio;
//! # extern crate uuid;
//! # use fabrique::prelude::*;
//! # use uuid::Uuid;
//! #
//! #[derive(Clone, Factory, Model)]
//! pub struct Product {
//! id: Uuid,
//! name: String,
//! price_cents: i32,
//! }
//!
//! # #[fabrique::doctest]
//! # async fn main(pool: Pool<sqlx::Sqlite>) -> Result<(), fabrique::Error> {
//! // Create with defaults
//! let product = Product::factory().create(&pool).await?;
//!
//! // Override specific fields
//! let expensive_product = Product::factory()
//! .name("Anvil 3000".to_owned())
//! .price_cents(9999)
//! .create(&pool)
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Belongs To Relationships
//!
//! Use `for_<relation>()` methods to link a factory to a parent model.
//! The method accepts either a factory (creates a new parent) or an existing
//! model instance (uses its primary key):
//!
//! ```rust
//! # extern crate fabrique;
//! # extern crate sqlx;
//! # extern crate tokio;
//! # extern crate uuid;
//! # use fabrique::prelude::*;
//! # use uuid::Uuid;
//! #
//! # #[derive(Clone, Factory, Model)]
//! # pub struct User {
//! # id: Uuid,
//! # name: String,
//! # email: String,
//! # }
//! #
//! #[derive(Clone, Factory, Model)]
//! pub struct Order {
//! id: Uuid,
//! #[fabrique(belongs_to = "User")]
//! user_id: Uuid,
//! }
//!
//! # #[fabrique::doctest]
//! # async fn main(pool: Pool<sqlx::Sqlite>) -> Result<(), fabrique::Error> {
//! // Creates a new User, then creates an Order linked to it
//! Order::factory()
//! .for_user(User::factory())
//! .create(&pool)
//! .await?;
//!
//! // Or use an existing user
//! let user = User::factory().create(&pool).await?;
//! Order::factory()
//! .for_user(user)
//! .create(&pool)
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Has Many Relationships
//!
//! Use `has_<relation>()` methods to create child models after the parent.
//! These methods are automatically generated from the child's `belongs_to`
//! declaration — no `HasMany<T>` field is needed on the parent.
//! Specify a child factory and the number of instances to create:
//!
//! ```rust
//! # extern crate fabrique;
//! # extern crate sqlx;
//! # extern crate tokio;
//! # extern crate uuid;
//! # use fabrique::prelude::*;
//! # use uuid::Uuid;
//!
//! #[derive(Clone, Factory, Model)]
//! pub struct User {
//! id: Uuid,
//! name: String,
//! email: String,
//! }
//!
//! #[derive(Clone, Factory, Model)]
//! pub struct Order {
//! id: Uuid,
//! #[fabrique(belongs_to = "User")]
//! user_id: Uuid,
//! }
//!
//! # #[fabrique::doctest]
//! # async fn main(pool: Pool<sqlx::Sqlite>) -> Result<(), fabrique::Error> {
//! // Creates a User, then creates 3 Orders linked to it
//! User::factory()
//! .has_orders(Order::factory(), 3)
//! .create(&pool)
//! .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Many-to-Many Relationships
//!
//! For many-to-many relationships, create instances through the join model
//! using `has_<join_model>()`:
//!
//! ```rust
//! # extern crate fabrique;
//! # extern crate sqlx;
//! # extern crate tokio;
//! # extern crate uuid;
//! # use fabrique::prelude::*;
//! # use uuid::Uuid;
//!
//! # #[derive(Clone, Factory, Model)]
//! # pub struct User {
//! # id: Uuid,
//! # name: String,
//! # email: String,
//! # }
//! #
//! #[derive(Clone, Factory, Model)]
//! pub struct Order {
//! id: Uuid,
//! #[fabrique(belongs_to = "User")]
//! user_id: Uuid,
//! }
//!
//! #[derive(Clone, Factory, Model)]
//! pub struct Product {
//! id: Uuid,
//! name: String,
//! price_cents: i32,
//! }
//!
//! #[derive(Clone, Factory, Model)]
//! #[fabrique(table = "order_lines")]
//! pub struct OrderLine {
//! #[fabrique(primary_key, belongs_to = "Order")]
//! order_id: Uuid,
//! #[fabrique(primary_key, belongs_to = "Product")]
//! product_id: Uuid,
//! quantity: i32,
//! unit_price_cents: i32,
//! }
//!
//! # #[fabrique::doctest]
//! # async fn main(pool: Pool<sqlx::Sqlite>) -> Result<(), fabrique::Error> {
//! // Creates an Order, then 2 OrderLines (each auto-creating a Product)
//! Order::factory()
//! .has_order_lines(OrderLine::factory(), 2)
//! .create(&pool)
//! .await?;
//! # Ok(())
//! # }
//! ```
pub use *;
pub use Factory;