Clean up our quickcheck usage in tests

This wasn't possible when I added these types, as our tests were still
part of the same project. Now that they have their own Cargo.toml file,
I can add quickcheck as an optional dependency.
This commit is contained in:
Sean Griffin 2015-12-01 13:09:29 -07:00
parent eef7af937f
commit aab253e2f1
6 changed files with 53 additions and 46 deletions

View File

@ -9,6 +9,12 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
* `#[derive(Queriable)]` now allows generic parameters on the struct.
### Added
* Quickcheck is now an optional dependency. When `features = ["quickcheck"]` is
added to `Cargo.toml`, you'll gain `Arbitrary` implementations for everything
in `diesel::data_types`.
## [0.2.0] - 2015-11-30
### Added

View File

@ -13,11 +13,11 @@ keywords = ["orm", "database", "postgres", "postgresql", "sql"]
libc = "0.2.*"
pq-sys = "0.2.*"
byteorder = "0.3.*"
quickcheck = { version = "^0.2.24", optional = true }
[dev-dependencies]
quickcheck = "*"
quickcheck = "^0.2.24"
dotenv = "0.4.0"
[features]
unstable = []
nightly = []

View File

@ -10,6 +10,9 @@ use query_source::Queriable;
use super::option::UnexpectedNullError;
use types::{self, NativeSqlType, FromSql, ToSql, IsNull};
#[cfg(feature = "quickcheck")]
mod quickcheck_impls;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// Timestamps are represented in Postgres as a 32 bit signed integer representing the number of
/// microseconds since January 1st 2000. This struct is a dumb wrapper type, meant only to indicate

View File

@ -0,0 +1,37 @@
extern crate quickcheck;
use self::quickcheck::{Arbitrary, Gen};
use super::{PgDate, PgTime, PgTimestamp, PgInterval};
impl Arbitrary for PgDate {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
PgDate(i32::arbitrary(g))
}
}
impl Arbitrary for PgTime {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let mut time = -1;
while time < 0 {
time = i64::arbitrary(g);
}
PgTime(time)
}
}
impl Arbitrary for PgTimestamp {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
PgTimestamp(i64::arbitrary(g))
}
}
impl Arbitrary for PgInterval {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
PgInterval {
microseconds: i64::arbitrary(g),
days: i32::arbitrary(g),
months: i32::arbitrary(g),
}
}
}

View File

@ -11,7 +11,7 @@ syntex = { version = "^0.22.0", optional = true }
diesel_codegen = { path = "../diesel_codegen", default-features = false }
[dependencies]
diesel = { path = "../diesel" }
diesel = { path = "../diesel", features = ["quickcheck"] }
diesel_codegen = { path = "../diesel_codegen", default-features = false }
compiletest_rs = { version = "^0.0.10", optional = true }
syntex = { version = "^0.22.0", optional = true }

View File

@ -53,46 +53,7 @@ test_round_trip!(f64_roundtrips, Double, f64, "double precision");
test_round_trip!(string_roundtrips, VarChar, String, "varchar");
test_round_trip!(text_roundtrips, Text, String, "text");
test_round_trip!(binary_roundtrips, Binary, Vec<u8>, "bytea");
macro_rules! test_newtype_round_trip {
($test_name:ident, $sql_type:ident, $newtype:ident, $tpe:ty, $sql_type_name:expr) => {
#[test]
fn $test_name() {
fn round_trip(val: $tpe) -> bool {
test_type_round_trips::<types::$sql_type, _>($newtype(val), $sql_type_name)
}
fn option_round_trip(val: Option<$tpe>) -> bool {
let val = val.map($newtype);
test_type_round_trips::<Nullable<types::$sql_type>, _>(val, $sql_type_name)
}
fn vec_round_trip(val: Vec<$tpe>) -> bool {
let val: Vec<_> = val.into_iter().map($newtype).collect();
test_type_round_trips::<Array<types::$sql_type>, _>(val, concat!($sql_type_name, "[]"))
}
quickcheck(round_trip as fn($tpe) -> bool);
quickcheck(option_round_trip as fn(Option<$tpe>) -> bool);
quickcheck(vec_round_trip as fn(Vec<$tpe>) -> bool);
}
}
}
test_newtype_round_trip!(date_roundtrips, Date, PgDate, i32, "date");
test_newtype_round_trip!(time_roundtrips, Time, to_pg_time, i64, "time");
test_newtype_round_trip!(timestamp_roundtrips, Timestamp, PgTimestamp, i64, "timestamp");
test_newtype_round_trip!(interval_roundtrips, Interval, to_pg_interval, (i64, i32, i32), "interval");
fn to_pg_time(int: i64) -> PgTime {
PgTime(::std::cmp::max(0, int))
}
fn to_pg_interval(vals: (i64, i32, i32)) -> PgInterval {
let (microseconds, days, months) = vals;
PgInterval {
microseconds: microseconds,
days: days,
months: months,
}
}
test_round_trip!(date_roundtrips, Date, PgDate, "date");
test_round_trip!(time_roundtrips, Time, PgTime, "time");
test_round_trip!(timestamp_roundtrips, Timestamp, PgTimestamp, "timestamp");
test_round_trip!(interval_roundtrips, Interval, PgInterval, "interval");