diff --git a/src/chrono_04.rs b/src/chrono_04.rs index be9a741..cbfb821 100644 --- a/src/chrono_04.rs +++ b/src/chrono_04.rs @@ -1,4 +1,4 @@ -use chrono_04::{DateTime, NaiveDateTime, TimeZone}; +use chrono_04::{DateTime, NaiveDateTime, TimeZone, NaiveDate}; use crate::{Normalizable, RangeBound, BoundSided}; @@ -20,4 +20,6 @@ impl Normalizable for NaiveDateTime { bound } -} \ No newline at end of file +} + +bounded_normalizable!(NaiveDate, ::chrono_04::Duration::days(1)); diff --git a/src/impls.rs b/src/impls.rs index 19615e1..92d3ff2 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -118,7 +118,7 @@ mod test { use postgres::{Client, NoTls}; use postgres::types::{FromSql, ToSql}; #[cfg(feature = "with-chrono-0_4")] - use chrono_04::{TimeZone, Utc, Duration}; + use chrono_04::{NaiveDate, NaiveTime, NaiveDateTime, TimeZone, Utc, Duration}; macro_rules! test_range { ($name:expr, $t:ty, $low:expr, $low_str:expr, $high:expr, $high_str:expr) => ({ @@ -144,7 +144,7 @@ mod test { fn test_type(sql_type: &str, checks: &[(T, S)]) where for<'a> - T: Sync + PartialEq + FromSql<'a> + ToSql, + T: Sync + PartialEq + FromSql<'a> + ToSql + fmt::Debug, S: fmt::Display { let mut conn = Client::connect("postgres://postgres@localhost", NoTls).unwrap(); @@ -152,11 +152,11 @@ mod test { let stmt = conn.prepare(&*format!("SELECT {}::{}", *repr, sql_type)) .unwrap(); let result = conn.query(&stmt, &[]).unwrap().iter().next().unwrap().get(0); - assert!(val == &result); + assert_eq!(val, &result, "'SELECT {repr}::{sql_type}'"); let stmt = conn.prepare(&*format!("SELECT $1::{}", sql_type)).unwrap(); let result = conn.query(&stmt, &[val]).unwrap().iter().next().unwrap().get(0); - assert!(val == &result); + assert_eq!(val, &result, "'SELECT $1::{sql_type}'"); } } @@ -173,16 +173,28 @@ mod test { #[test] #[cfg(feature = "with-chrono-0_4")] fn test_tsrange_params() { - let low = Utc.timestamp(0, 0); + let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap(); + let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap(); + + let low = NaiveDateTime::new(d, t); let high = low + Duration::days(10); - test_range!("TSRANGE", NaiveDateTime, low.naive_utc(), "1970-01-01", high.naive_utc(), "1970-01-11"); + test_range!("TSRANGE", NaiveDateTime, low, "2015-06-03T12:34:56.789", high, "2015-06-13T12:34:56.789"); } #[test] #[cfg(feature = "with-chrono-0_4")] fn test_tstzrange_params() { - let low = Utc.timestamp(0, 0); + let low = Utc.with_ymd_and_hms(2014, 7, 8, 9, 10, 11).unwrap(); + let high = low + Duration::days(10); + test_range!("TSTZRANGE", DateTime<_>, low, "2014-07-08T09:10:11Z", high, "2014-07-18T09:10:11Z"); + } + + + #[test] + #[cfg(feature = "with-chrono-0_4")] + fn test_daterange_params() { + let low = NaiveDate::from_ymd_opt(2015, 6, 4).unwrap(); let high = low + Duration::days(10); - test_range!("TSTZRANGE", DateTime, low, "1970-01-01", high, "1970-01-11"); + test_range!("DATERANGE", NaiveDate, low, "2015-06-04", high, "2015-06-14"); } } diff --git a/src/lib.rs b/src/lib.rs index a1d68a5..4bbf283 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,9 +5,6 @@ #[macro_use(to_sql_checked)] extern crate postgres_types; -#[cfg(feature = "with-chrono-0_4")] -mod chrono_04; - use std::cmp::Ordering; use std::fmt; use std::i32; @@ -106,8 +103,6 @@ macro_rules! range { ) } -mod impls; - /// A trait that normalizes a range bound for a type pub trait Normalizable: Sized { /// Given a range bound, returns the normalized version of that bound. For @@ -124,17 +119,19 @@ pub trait Normalizable: Sized { } macro_rules! bounded_normalizable { - ($t:ident) => ( + ($t:ident, $delta:expr) => ( impl Normalizable for $t { fn normalize(bound: RangeBound) -> RangeBound where S: BoundSided { + use $crate::{BoundSide::*, BoundType::*}; + match (::side(), bound.type_) { (Upper, Inclusive) => { assert!(bound.value != $t::MAX); - RangeBound::new(bound.value + 1, Exclusive) + RangeBound::new(bound.value + ($delta), Exclusive) } (Lower, Exclusive) => { assert!(bound.value != $t::MAX); - RangeBound::new(bound.value + 1, Inclusive) + RangeBound::new(bound.value + ($delta), Inclusive) } _ => bound } @@ -143,8 +140,13 @@ macro_rules! bounded_normalizable { ) } -bounded_normalizable!(i32); -bounded_normalizable!(i64); +bounded_normalizable!(i32, 1); +bounded_normalizable!(i64, 1); + +mod impls; + +#[cfg(feature = "with-chrono-0_4")] +mod chrono_04; /// The possible sides of a bound. #[derive(Debug, PartialEq, Eq, Clone, Copy)]