use super::SqlNullable;
use crate::{
Nullable, ScalarFilter,
comparison::{TryFromFilter, sql::SqlScalarComparison},
};
type Column = SqlNullable<SqlScalarComparison<i64, ()>, i64>;
#[test]
fn the_wrapper_crosses_its_inner_and_keeps_the_null_test() {
let filter = Nullable::new(ScalarFilter::<i64>::new().with_eq(1i64)).with_null(false);
let crossed = Column::try_from_filter(filter).unwrap();
assert_eq!(crossed.inner().eq(), Some(&1));
assert_eq!(crossed.null(), Some(false));
assert_eq!(crossed.inner().conjuncts(), 1);
}
#[test]
fn the_null_test_alone_is_not_an_empty_comparison() {
let crossed =
Column::try_from_filter(Nullable::new(ScalarFilter::<i64>::new()).with_null(true)).unwrap();
assert!(crossed.inner().is_empty());
assert_eq!(crossed.null(), Some(true));
}
#[test]
fn the_default_is_the_empty_inner_comparison_with_no_null_test() {
let empty = Column::default();
assert_eq!(empty.null(), None);
assert!(empty.inner().is_empty());
assert_eq!(empty, Column::new(SqlScalarComparison::new()));
}
#[cfg(feature = "sqlite")]
#[test]
fn the_column_option_lands_on_the_wrapper_and_not_on_the_operands() {
use core::marker::PhantomData;
use crate::{
At, ComplexComparison,
comparison::{
ToDatabaseComparison,
sql::{Carrier, SqlConjunction, SqlLoweringError, push_column},
},
};
struct KeyFilter;
struct KeyComparison<V>(Option<V>);
impl<V: Carrier<sqlx::Sqlite>, Db> ToDatabaseComparison<V, Db> for KeyFilter
where
Db: sqlx::Database + crate::Backend,
KeyComparison<V>: ComplexComparison<Db>,
{
type Comparison = KeyComparison<V>;
}
impl<V: Carrier<sqlx::Sqlite>> ComplexComparison<sqlx::Sqlite> for KeyComparison<V> {
fn conjuncts(&self) -> usize {
usize::from(self.0.is_some())
}
fn apply<'q>(
&'q self,
at: At<'_>,
mut builder: SqlConjunction<'q, sqlx::Sqlite>,
) -> Result<SqlConjunction<'q, sqlx::Sqlite>, SqlLoweringError> {
if let Some(value) = &self.0 {
let qb = builder.open();
push_column(qb, at);
qb.push(" = ");
value.push_bind(qb);
}
Ok(builder)
}
}
fn same<A>(_: PhantomData<A>, _: PhantomData<A>) {}
same(
PhantomData::<
<Nullable<KeyFilter> as ToDatabaseComparison<Option<i64>, sqlx::Sqlite>>::Comparison,
>,
PhantomData::<SqlNullable<KeyComparison<i64>, i64>>,
);
}
#[test]
fn the_two_function_entry_nests_the_inner_kinds_own_crossing() {
let filter = Nullable::new(ScalarFilter::<u8>::new().with_eq(3u8)).with_null(true);
let crossed = SqlNullable::<_, i64>::try_from_filter_with_inner(filter, |inner| {
SqlScalarComparison::<i64, ()>::try_from_filter_with(inner, |v: u8| {
Ok::<_, core::convert::Infallible>(i64::from(v) * 10)
})
})
.unwrap();
assert_eq!(crossed.inner().eq(), Some(&30));
assert_eq!(crossed.null(), Some(true));
}
#[test]
fn one_operand_converter_crosses_the_wrapper_the_way_every_flat_kind_takes_one() {
let filter = Nullable::new(ScalarFilter::<u8>::new().with_eq(3u8)).with_null(true);
let crossed =
SqlNullable::<SqlScalarComparison<i64, ()>, i64>::try_from_filter_with(filter, |v: u8| {
Ok::<_, core::convert::Infallible>(i64::from(v) * 10)
})
.unwrap();
assert_eq!(crossed.inner().eq(), Some(&30));
assert_eq!(
crossed.null(),
Some(true),
"the null test names no value, so no converter is asked about it"
);
}
#[cfg(feature = "sqlite")]
mod sqlite {
use core::{cell::RefCell, convert::Infallible};
use crate::{
At, ColumnName, ColumnShape, ComplexComparison, FieldName, Nullable, ScalarFilter,
comparison::{
CompositeComparison, TryFromFilter,
sql::{
SqlConjunction, SqlLoweringError, SqlNullable, SqlScalarComparison,
tests::{Probe, render, threaded},
},
},
};
type Column = SqlNullable<SqlScalarComparison<i64, sqlx::Sqlite>, i64>;
#[test]
fn a_nullable_columns_operands_are_not_themselves_nullable() {
use core::marker::PhantomData;
use crate::comparison::ToDatabaseComparison;
fn same<A>(_: PhantomData<A>, _: PhantomData<A>) {}
same(
PhantomData::<
<Nullable<ScalarFilter<i64>> as ToDatabaseComparison<
Option<i64>,
sqlx::Sqlite,
>>::Comparison,
>,
PhantomData::<Column>,
);
let crossed = Column::try_from_filter(
Nullable::new(ScalarFilter::<i64>::new().with_eq(1i64)).with_null(true),
)
.unwrap();
let operand: Option<&i64> = crossed.inner().eq();
assert_eq!(operand, Some(&1));
}
#[test]
fn the_null_test_closes_the_clause_and_binds_nothing() {
let crossed = Column::try_from_filter(
Nullable::new(ScalarFilter::<i64>::new().with_eq(1i64)).with_null(false),
)
.unwrap();
assert_eq!(
render::<sqlx::Sqlite, _>("n", &crossed),
("\"n\" = ? AND \"n\" IS NOT NULL".to_owned(), 1)
);
}
#[test]
fn the_null_test_negates_by_operator() {
let is_null =
Column::try_from_filter(Nullable::new(ScalarFilter::<i64>::new()).with_null(true)).unwrap();
let is_not_null =
Column::try_from_filter(Nullable::new(ScalarFilter::<i64>::new()).with_null(false)).unwrap();
assert_eq!(
render::<sqlx::Sqlite, _>("n", &is_null),
("\"n\" IS NULL".to_owned(), 0)
);
assert_eq!(
render::<sqlx::Sqlite, _>("n", &is_not_null),
("\"n\" IS NOT NULL".to_owned(), 0)
);
}
#[test]
fn the_wrapper_adds_only_the_null_test() {
let bare = SqlScalarComparison::<i64, sqlx::Sqlite>::try_from_filter(
ScalarFilter::<i64>::new().with_eq(1i64),
)
.unwrap();
let wrapped =
Column::try_from_filter(Nullable::new(ScalarFilter::<i64>::new().with_eq(1i64))).unwrap();
assert_eq!(
render::<sqlx::Sqlite, _>("n", &bare),
render::<sqlx::Sqlite, _>("n", &wrapped)
);
}
#[test]
fn the_wrapper_threads_its_inner_the_accumulator_it_was_handed() {
let crossed = Column::try_from_filter(
Nullable::new(ScalarFilter::<i64>::new().with_eq(1i64).with_gt(3i64)).with_null(false),
)
.unwrap();
assert_eq!(
threaded::<sqlx::Sqlite, _>("n", &crossed),
"SELECT \"id\" FROM \"t\" WHERE \"n\" = ? AND \"n\" > ? AND \"n\" IS NOT NULL"
);
}
#[test]
fn nesting_composes_with_no_layer_knowing_the_depth() {
let crossed = SqlNullable::<Column, i64>::try_from_filter(
Nullable::new(Nullable::new(ScalarFilter::<i64>::new().with_eq(1i64)).with_null(true))
.with_null(false),
)
.unwrap();
assert_eq!(
threaded::<sqlx::Sqlite, _>("n", &crossed),
"SELECT \"id\" FROM \"t\" WHERE \"n\" = ? AND \"n\" IS NULL AND \"n\" IS NOT NULL"
);
assert_eq!(ComplexComparison::<sqlx::Sqlite>::conjuncts(&crossed), 3);
}
#[test]
fn the_conjunct_count_is_the_whole_subtree() {
let both = Column::try_from_filter(
Nullable::new(ScalarFilter::<i64>::new().with_eq(1i64).with_gt(3i64)).with_null(false),
)
.unwrap();
let inner_only =
Column::try_from_filter(Nullable::new(ScalarFilter::<i64>::new().with_eq(1i64))).unwrap();
let null_only =
Column::try_from_filter(Nullable::new(ScalarFilter::<i64>::new()).with_null(true)).unwrap();
let neither = Column::try_from_filter(Nullable::new(ScalarFilter::<i64>::new())).unwrap();
assert_eq!(ComplexComparison::<sqlx::Sqlite>::conjuncts(&both), 3);
assert_eq!(ComplexComparison::<sqlx::Sqlite>::conjuncts(&inner_only), 1);
assert_eq!(ComplexComparison::<sqlx::Sqlite>::conjuncts(&null_only), 1);
assert_eq!(ComplexComparison::<sqlx::Sqlite>::conjuncts(&neither), 0);
assert!(ComplexComparison::<sqlx::Sqlite>::is_empty(&neither));
}
#[test]
fn the_wrapper_binds_its_inner_operands_and_adds_none() {
let bound = RefCell::new(Vec::new());
let crossed = SqlNullable::<_, i64>::try_from_filter_with_inner(
Nullable::new(ScalarFilter::<i64>::new().with_eq(1i64).with_gt(3i64)).with_null(true),
|inner| {
SqlScalarComparison::<Probe<'_>, sqlx::Sqlite>::try_from_filter_with(inner, |value| {
Ok::<_, Infallible>(Probe {
value,
bound: &bound,
})
})
},
)
.unwrap();
let sql = threaded::<sqlx::Sqlite, _>("n", &crossed);
assert_eq!(
sql,
"SELECT \"id\" FROM \"t\" WHERE \"n\" = ? AND \"n\" > ? AND \"n\" IS NULL"
);
assert_eq!(*bound.borrow(), [1, 3]);
}
struct Ratio {
num: SqlScalarComparison<i64, sqlx::Sqlite>,
den: SqlScalarComparison<i64, sqlx::Sqlite>,
}
impl ComplexComparison<sqlx::Sqlite> for Ratio {
fn conjuncts(&self) -> usize {
ComplexComparison::<sqlx::Sqlite>::conjuncts(&self.num)
+ ComplexComparison::<sqlx::Sqlite>::conjuncts(&self.den)
}
fn apply<'q>(
&'q self,
at: At<'_>,
builder: SqlConjunction<'q, sqlx::Sqlite>,
) -> Result<SqlConjunction<'q, sqlx::Sqlite>, SqlLoweringError> {
let num = at.frame(FieldName::new("num"));
let builder = self.num.apply(num.at(), builder)?;
let den = at.frame(FieldName::new("den"));
self.den.apply(den.at(), builder)
}
}
impl CompositeComparison<sqlx::Sqlite> for Ratio {
const SEGMENTS: &'static [ColumnShape] = &[
ColumnShape::Plain(ColumnName::new("num")),
ColumnShape::Plain(ColumnName::new("den")),
];
}
struct Aspect;
impl crate::ColumnKind for Aspect {
const SEGMENTS: &'static [ColumnShape] = <Ratio as CompositeComparison<sqlx::Sqlite>>::SEGMENTS;
}
struct Reduced;
impl crate::ColumnKind for Reduced {
const SEGMENTS: &'static [ColumnShape] = &[
ColumnShape::Plain(ColumnName::new("num")),
ColumnShape::Plain(ColumnName::new("den")),
ColumnShape::Nullable(ColumnName::new("norm")),
];
}
#[test]
fn the_wrapper_is_a_composite_when_its_inner_is_and_forwards_the_segments() {
fn segments<C: CompositeComparison<sqlx::Sqlite>>() -> &'static [ColumnShape] {
C::SEGMENTS
}
const PARTS: [ColumnShape; 2] = [
ColumnShape::Plain(ColumnName::new("num")),
ColumnShape::Plain(ColumnName::new("den")),
];
assert_eq!(segments::<Ratio>(), PARTS);
assert_eq!(segments::<SqlNullable<Ratio, Aspect>>(), PARTS);
}
fn ratio() -> Ratio {
Ratio {
num: SqlScalarComparison::try_from_filter(ScalarFilter::<i64>::new().with_eq(16i64)).unwrap(),
den: SqlScalarComparison::try_from_filter(ScalarFilter::<i64>::new().with_eq(9i64)).unwrap(),
}
}
#[test]
fn a_nullable_composite_tests_every_one_of_its_value_columns() {
let crossed = SqlNullable::<_, Aspect> {
inner: ratio(),
null: Some(false),
value: core::marker::PhantomData,
};
assert_eq!(
render::<sqlx::Sqlite, _>("aspect", &crossed),
(
"\"aspect_num\" = ? AND \"aspect_den\" = ? AND (\"aspect_num\" IS NOT NULL AND \
\"aspect_den\" IS NOT NULL)"
.to_owned(),
2
)
);
}
#[test]
fn a_derived_leaf_is_in_neither_direction_of_the_null_test() {
let absent = SqlNullable::<_, Reduced> {
inner: ratio(),
null: Some(true),
value: core::marker::PhantomData,
};
let (sql, _) = render::<sqlx::Sqlite, _>("aspect", &absent);
assert!(
sql.ends_with("(\"aspect_num\" IS NULL AND \"aspect_den\" IS NULL)"),
"the test is the two VALUE columns and closes there: {sql}"
);
assert!(
!sql.contains("aspect_norm"),
"the derived column takes no part in whether the field is there: {sql}"
);
}
#[test]
fn a_shape_with_no_value_column_is_never_absent_and_always_present() {
struct Indexed;
impl crate::ColumnKind for Indexed {
const SEGMENTS: &'static [ColumnShape] = &[ColumnShape::Nullable(ColumnName::new("norm"))];
}
let test = |null| {
render::<sqlx::Sqlite, _>(
"aspect",
&SqlNullable::<_, Indexed> {
inner: ratio(),
null: Some(null),
value: core::marker::PhantomData,
},
)
.0
};
assert!(test(true).ends_with("(FALSE)"), "{}", test(true));
assert!(test(false).ends_with("(TRUE)"), "{}", test(false));
}
#[test]
fn a_scalar_null_test_is_unchanged() {
let crossed =
Column::try_from_filter(Nullable::new(ScalarFilter::<i64>::new()).with_null(true)).unwrap();
assert_eq!(
render::<sqlx::Sqlite, _>("n", &crossed),
("\"n\" IS NULL".to_owned(), 0)
);
}
}