#![cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use super::Backend;
use crate::{
BindCeiling,
comparison::sql::{SqlConjunction, SqlLoweringError},
};
fn two_conjuncts<Db>() -> String
where
Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
{
let mut qb = sqlx::QueryBuilder::<Db>::default();
{
let mut accumulator: <Db as Backend>::Builder<'_> = SqlConjunction::with_lead(&mut qb, "");
accumulator.open().push("\"a\" = 1");
accumulator.open().push("\"b\" = 2");
}
qb.into_string()
}
#[test]
fn every_dialect_row_names_the_accumulator_over_itself() {
const RENDERED: &str = "\"a\" = 1 AND \"b\" = 2";
#[cfg(feature = "sqlite")]
assert_eq!(two_conjuncts::<sqlx::Sqlite>(), RENDERED);
#[cfg(feature = "postgres")]
assert_eq!(two_conjuncts::<sqlx::Postgres>(), RENDERED);
#[cfg(feature = "mysql")]
assert_eq!(two_conjuncts::<sqlx::MySql>(), RENDERED);
}
fn ceiling<Db>() -> usize
where
Db: BindCeiling + Backend<Error = SqlLoweringError>,
{
Db::MAX_BIND_PARAMETERS
}
#[test]
fn every_dialect_row_refuses_with_the_same_type_and_its_own_number() {
#[cfg(feature = "sqlite")]
assert_eq!(ceiling::<sqlx::Sqlite>(), 32766);
#[cfg(feature = "postgres")]
assert_eq!(ceiling::<sqlx::Postgres>(), 65535);
#[cfg(feature = "mysql")]
assert_eq!(ceiling::<sqlx::MySql>(), 65535);
}
#[cfg(feature = "sqlite")]
#[test]
fn a_sql_lowering_refuses_a_ceiling_an_edge_a_json_scope_a_wide_relation_a_deep_one_and_no_more() {
use crate::{
FieldName,
comparison::sql::{BindCeilingError, RelationDepthError, RelationWidthError},
};
fn reason(error: <sqlx::Sqlite as Backend>::Error) -> String {
match error {
SqlLoweringError::BindCeiling(error) => error.ceiling().to_string(),
SqlLoweringError::EdgeWithoutField(table) => (**table).to_owned(),
SqlLoweringError::JsonElementScope => "json".to_owned(),
SqlLoweringError::RelationWidth(width) => {
format!("{}/{}", &**width.table(), width.columns())
}
SqlLoweringError::RelationDepth(depth) => {
format!("{}@{}/{}", &**depth.table(), depth.depth(), depth.ceiling())
}
}
}
let lowered: Result<(), <sqlx::Sqlite as Backend>::Error> = Err(SqlLoweringError::BindCeiling(
BindCeilingError::new(16384, 2, <sqlx::Sqlite as BindCeiling>::MAX_BIND_PARAMETERS),
));
match lowered {
Ok(()) => panic!("the refusals here are the ones under test"),
Err(error) => assert_eq!(reason(error), "32766"),
}
let lowered: Result<(), <sqlx::Sqlite as Backend>::Error> =
Err(SqlLoweringError::EdgeWithoutField(FieldName::new("track")));
match lowered {
Ok(()) => panic!("the refusals here are the ones under test"),
Err(error) => assert_eq!(reason(error), "track"),
}
let lowered: Result<(), <sqlx::Sqlite as Backend>::Error> = Err(SqlLoweringError::RelationDepth(
RelationDepthError::new(FieldName::new("scene"), 8, 8),
));
match lowered {
Ok(()) => panic!("the refusals here are the ones under test"),
Err(error) => assert_eq!(reason(error), "scene@8/8"),
}
let lowered: Result<(), <sqlx::Sqlite as Backend>::Error> = Err(SqlLoweringError::RelationWidth(
RelationWidthError::new(FieldName::new("splice"), 2),
));
match lowered {
Ok(()) => panic!("the refusals here are the ones under test"),
Err(error) => assert_eq!(reason(error), "splice/2"),
}
}