fix: simplify testing with pg data
This commit is contained in:
parent
2f0716b6cf
commit
6fb5219796
@ -38,26 +38,16 @@ mod test {
|
||||
use postgres::types::{FromSql, ToSql, Type};
|
||||
|
||||
use super::{HashedText, HashedTextExt, HashedTextExtImpl};
|
||||
use crate::postgres::test::{Client, Row};
|
||||
use crate::postgres::test::{from_sql_owned, Client, Row};
|
||||
use crate::postgres::{Postgres, PostgresImpl};
|
||||
|
||||
#[test]
|
||||
fn hashed_text_matches_fn_call() {
|
||||
let client = || Client { query_one: Box::new(|_, q, ps| {
|
||||
assert_eq!(q.unwrap_str(), "select public.hashed_text_matches($1, public.hashed_text_of_string($2))");
|
||||
assert_eq!(from_sql_owned::<String>(ps[1]), String::from("XXX"));
|
||||
|
||||
let mut p0 = BytesMut::with_capacity(32);
|
||||
let mut p1 = BytesMut::with_capacity(32);
|
||||
|
||||
ps[0].to_sql(&Type::TEXT, &mut p0).unwrap();
|
||||
ps[1].to_sql(&Type::TEXT, &mut p1).unwrap();
|
||||
|
||||
let p0 = <&str as FromSql>::from_sql(&Type::TEXT, &p0).unwrap();
|
||||
let p1 = <&str as FromSql>::from_sql(&Type::TEXT, &p1).unwrap();
|
||||
|
||||
assert_eq!(p1, "XXX");
|
||||
|
||||
Ok(Row::new(vec![("", Type::BOOL)]).value(Type::BOOL, p0 == "foo"))
|
||||
Ok(Row::new(vec![("", Type::BOOL)]).value(Type::BOOL, from_sql_owned::<String>(ps[0]) == String::from("foo")))
|
||||
}),
|
||||
..Client::default() };
|
||||
|
||||
|
@ -82,6 +82,14 @@ impl<C> Postgres for PostgresImpl<C>
|
||||
pub mod test {
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub fn from_sql_owned<T>(to: &dyn ToSql) -> T
|
||||
where T: 'static + for<'a> FromSql<'a> + postgres::types::Typed
|
||||
{
|
||||
let mut buf = BytesMut::with_capacity(128);
|
||||
to.to_sql(&T::TYPE, &mut buf).unwrap();
|
||||
T::from_sql(&T::TYPE, &buf).unwrap()
|
||||
}
|
||||
|
||||
pub mod client_function {
|
||||
use postgres::StatementOrString;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user