#[path = "support/containers.rs"]
mod containers;
#[path = "support/exit_slot.rs"]
mod exit_slot;
#[path = "support/pg18.rs"]
#[expect(
dead_code,
reason = "This test selects the PostgreSQL 18 image directly."
)]
mod pg18;
#[path = "support/postgres.rs"]
mod postgres;
use core::hash::BuildHasher;
use std::hash::RandomState;
use diesel::{
RunQueryDsl, define_sql_function,
sql_types::{Bool, Text},
};
define_sql_function!(fn set_config(setting: Text, value: Text, is_local: Bool) -> Text);
use sql_values::postgres::{
CastValue, InputContext, IntegerType, PgArray, PgBpchar, PgTime, PgTimeTz, PgTimestampTz,
PgValue, parse_interval,
};
#[test]
fn pg18_value_constants() {
let mut connection = postgres::connect(&pg18::PG18, "18");
for (setting, value) in [
("TimeZone", "UTC"),
("DateStyle", "ISO"),
("IntervalStyle", "postgres"),
] {
diesel::select(set_config(setting, value, false))
.get_result::<String>(&mut connection)
.unwrap();
}
let context = InputContext::new();
let groups = [
("bpchar", vec!["", " ", "a", "a ", "a\t", " a", "b", "é"]),
(
"timestamptz",
vec![
"-infinity",
"2000-01-01 00:00:00+00",
"2000-01-01 02:00:00+02",
"2000-01-01 00:00:00.000001+00",
"0001-01-01 00:00:00+00 BC",
"infinity",
],
),
(
"time",
vec!["00:00", "12:00:00.123400", "23:59:59.999999", "24:00"],
),
(
"timetz",
vec![
"00:00+15:59:59",
"12:00+01",
"11:00+00",
"12:00:00.1+01",
"24:00-15:59:59",
],
),
(
"interval",
vec![
"-infinity",
"-1 mon",
"0",
"1 mon",
"30 days",
"720 hours",
"1 mon 00:00:00.000001",
"-1 days +02:00:00",
"infinity",
],
),
];
let hasher = RandomState::new();
for (type_name, literals) in groups {
let values: Vec<_> = literals
.into_iter()
.map(|literal| {
let cast = match type_name {
"bpchar" => CastValue::Bpchar(PgBpchar::new(literal.to_owned())),
"timestamptz" => {
CastValue::TimestampTz(PgTimestampTz::parse(literal, context).unwrap())
}
"time" => CastValue::Time(PgTime::parse(literal, context).unwrap()),
"timetz" => CastValue::TimeTz(PgTimeTz::parse(literal, context).unwrap()),
"interval" => CastValue::Interval(parse_interval(literal).unwrap()),
_ => unreachable!(),
};
let value = PgValue::from(cast.clone());
assert_eq!(
value.clone().into_cast_value(IntegerType::Integer),
Ok(cast)
);
let expression = format!("{}::{type_name}", pg18::quoted(literal));
let expression = if type_name == "bpchar" {
format!("{expression} COLLATE \"C\"")
} else {
expression
};
assert_eq!(
value.to_string(),
pg18::pg_text(&mut connection, &expression).unwrap(),
"{expression}"
);
let stable = matches!(type_name, "bpchar" | "time" | "timetz");
assert_eq!(value.output().is_some(), stable);
if let Some(output) = value.output() {
assert_eq!(output.to_string(), value.to_string());
}
assert_eq!(
PgValue::Array(PgArray::new(vec![Some(value.clone())]))
.output()
.is_some(),
stable
);
(expression, value)
})
.collect();
for (left_sql, left) in &values {
for (right_sql, right) in &values {
assert_eq!(
Some(left.cmp(right)),
pg18::pg_order(&mut connection, left_sql, right_sql),
"{left_sql} vs {right_sql}"
);
assert_eq!(left == right, left.cmp(right).is_eq());
if left == right {
assert_eq!(hasher.hash_one(left), hasher.hash_one(right));
}
}
}
}
}
#[test]
fn pg18_setting_dependent_output() {
let mut connection = postgres::connect(&pg18::PG18, "18");
for (setting, first, second, expression, cast) in [
(
"TimeZone",
"UTC",
"Europe/Rome",
"'2026-01-01 00:00:00+00'::timestamptz",
CastValue::TimestampTz(
PgTimestampTz::parse("2026-01-01 00:00:00+00", InputContext::new()).unwrap(),
),
),
(
"IntervalStyle",
"postgres",
"iso_8601",
"'1 mon 2 days 03:04:05'::interval",
CastValue::Interval(parse_interval("1 mon 2 days 03:04:05").unwrap()),
),
] {
let value = PgValue::from(cast);
diesel::select(set_config(setting, first, false))
.get_result::<String>(&mut connection)
.unwrap();
let default_output = pg18::pg_text(&mut connection, expression).unwrap();
assert_eq!(value.to_string(), default_output);
diesel::select(set_config(setting, second, false))
.get_result::<String>(&mut connection)
.unwrap();
assert_ne!(
default_output,
pg18::pg_text(&mut connection, expression).unwrap()
);
assert!(value.output().is_none());
let nested = PgValue::Array(PgArray::new(vec![Some(PgValue::Array(PgArray::new(
vec![Some(value)],
)))]));
assert!(nested.output().is_none());
}
}