rust-postgres/postgres-derive-test/src/lib.rs

58 lines
1.5 KiB
Rust
Raw Normal View History

2019-10-10 02:23:12 +00:00
#![cfg(test)]
use postgres::Client;
use postgres_types::{FromSqlOwned, ToSql};
use std::fmt;
mod composites;
mod domains;
mod enums;
2021-12-26 19:43:14 +00:00
mod transparent;
2019-10-10 02:23:12 +00:00
pub fn test_type<T, S>(conn: &mut Client, sql_type: &str, checks: &[(T, S)])
where
T: PartialEq + FromSqlOwned + ToSql + Sync,
S: fmt::Display,
{
2023-02-25 15:10:04 +00:00
for (val, repr) in checks.iter() {
2019-10-10 02:23:12 +00:00
let stmt = conn
2022-11-21 23:25:39 +00:00
.prepare(&format!("SELECT {}::{}", *repr, sql_type))
2019-10-10 02:23:12 +00:00
.unwrap();
let result = conn.query_one(&stmt, &[]).unwrap().get(0);
assert_eq!(val, &result);
2022-11-21 23:25:39 +00:00
let stmt = conn.prepare(&format!("SELECT $1::{}", sql_type)).unwrap();
2019-10-10 02:23:12 +00:00
let result = conn.query_one(&stmt, &[val]).unwrap().get(0);
assert_eq!(val, &result);
}
}
2019-10-10 22:21:45 +00:00
2022-08-09 00:22:06 +00:00
pub fn test_type_asymmetric<T, F, S, C>(
conn: &mut Client,
sql_type: &str,
checks: &[(T, S)],
cmp: C,
) where
T: ToSql + Sync,
F: FromSqlOwned,
S: fmt::Display,
C: Fn(&T, &F) -> bool,
{
2023-02-25 15:10:04 +00:00
for (val, repr) in checks.iter() {
2022-08-09 00:22:06 +00:00
let stmt = conn
2022-11-21 23:25:39 +00:00
.prepare(&format!("SELECT {}::{}", *repr, sql_type))
2022-08-09 00:22:06 +00:00
.unwrap();
let result: F = conn.query_one(&stmt, &[]).unwrap().get(0);
assert!(cmp(val, &result));
2022-11-21 23:25:39 +00:00
let stmt = conn.prepare(&format!("SELECT $1::{}", sql_type)).unwrap();
2022-08-09 00:22:06 +00:00
let result: F = conn.query_one(&stmt, &[val]).unwrap().get(0);
assert!(cmp(val, &result));
}
}
2019-10-10 22:21:45 +00:00
#[test]
fn compile_fail() {
trybuild::TestCases::new().compile_fail("src/compile-fail/*.rs");
}