#[path = "support/containers.rs"]
mod containers;
#[path = "support/exit_slot.rs"]
mod exit_slot;
#[path = "support/pg18.rs"]
mod pg18;
#[path = "support/postgres.rs"]
mod postgres;
use core::{cmp::Ordering, hash::BuildHasher};
use std::{hash::RandomState, sync::Once};
use diesel::{PgConnection, RunQueryDsl, sql_query};
use pg18::{PG18, PG18_TAG, connect, pg_nullable_text, pg_order, pg_text, quoted};
use sql_values::postgres::{CastError, JsonbError, NumericError, PgJsonb, PgType};
const C_DATABASE: &str = "jsonb_c";
fn connect_c() -> PgConnection {
static CREATED: Once = Once::new();
CREATED.call_once(|| {
sql_query(format!(
"CREATE DATABASE {C_DATABASE} TEMPLATE template0 LOCALE 'C'"
))
.execute(&mut connect())
.expect("create the C database");
});
postgres::connect_to(&PG18, PG18_TAG, C_DATABASE)
}
fn pg_jsonb(conn: &mut PgConnection, text: &str) -> Result<String, JsonbError> {
pg_text(conn, &format!("{}::jsonb", quoted(text))).map_err(|error| match error {
CastError::Jsonb(error) => error,
CastError::NumericFormatOverflow => JsonbError::Numeric(NumericError::FormatOverflow),
CastError::InvalidInputSyntax(PgType::Numeric) => {
JsonbError::Numeric(NumericError::InvalidInputSyntax)
}
other => panic!("{text:?}::jsonb raised {other}"),
})
}
const TEXTS: &[&str] = &[
r#"{"bb":1,"a":2,"c":3,"a":4}"#,
r#"{"aaa":1,"b":2,"aa":3,"ab":4,"a":5,"B":6}"#,
r#"{"a":{"x":1,"x":2,"w":[]},"a":[1,{"b":null}],"c":{}}"#,
r#"{"\u0061":1,"a":2}"#,
r#"{"a":[{"b":null},{"a":1,"a":2}],"a":{"z":{"y":{"x":[[[]]]}}}}"#,
r#"{"é":1,"z":2,"😀":3,"e":4,"ez":5}"#,
" [ 1 , {\"b\" : null } ] ",
"\t\r\n{\n\"a\"\t:\r[\n]\n}\n",
"1e2",
"1E-2",
"-0.0",
"0.000",
"-0",
"0e5",
"1.0",
"1.50",
"-1E+3",
"1e-20",
"123456789012345678901234567890.123456789",
"[1, 1.0, 1.00, 100e-2]",
r#"{"a":1.0}"#,
"1e-16383",
"9e131071",
"null",
"true",
"false",
"[]",
"{}",
"[[[[[]]]]]",
r#""""#,
r#""\u0001\b\t\u001f\u007f\u00e9 \ud83d\ude00 \/""#,
r#""\"\\\/\b\f\n\r\t""#,
r#""\uD834\uDD1E \u00FF \u0020""#,
"\"日本 é \u{7f}\"",
r#""\u001F""#,
r#"{"a":1,}"#,
"01",
"-01",
"tru",
"truex",
"nul",
"True",
"+1",
".5",
"1.",
"1.e5",
"-",
"-a",
"1e",
"1e+",
"0x10",
"NaN",
"Infinity",
"é",
"\u{c}1",
"'a'",
"\"abc",
"\"\\",
r#""\u0000""#,
r#""\ud83d""#,
r#""\ud83d x""#,
r#""\ud83d\n""#,
r#""\ud83d\u0041""#,
r#""\ude00""#,
r#""\ud83d\ud83d""#,
r#""\u12""#,
r#""\u12x4""#,
r#""\q""#,
"\"a\nb\"",
"\"a\u{1}b\"",
r#"{"a":"\ud83d"}"#,
"",
" ",
"[",
"{",
r#"{"a""#,
r#"{"a":"#,
"[1",
"[1,",
"[1 2]",
"[1,,2]",
"[,]",
"[1,]",
"{1:2}",
"{,}",
r#"{"a" 1}"#,
r#"{"a":1 "b":2}"#,
r#"{"a":1]"#,
"[1}",
"[1]]",
"1 2",
"{} {}",
":",
"1e1000000",
"1e-2000000000",
"1e-16384",
"[1e1000000,",
"[1e1000000 x]",
"1e1000000 01",
];
#[test]
fn pg18_reads_and_prints_every_text() {
let mut conn = connect();
let mismatches: Vec<String> = TEXTS
.iter()
.filter_map(|&text| {
let got = PgJsonb::parse(text).map(|jsonb| jsonb.to_string());
let expected = pg_jsonb(&mut conn, text);
(got != expected).then(|| format!("{text:?}: crate {got:?}, PostgreSQL {expected:?}"))
})
.collect();
assert!(mismatches.is_empty(), "{}", mismatches.join("\n"));
}
#[test]
fn pg18_extracts_the_text_of_every_value() {
let mut conn = connect();
let mismatches: Vec<String> = TEXTS
.iter()
.filter_map(|&text| {
let value = PgJsonb::parse(text).ok()?;
let got = value.to_text().map(String::from);
let expected =
pg_nullable_text(&mut conn, &format!("{}::jsonb #>> '{{}}'", quoted(text)))
.unwrap_or_else(|error| panic!("{text:?} #>> '{{}}' raised {error}"));
(got != expected).then(|| format!("{text:?}: crate {got:?}, PostgreSQL {expected:?}"))
})
.collect();
assert!(mismatches.is_empty(), "{}", mismatches.join("\n"));
}
const COMPARED: &[&str] = &[
"[]",
"null",
r#""""#,
r#""B""#,
r#""a""#,
r#""ab""#,
r#""b""#,
r#""é""#,
"-1",
"1",
"1.0",
"1.5",
"1e100",
"false",
"true",
"[null]",
r#"["a"]"#,
"[1]",
"[1.00]",
"[true]",
"[[]]",
"[{}]",
"[1, 2]",
"[2, 1]",
"[1, [2]]",
"[[1], 2]",
"[[], 1]",
"{}",
r#"{"a": 1}"#,
r#"{"a": 1.0}"#,
r#"{"a": 2}"#,
r#"{"a": null}"#,
r#"{"a": []}"#,
r#"{"a": {}}"#,
r#"{"aa": 1}"#,
r#"{"b": 1}"#,
r#"{"B": 1}"#,
r#"{"b": {}}"#,
r#"{"a": 1, "b": 2}"#,
r#"{"b": 1, "aa": 2}"#,
r#"{"b": 2, "a": 1}"#,
r#"{"a": [1, {"b": 2}], "c": 3}"#,
r#"{"a": [1, {"b": 3}], "c": 3}"#,
];
#[test]
fn pg18_compares_every_jsonb_pair() {
let mut conn = connect_c();
let hasher = RandomState::new();
let values: Vec<(String, PgJsonb)> = COMPARED
.iter()
.map(|&text| {
let value = PgJsonb::parse(text).unwrap_or_else(|error| panic!("{text:?}: {error}"));
(format!("{}::jsonb", quoted(text)), value)
})
.collect();
let mut mismatches = Vec::new();
for (left_sql, left) in &values {
for (right_sql, right) in &values {
let got = left.cmp(right);
let expected = pg_order(&mut conn, left_sql, right_sql);
if Some(got) != expected {
mismatches.push(format!(
"{left_sql} vs {right_sql}: crate {got:?}, PostgreSQL {expected:?}"
));
}
if got == Ordering::Equal && hasher.hash_one(left) != hasher.hash_one(right) {
mismatches.push(format!(
"{left_sql} and {right_sql} are equal but hash apart"
));
}
}
}
assert!(mismatches.is_empty(), "{}", mismatches.join("\n"));
}