Skip to content

Commit aab253e

Browse files
committed
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.
1 parent eef7af9 commit aab253e

6 files changed

Lines changed: 53 additions & 46 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
99

1010
* `#[derive(Queriable)]` now allows generic parameters on the struct.
1111

12+
### Added
13+
14+
* Quickcheck is now an optional dependency. When `features = ["quickcheck"]` is
15+
added to `Cargo.toml`, you'll gain `Arbitrary` implementations for everything
16+
in `diesel::data_types`.
17+
1218
## [0.2.0] - 2015-11-30
1319

1420
### Added

diesel/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ keywords = ["orm", "database", "postgres", "postgresql", "sql"]
1313
libc = "0.2.*"
1414
pq-sys = "0.2.*"
1515
byteorder = "0.3.*"
16+
quickcheck = { version = "^0.2.24", optional = true }
1617

1718
[dev-dependencies]
18-
quickcheck = "*"
19+
quickcheck = "^0.2.24"
1920
dotenv = "0.4.0"
2021

2122
[features]
2223
unstable = []
23-
nightly = []

diesel/src/types/impls/date_and_time.rs renamed to diesel/src/types/impls/date_and_time/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use query_source::Queriable;
1010
use super::option::UnexpectedNullError;
1111
use types::{self, NativeSqlType, FromSql, ToSql, IsNull};
1212

13+
#[cfg(feature = "quickcheck")]
14+
mod quickcheck_impls;
15+
1316
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1417
/// Timestamps are represented in Postgres as a 32 bit signed integer representing the number of
1518
/// microseconds since January 1st 2000. This struct is a dumb wrapper type, meant only to indicate
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
extern crate quickcheck;
2+
3+
use self::quickcheck::{Arbitrary, Gen};
4+
5+
use super::{PgDate, PgTime, PgTimestamp, PgInterval};
6+
7+
impl Arbitrary for PgDate {
8+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
9+
PgDate(i32::arbitrary(g))
10+
}
11+
}
12+
13+
impl Arbitrary for PgTime {
14+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
15+
let mut time = -1;
16+
while time < 0 {
17+
time = i64::arbitrary(g);
18+
}
19+
PgTime(time)
20+
}
21+
}
22+
23+
impl Arbitrary for PgTimestamp {
24+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
25+
PgTimestamp(i64::arbitrary(g))
26+
}
27+
}
28+
29+
impl Arbitrary for PgInterval {
30+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
31+
PgInterval {
32+
microseconds: i64::arbitrary(g),
33+
days: i32::arbitrary(g),
34+
months: i32::arbitrary(g),
35+
}
36+
}
37+
}

diesel_tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ syntex = { version = "^0.22.0", optional = true }
1111
diesel_codegen = { path = "../diesel_codegen", default-features = false }
1212

1313
[dependencies]
14-
diesel = { path = "../diesel" }
14+
diesel = { path = "../diesel", features = ["quickcheck"] }
1515
diesel_codegen = { path = "../diesel_codegen", default-features = false }
1616
compiletest_rs = { version = "^0.0.10", optional = true }
1717
syntex = { version = "^0.22.0", optional = true }

diesel_tests/tests/types_roundtrip.rs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -53,46 +53,7 @@ test_round_trip!(f64_roundtrips, Double, f64, "double precision");
5353
test_round_trip!(string_roundtrips, VarChar, String, "varchar");
5454
test_round_trip!(text_roundtrips, Text, String, "text");
5555
test_round_trip!(binary_roundtrips, Binary, Vec<u8>, "bytea");
56-
57-
macro_rules! test_newtype_round_trip {
58-
($test_name:ident, $sql_type:ident, $newtype:ident, $tpe:ty, $sql_type_name:expr) => {
59-
#[test]
60-
fn $test_name() {
61-
fn round_trip(val: $tpe) -> bool {
62-
test_type_round_trips::<types::$sql_type, _>($newtype(val), $sql_type_name)
63-
}
64-
65-
fn option_round_trip(val: Option<$tpe>) -> bool {
66-
let val = val.map($newtype);
67-
test_type_round_trips::<Nullable<types::$sql_type>, _>(val, $sql_type_name)
68-
}
69-
70-
fn vec_round_trip(val: Vec<$tpe>) -> bool {
71-
let val: Vec<_> = val.into_iter().map($newtype).collect();
72-
test_type_round_trips::<Array<types::$sql_type>, _>(val, concat!($sql_type_name, "[]"))
73-
}
74-
75-
quickcheck(round_trip as fn($tpe) -> bool);
76-
quickcheck(option_round_trip as fn(Option<$tpe>) -> bool);
77-
quickcheck(vec_round_trip as fn(Vec<$tpe>) -> bool);
78-
}
79-
}
80-
}
81-
82-
test_newtype_round_trip!(date_roundtrips, Date, PgDate, i32, "date");
83-
test_newtype_round_trip!(time_roundtrips, Time, to_pg_time, i64, "time");
84-
test_newtype_round_trip!(timestamp_roundtrips, Timestamp, PgTimestamp, i64, "timestamp");
85-
test_newtype_round_trip!(interval_roundtrips, Interval, to_pg_interval, (i64, i32, i32), "interval");
86-
87-
fn to_pg_time(int: i64) -> PgTime {
88-
PgTime(::std::cmp::max(0, int))
89-
}
90-
91-
fn to_pg_interval(vals: (i64, i32, i32)) -> PgInterval {
92-
let (microseconds, days, months) = vals;
93-
PgInterval {
94-
microseconds: microseconds,
95-
days: days,
96-
months: months,
97-
}
98-
}
56+
test_round_trip!(date_roundtrips, Date, PgDate, "date");
57+
test_round_trip!(time_roundtrips, Time, PgTime, "time");
58+
test_round_trip!(timestamp_roundtrips, Timestamp, PgTimestamp, "timestamp");
59+
test_round_trip!(interval_roundtrips, Interval, PgInterval, "interval");

0 commit comments

Comments
 (0)