From 6fb5219796b67006bf0d2e387d1ce5518d330228 Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Fri, 7 Jul 2023 12:33:19 -0500 Subject: [PATCH] fix: simplify testing with pg data --- src/hashed_text.rs | 16 +++------------- src/postgres.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/hashed_text.rs b/src/hashed_text.rs index fef8091..9257adc 100644 --- a/src/hashed_text.rs +++ b/src/hashed_text.rs @@ -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::(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::(ps[0]) == String::from("foo"))) }), ..Client::default() }; diff --git a/src/postgres.rs b/src/postgres.rs index 6350e62..0dc7d2f 100644 --- a/src/postgres.rs +++ b/src/postgres.rs @@ -82,6 +82,14 @@ impl Postgres for PostgresImpl pub mod test { use std::marker::PhantomData; + pub fn from_sql_owned(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;