7个版本 (3个稳定版)
2.0.0 | 2020年11月9日 |
---|---|
1.0.1 | 2020年7月11日 |
1.0.0 | 2019年6月30日 |
0.1.2 | 2019年6月19日 |
0.0.1 | 2018年12月9日 |
#2345 在 数据库接口
每月331次下载
21KB
132 行
diesel-factories
为与Diesel一起工作而设计的测试工厂模式的实现。
查看文档以获取更多信息。
lib.rs
:
这是为与Diesel一起工作而实现的测试工厂模式的实现。
示例用法
#[macro_use]
extern crate diesel;
use diesel_factories::{Association, Factory};
use diesel::{pg::PgConnection, prelude::*};
// Tell Diesel what our schema is
// Note unusual primary key name - see options for derive macro.
mod schema {
table! {
countries (identity) {
identity -> Integer,
name -> Text,
}
}
table! {
cities (id) {
id -> Integer,
name -> Text,
country_id -> Integer,
}
}
}
// Our city model
#[derive(Clone, Queryable)]
struct City {
pub id: i32,
pub name: String,
pub country_id: i32,
}
#[derive(Clone, Factory)]
#[factory(
// model type our factory inserts
model = City,
// table the model belongs to
table = crate::schema::cities,
// connection type you use. Defaults to `PgConnection`
connection = diesel::pg::PgConnection,
// type of primary key. Defaults to `i32`
id = i32,
)]
struct CityFactory<'a> {
pub name: String,
// A `CityFactory` is associated to either an inserted `&'a Country` or a `CountryFactory`
// instance.
pub country: Association<'a, Country, CountryFactory>,
}
// We make new factory instances through the `Default` trait
impl<'a> Default for CityFactory<'a> {
fn default() -> Self {
Self {
name: "Copenhagen".to_string(),
// `default` will return an `Association` with a `CountryFactory`. No inserts happen
// here.
//
// This is the same as `Association::Factory(CountryFactory::default())`.
country: Association::default(),
}
}
}
// The same setup, but for `Country`
#[derive(Clone, Queryable)]
struct Country {
pub identity: i32,
pub name: String,
}
#[derive(Clone, Factory)]
#[factory(
model = Country,
table = crate::schema::countries,
connection = diesel::pg::PgConnection,
id = i32,
id_name = identity,
)]
struct CountryFactory {
pub name: String,
}
impl Default for CountryFactory {
fn default() -> Self {
Self {
name: "Denmark".into(),
}
}
}
// Usage
fn basic_usage() {
let con = establish_connection();
let city = CityFactory::default().insert(&con);
assert_eq!("Copenhagen", city.name);
let country = find_country_by_id(city.country_id, &con);
assert_eq!("Denmark", country.name);
assert_eq!(1, count_cities(&con));
assert_eq!(1, count_countries(&con));
}
fn setting_fields() {
let con = establish_connection();
let city = CityFactory::default()
.name("Amsterdam")
.country(CountryFactory::default().name("Netherlands"))
.insert(&con);
assert_eq!("Amsterdam", city.name);
let country = find_country_by_id(city.country_id, &con);
assert_eq!("Netherlands", country.name);
assert_eq!(1, count_cities(&con));
assert_eq!(1, count_countries(&con));
}
fn multiple_models_with_same_association() {
let con = establish_connection();
let netherlands = CountryFactory::default()
.name("Netherlands")
.insert(&con);
let amsterdam = CityFactory::default()
.name("Amsterdam")
.country(&netherlands)
.insert(&con);
let hague = CityFactory::default()
.name("The Hague")
.country(&netherlands)
.insert(&con);
assert_eq!(amsterdam.country_id, hague.country_id);
assert_eq!(2, count_cities(&con));
assert_eq!(1, count_countries(&con));
}
#
#
#
// Utility functions just for demo'ing
fn count_cities(con: &PgConnection) -> i64 {
use crate::schema::cities;
use diesel::dsl::count_star;
cities::table.select(count_star()).first(con).unwrap()
}
fn count_countries(con: &PgConnection) -> i64 {
use crate::schema::countries;
use diesel::dsl::count_star;
countries::table.select(count_star()).first(con).unwrap()
}
fn find_country_by_id(input: i32, con: &PgConnection) -> Country {
use crate::schema::countries::dsl::*;
countries
.filter(identity.eq(&input))
.first::<Country>(con)
.unwrap()
}
#[derive(Factory)]
属性
这些属性可以在结构体内部使用,在 #[factory(...)]
上。
名称 | 描述 | 示例 | 默认 |
---|---|---|---|
model |
工厂插入的模型类型 | City |
None,必需 |
table |
模型所属的表 | crate::schema::cities |
None,必需 |
connection |
应用程序使用的连接类型 | MysqlConnection |
diesel::pg::PgConnection |
id |
您表的主键类型 | i64 |
i32 |
id_name |
您表的主键列名称 | identity |
id |
这些属性可以在 #[factory(...)]
上的关联字段中使用。
名称 | 描述 | 示例 | 默认 |
---|---|---|---|
foreign_key_name |
您模型上的外键列名称 | country_identity |
{association_name}_id |
构建方法
除了为您的结构体实现 Factory
外,它还会派生构建方法以轻松自定义每个字段。生成的代码看起来像这样
struct CountryFactory {
pub name: String,
}
// This is what gets generated for each field
impl CountryFactory {
fn name<T: Into<String>>(mut self, new: T) -> Self {
self.name = new.into();
self
}
}
#
// So you can do this
CountryFactory::default().name("Amsterdam");
关联字段的构建方法
为 Association
字段生成的构建方法略有不同。如果您有一个类似于
#
#[derive(Clone, Factory)]
#[factory(
model = City,
table = crate::schema::cities,
)]
struct CityFactory<'a> {
pub name: String,
pub country: Association<'a, Country, CountryFactory>,
}
#
#
的工厂
#
#
#
let country_factory = CountryFactory::default();
CityFactory::default().country(country_factory);
您将能够调用 country
,要么使用所有权的 CountryFactory
#
#
#
let country = Country { id: 1, name: "Denmark".into() };
CityFactory::default().country(&country);
或者借用的 Country
这将防止在测试过程中出现您有多个工厂实例共享某些关联关系,然后在测试中途修改这些关联关系的错误。
可选关联
#
#[derive(Clone, Factory)]
#[factory(
model = User,
table = crate::schema::users,
)]
struct UserFactory<'a> {
pub name: String,
pub country: Option<Association<'a, Country, CountryFactory>>,
}
impl<'a> Default for UserFactory<'a> {
fn default() -> Self {
Self {
name: "Bob".into(),
country: None,
}
}
}
// Setting `country` to a `CountryFactory`
let country_factory = CountryFactory::default();
UserFactory::default().country(Some(country_factory));
// Setting `country` to a `Country`
let country = Country { id: 1, name: "Denmark".into() };
UserFactory::default().country(Some(&country));
// Setting `country` to `None`
UserFactory::default().country(Option::<CountryFactory>::None);
UserFactory::default().country(Option::<&Country>::None);
自定义外键名称
您可以根据以下方式自定义关联关系的外键名称
#
#[derive(Clone, Factory)]
#[factory(
model = City,
table = crate::schema::cities,
)]
struct CityFactory<'a> {
#[factory(foreign_key_name = country_id)]
pub country: Association<'a, Country, CountryFactory>,
}
#
#
依赖项
~4MB
~81K SLoC