use jiff::SignedDuration;
use super::SqlDurationComparison;
use crate::{DurationFilter, DurationOutOfRange};
fn nanos(value: SignedDuration) -> Result<i64, DurationOutOfRange> {
DurationFilter::operand_nanos(value)
}
#[test]
fn a_parameterless_filter_crosses_into_a_parameterised_comparison() {
let filter = DurationFilter::new()
.with_gte(SignedDuration::from_millis(500))
.with_in_([SignedDuration::from_secs(1), SignedDuration::ZERO]);
let crossed = SqlDurationComparison::<i64, ()>::try_from_filter_with(filter, nanos).unwrap();
assert_eq!(crossed.gte(), Some(&500_000_000));
assert_eq!(crossed.in_(), Some(&[1_000_000_000i64, 0][..]));
}
#[test]
fn an_unstorable_duration_is_refused_at_the_crossing() {
let ceiling = SignedDuration::new(9_223_372_036, 854_775_807);
let floor = SignedDuration::new(-9_223_372_036, -854_775_808);
for (edge, stored) in [(ceiling, i64::MAX), (floor, i64::MIN)] {
let crossed = SqlDurationComparison::<i64, ()>::try_from_filter_with(
DurationFilter::new().with_eq(edge),
nanos,
);
assert_eq!(crossed.unwrap().eq(), Some(&stored));
}
for past in [
ceiling + SignedDuration::from_nanos(1),
floor - SignedDuration::from_nanos(1),
] {
let refused = SqlDurationComparison::<i64, ()>::try_from_filter_with(
DurationFilter::new().with_eq(past),
nanos,
);
assert_eq!(refused.unwrap_err(), DurationOutOfRange);
}
}
#[test]
fn a_list_crossing_stops_at_the_first_refusal() {
let over = SignedDuration::new(9_223_372_036, 854_775_807) + SignedDuration::from_nanos(1);
let filter =
DurationFilter::new().with_in_([SignedDuration::from_secs(1), over, SignedDuration::ZERO]);
let refused = SqlDurationComparison::<i64, ()>::try_from_filter_with(filter, nanos);
assert_eq!(refused.unwrap_err(), DurationOutOfRange);
}
#[test]
fn an_empty_membership_list_stays_present() {
let filter = DurationFilter::new().with_in_(Vec::<SignedDuration>::new());
let crossed = SqlDurationComparison::<i64, ()>::try_from_filter_with(filter, nanos).unwrap();
assert_eq!(crossed.in_(), Some(&[][..]));
assert!(!crossed.is_empty());
assert_eq!(crossed.conjuncts(), 1);
}
#[cfg(feature = "postgres")]
mod postgres {
use core::time::Duration;
use jiff::SignedDuration;
use sqlx::postgres::types::PgInterval;
use super::{SqlDurationComparison, nanos};
use crate::{
DurationFilter, PgDuration, PgDurationError,
comparison::{
TryFromFilter,
sql::tests::{render, threaded},
},
};
#[test]
fn every_operator_renders_its_own_conjunct() {
let filter = DurationFilter::new()
.with_eq(SignedDuration::from_secs(1))
.with_ne(SignedDuration::from_secs(2))
.with_gt(SignedDuration::from_secs(3))
.with_gte(SignedDuration::from_secs(4))
.with_lt(SignedDuration::from_secs(5))
.with_lte(SignedDuration::from_secs(6))
.with_in_([SignedDuration::from_secs(7), SignedDuration::from_secs(8)])
.with_not_in([SignedDuration::from_secs(9)]);
let crossed =
SqlDurationComparison::<PgDuration, sqlx::Postgres>::try_from_filter(filter).unwrap();
assert_eq!(crossed.conjuncts(), 8);
assert_eq!(
render::<sqlx::Postgres, _>("len", &crossed),
(
"\"len\" = $1 AND NOT ( \"len\" = $2 ) AND \"len\" > $3 AND \"len\" >= $4 AND \
\"len\" < $5 AND \"len\" <= $6 AND \"len\" IN ($7, $8) AND NOT ( \"len\" IN ($9) )"
.to_owned(),
9
)
);
}
#[test]
fn a_native_interval_column_crosses_through_a_checked_conversion() {
let crossed = SqlDurationComparison::<PgDuration, sqlx::Postgres>::try_from_filter(
DurationFilter::new().with_lte(SignedDuration::from_secs(90)),
)
.expect("90 seconds is an interval");
assert_eq!(
crossed.lte().map(|stored| stored.get()),
Some(PgInterval {
months: 0,
days: 0,
microseconds: 90_000_000,
})
);
assert_eq!(
render::<sqlx::Postgres, _>("len", &crossed),
("\"len\" <= $1".to_owned(), 1)
);
let negative = SqlDurationComparison::<PgDuration, sqlx::Postgres>::try_from_filter(
DurationFilter::new().with_gte(SignedDuration::from_secs(-2)),
)
.expect("an `INTERVAL` is signed");
assert_eq!(
negative.gte().map(|stored| stored.get().microseconds),
Some(-2_000_000)
);
}
#[test]
fn the_carrier_refuses_sub_microsecond_precision_and_a_count_past_the_field() {
assert_eq!(
PgDuration::try_from(SignedDuration::from_nanos(1_500)).unwrap_err(),
PgDurationError::Nanoseconds(500)
);
assert_eq!(
PgDuration::try_from(SignedDuration::from_nanos(-1_500)).unwrap_err(),
PgDurationError::Nanoseconds(500)
);
let ceiling = SignedDuration::new(9_223_372_036_854, 775_807_000);
assert_eq!(
PgDuration::try_from(ceiling).unwrap().get().microseconds,
i64::MAX
);
let floor = SignedDuration::new(-9_223_372_036_854, -775_808_000);
assert_eq!(
PgDuration::try_from(floor).unwrap().get().microseconds,
i64::MIN
);
for past in [
ceiling + SignedDuration::from_micros(1),
floor - SignedDuration::from_micros(1),
] {
assert_eq!(
PgDuration::try_from(past).unwrap_err(),
PgDurationError::Range
);
}
assert_eq!(
PgDuration::try_from(SignedDuration::new(i64::MAX, 999_999_000)).unwrap_err(),
PgDurationError::Range
);
assert_eq!(
PgDuration::try_from(SignedDuration::MAX).unwrap_err(),
PgDurationError::Nanoseconds(999)
);
let refused = SqlDurationComparison::<PgDuration, sqlx::Postgres>::try_from_filter(
DurationFilter::new().with_in_([
SignedDuration::from_secs(1),
SignedDuration::from_nanos(1_500),
]),
);
assert_eq!(
refused.unwrap_err(),
PgDurationError::Nanoseconds(500),
"the first refusal in a list is the answer"
);
}
#[test]
fn a_declared_interval_converter_is_the_route_to_a_mixed_field_interval() {
fn as_months(value: SignedDuration) -> Result<PgInterval, &'static str> {
let days = value.as_secs() / 86_400;
let months = i32::try_from(days / 30).map_err(|_| "too many months")?;
Ok(PgInterval {
months,
days: i32::try_from(days % 30).map_err(|_| "too many days")?,
microseconds: 0,
})
}
let crossed = SqlDurationComparison::<PgInterval, sqlx::Postgres>::try_from_filter_with(
DurationFilter::new().with_gte(SignedDuration::from_hours(24 * 31)),
as_months,
)
.expect("the declared converter decides the fields");
assert_eq!(
crossed.gte(),
Some(&PgInterval {
months: 1,
days: 1,
microseconds: 0,
})
);
assert_eq!(
render::<sqlx::Postgres, _>("len", &crossed),
("\"len\" >= $1".to_owned(), 1)
);
let plain = PgDuration::try_from(SignedDuration::from_hours(24 * 31)).unwrap();
assert_eq!(plain.get().months, 0);
assert_eq!(plain.get().days, 0);
assert_eq!(plain.get().microseconds, 2_678_400_000_000);
}
#[test]
fn a_declared_integer_renders_what_the_native_interval_does() {
let native = SqlDurationComparison::<PgDuration, sqlx::Postgres>::try_from_filter(
DurationFilter::new().with_lte(SignedDuration::from_secs(90)),
)
.unwrap();
let declared = SqlDurationComparison::<i64, sqlx::Postgres>::try_from_filter_with(
DurationFilter::new().with_lte(SignedDuration::from_secs(90)),
nanos,
)
.unwrap();
assert_eq!(declared.lte(), Some(&90_000_000_000));
assert_eq!(
render::<sqlx::Postgres, _>("len", &native),
render::<sqlx::Postgres, _>("len", &declared)
);
}
#[test]
fn the_two_carriers_refuse_a_sub_microsecond_operand_at_different_moments() {
let refused = SqlDurationComparison::<PgDuration, sqlx::Postgres>::try_from_filter(
DurationFilter::new().with_eq(SignedDuration::from_nanos(1_500)),
);
assert_eq!(refused.unwrap_err(), PgDurationError::Nanoseconds(500));
let whole = SqlDurationComparison::<PgDuration, sqlx::Postgres>::try_from_filter(
DurationFilter::new().with_eq(SignedDuration::from_nanos(2_000)),
)
.unwrap();
assert_eq!(
render::<sqlx::Postgres, _>("len", &whole),
("\"len\" = $1".to_owned(), 1)
);
assert!(
SqlDurationComparison::<Duration, sqlx::Postgres>::try_from_filter(
DurationFilter::new().with_eq(SignedDuration::from_nanos(1_500)),
)
.is_ok()
);
}
#[test]
fn the_threaded_lowering_renders_the_same_conjuncts() {
let crossed = SqlDurationComparison::<PgDuration, sqlx::Postgres>::try_from_filter(
DurationFilter::new()
.with_gte(SignedDuration::from_secs(4))
.with_lt(SignedDuration::from_secs(5)),
)
.unwrap();
assert_eq!(
threaded::<sqlx::Postgres, _>("len", &crossed),
"SELECT \"id\" FROM \"t\" WHERE \"len\" >= $1 AND \"len\" < $2"
);
}
}
#[cfg(feature = "mysql")]
mod mysql {
use core::time::Duration;
use jiff::SignedDuration;
use sqlx::mysql::types::{MySqlTime, MySqlTimeSign};
use super::SqlDurationComparison;
use crate::{
DurationFilter, MySqlDuration, MySqlDurationError,
comparison::{
TryFromFilter,
sql::tests::{render, threaded},
},
};
#[test]
fn every_operator_renders_its_own_conjunct() {
let filter = DurationFilter::new()
.with_eq(SignedDuration::from_secs(1))
.with_ne(SignedDuration::from_secs(2))
.with_gt(SignedDuration::from_secs(3))
.with_gte(SignedDuration::from_secs(4))
.with_lt(SignedDuration::from_secs(5))
.with_lte(SignedDuration::from_secs(6))
.with_in_([SignedDuration::from_secs(7), SignedDuration::from_secs(8)])
.with_not_in([SignedDuration::from_secs(9)]);
let crossed =
SqlDurationComparison::<MySqlDuration, sqlx::MySql>::try_from_filter(filter).unwrap();
assert_eq!(crossed.conjuncts(), 8);
assert_eq!(
render::<sqlx::MySql, _>("len", &crossed),
(
"`len` = ? AND NOT ( `len` = ? ) AND `len` > ? AND `len` >= ? AND `len` < ? AND \
`len` <= ? AND `len` IN (?, ?) AND NOT ( `len` IN (?) )"
.to_owned(),
9
)
);
}
#[test]
fn sqlx_rounds_a_sub_microsecond_operand_and_this_crate_refuses_it() {
let rounded = MySqlTime::try_from(Duration::from_nanos(1_500)).expect("sqlx accepts it");
let whole = MySqlTime::try_from(Duration::from_nanos(1_000)).expect("sqlx accepts it");
assert_eq!(
rounded, whole,
"sqlx's conversion divides by 1000, so the nanoseconds are gone with no error"
);
assert_eq!(
MySqlDuration::try_from(SignedDuration::from_nanos(1_500)).unwrap_err(),
MySqlDurationError::Nanoseconds(500)
);
assert_eq!(
MySqlDuration::try_from(SignedDuration::from_nanos(1_000))
.unwrap()
.get(),
whole
);
}
#[test]
fn the_crossing_carries_the_refusal() {
let refused = SqlDurationComparison::<MySqlDuration, sqlx::MySql>::try_from_filter(
DurationFilter::new().with_in_([
SignedDuration::from_secs(1),
SignedDuration::from_nanos(1_500),
]),
);
assert_eq!(
refused.unwrap_err(),
MySqlDurationError::Nanoseconds(500),
"the first refusal in a list is the answer"
);
}
#[test]
fn the_time_ceiling_is_a_refusal_and_not_a_wrap() {
let at_ceiling = SignedDuration::from_secs(838 * 3600 + 59 * 60 + 59);
let one = SignedDuration::from_secs(1);
for edge in [at_ceiling, -at_ceiling] {
assert!(MySqlDuration::try_from(edge).is_ok(), "{edge}");
}
for past in [at_ceiling + one, -at_ceiling - one] {
assert_eq!(
MySqlDuration::try_from(past).unwrap_err(),
MySqlDurationError::Ceiling,
"{past}"
);
}
assert_eq!(
DurationFilter::operand_nanos(at_ceiling + one),
Ok(3_020_400_000_000_000)
);
}
#[test]
fn a_negative_operand_reaches_the_time_column_with_its_sign() {
let negative = MySqlDuration::try_from(SignedDuration::from_secs(-90))
.expect("`TIME` is a signed type")
.get();
assert_eq!(negative.sign(), MySqlTimeSign::Negative);
assert_eq!(negative.hours(), 0);
assert_eq!(negative.minutes(), 1);
assert_eq!(negative.seconds(), 30);
let positive = MySqlDuration::try_from(SignedDuration::from_secs(90))
.expect("the magnitude is inside the ceiling")
.get();
assert_eq!(positive.sign(), MySqlTimeSign::Positive);
assert_ne!(negative, positive);
assert!(negative < positive);
let zero = MySqlDuration::try_from(SignedDuration::ZERO)
.expect("zero is a duration")
.get();
assert_eq!(zero.sign(), MySqlTimeSign::Positive);
assert_eq!(zero, MySqlTime::ZERO);
assert_eq!(
MySqlDuration::try_from(SignedDuration::from_nanos(-1_500)).unwrap_err(),
MySqlDurationError::Nanoseconds(500)
);
}
#[test]
fn the_threaded_lowering_renders_the_same_conjuncts() {
let crossed = SqlDurationComparison::<MySqlDuration, sqlx::MySql>::try_from_filter(
DurationFilter::new()
.with_gte(SignedDuration::from_secs(4))
.with_lt(SignedDuration::from_secs(5)),
)
.unwrap();
assert_eq!(
threaded::<sqlx::MySql, _>("len", &crossed),
"SELECT \"id\" FROM \"t\" WHERE `len` >= ? AND `len` < ?"
);
}
}