Skip to content

Commit 308a03e

Browse files
committed
update unit tests
1 parent 3857c69 commit 308a03e

File tree

3 files changed

+34
-140
lines changed

3 files changed

+34
-140
lines changed

tests/mysql/types.rs

+12-47
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use sqlx::types::Text;
1515
use sqlx::mysql::types::MySqlTime;
1616
use sqlx_mysql::types::MySqlTimeSign;
1717

18-
use sqlx_test::{new, test_type};
18+
use sqlx_test::{new, test_prepared_type, test_type};
1919

2020
test_type!(bool(MySql, "false" == false, "true" == true));
2121

@@ -303,6 +303,17 @@ mod json_tests {
303303
));
304304
}
305305

306+
test_type!(test_arc<Arc<i32>>(MySql, "1" == Arc::new(1i32)));
307+
test_type!(test_cow<Cow<'_, i32>>(MySql, "1" == Cow::<i32>::Owned(1i32)));
308+
test_type!(test_box<Box<i32>>(MySql, "1" == Box::new(1i32)));
309+
test_type!(test_rc<Rc<i32>>(MySql, "1" == Rc::new(1i32)));
310+
311+
test_type!(test_box_str<Box<str>>(MySql, "'John'" == Box::<str>::from("John")));
312+
test_type!(test_cow_str<Cow<'_, str>>(MySql, "'Phil'" == Cow::<'static, str>::from("Phil")));
313+
314+
test_prepared_type!(test_box_slice<Box<[u8]>>(MySql, "X'01020304'" == Box::<[u8]>::from([1,2,3,4])));
315+
test_prepared_type!(test_cow_slice<Cow<'_, [u8]>>(MySql, "X'01020304'" == Cow::<'static, [u8]>::from(&[1,2,3,4])));
316+
306317
#[sqlx_macros::test]
307318
async fn test_bits() -> anyhow::Result<()> {
308319
let mut conn = new::<MySql>().await?;
@@ -387,49 +398,3 @@ CREATE TEMPORARY TABLE user_login (
387398

388399
Ok(())
389400
}
390-
391-
#[sqlx_macros::test]
392-
async fn test_smartpointers() -> anyhow::Result<()> {
393-
let mut conn = new::<MySql>().await?;
394-
395-
let user_age: (Arc<i32>, Cow<'static, i32>, Box<i32>, i32) =
396-
sqlx::query_as("SELECT ?, ?, ?, ?")
397-
.bind(Arc::new(1i32))
398-
.bind(Cow::<'_, i32>::Borrowed(&2i32))
399-
.bind(Box::new(3i32))
400-
.bind(Rc::new(4i32))
401-
.fetch_one(&mut conn)
402-
.await?;
403-
404-
assert!(user_age.0.as_ref() == &1);
405-
assert!(user_age.1.as_ref() == &2);
406-
assert!(user_age.2.as_ref() == &3);
407-
assert!(user_age.3 == 4);
408-
Ok(())
409-
}
410-
411-
#[sqlx_macros::test]
412-
async fn test_str_slice() -> anyhow::Result<()> {
413-
let mut conn = new::<MySql>().await?;
414-
415-
let box_str: Box<str> = "John".into();
416-
let box_slice: Box<[u8]> = [1, 2, 3, 4].into();
417-
let cow_str: Cow<'static, str> = "Phil".into();
418-
let cow_slice: Cow<'static, [u8]> = Cow::Borrowed(&[1, 2, 3, 4]);
419-
420-
let row = sqlx::query("SELECT ?, ?, ?, ?")
421-
.bind(&box_str)
422-
.bind(&box_slice)
423-
.bind(&cow_str)
424-
.bind(&cow_slice)
425-
.fetch_one(&mut conn)
426-
.await?;
427-
428-
let data: (Box<str>, Box<[u8]>, Cow<'_, str>, Cow<'_, [u8]>) = FromRow::from_row(&row)?;
429-
430-
assert!(data.0 == box_str);
431-
assert!(data.1 == box_slice);
432-
assert!(data.2 == cow_str);
433-
assert!(data.3 == cow_slice);
434-
Ok(())
435-
}

tests/postgres/types.rs

+11-47
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::sync::Arc;
88

99
use sqlx::postgres::types::{Oid, PgCiText, PgInterval, PgMoney, PgRange};
1010
use sqlx::postgres::Postgres;
11-
use sqlx::FromRow;
1211
use sqlx_test::{new, test_decode_type, test_prepared_type, test_type};
1312

1413
use sqlx_core::executor::Executor;
@@ -657,6 +656,17 @@ test_type!(ltree_vec<Vec<sqlx::postgres::types::PgLTree>>(Postgres,
657656
]
658657
));
659658

659+
test_type!(test_arc<Arc<i32>>(Postgres, "1::INT4" == Arc::new(1i32)));
660+
test_type!(test_cow<Cow<'_, i32>>(Postgres, "1::INT4" == Cow::<i32>::Owned(1i32)));
661+
test_type!(test_box<Box<i32>>(Postgres, "1::INT4" == Box::new(1i32)));
662+
test_type!(test_rc<Rc<i32>>(Postgres, "1::INT4" == Rc::new(1i32)));
663+
664+
test_type!(test_box_str<Box<str>>(Postgres, "'John'::TEXT" == Box::<str>::from("John")));
665+
test_type!(test_cow_str<Cow<'_, str>>(Postgres, "'Phil'::TEXT" == Cow::<'static, str>::from("Phil")));
666+
667+
test_prepared_type!(test_box_slice<Box<[u8]>>(Postgres, "'\\x01020304'::BYTEA" == Box::<[u8]>::from([1,2,3,4])));
668+
test_prepared_type!(test_cow_slice<Cow<'_, [u8]>>(Postgres, "'\\x01020304'::BYTEA" == Cow::<'static, [u8]>::from(&[1,2,3,4])));
669+
660670
#[sqlx_macros::test]
661671
async fn test_text_adapter() -> anyhow::Result<()> {
662672
#[derive(sqlx::FromRow, Debug, PartialEq, Eq)]
@@ -699,49 +709,3 @@ CREATE TEMPORARY TABLE user_login (
699709

700710
Ok(())
701711
}
702-
703-
#[sqlx_macros::test]
704-
async fn test_smartpointers() -> anyhow::Result<()> {
705-
let mut conn = new::<Postgres>().await?;
706-
707-
let user_age: (Arc<i32>, Cow<'static, i32>, Box<i32>, i32) =
708-
sqlx::query_as("SELECT $1, $2, $3, $4")
709-
.bind(Arc::new(1i32))
710-
.bind(Cow::<'_, i32>::Borrowed(&2i32))
711-
.bind(Box::new(3i32))
712-
.bind(Rc::new(4i32))
713-
.fetch_one(&mut conn)
714-
.await?;
715-
716-
assert!(user_age.0.as_ref() == &1);
717-
assert!(user_age.1.as_ref() == &2);
718-
assert!(user_age.2.as_ref() == &3);
719-
assert!(user_age.3 == 4);
720-
Ok(())
721-
}
722-
723-
#[sqlx_macros::test]
724-
async fn test_str_slice() -> anyhow::Result<()> {
725-
let mut conn = new::<Postgres>().await?;
726-
727-
let box_str: Box<str> = "John".into();
728-
let box_slice: Box<[u8]> = [1, 2, 3, 4].into();
729-
let cow_str: Cow<'static, str> = "Phil".into();
730-
let cow_slice: Cow<'static, [u8]> = Cow::Borrowed(&[1, 2, 3, 4]);
731-
732-
let row = sqlx::query("SELECT $1, $2, $3, $4")
733-
.bind(&box_str)
734-
.bind(&box_slice)
735-
.bind(&cow_str)
736-
.bind(&cow_slice)
737-
.fetch_one(&mut conn)
738-
.await?;
739-
740-
let data: (Box<str>, Box<[u8]>, Cow<'_, str>, Cow<'_, [u8]>) = FromRow::from_row(&row)?;
741-
742-
assert!(data.0 == box_str);
743-
assert!(data.1 == box_slice);
744-
assert!(data.2 == cow_str);
745-
assert!(data.3 == cow_slice);
746-
Ok(())
747-
}

tests/sqlite/types.rs

+11-46
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ test_type!(uuid_simple<sqlx::types::uuid::fmt::Simple>(Sqlite,
212212
== sqlx::types::Uuid::parse_str("00000000000000000000000000000000").unwrap().simple()
213213
));
214214

215+
test_type!(test_arc<Arc<i32>>(Sqlite, "1" == Arc::new(1i32)));
216+
test_type!(test_cow<Cow<'_, i32>>(Sqlite, "1" == Cow::<i32>::Owned(1i32)));
217+
test_type!(test_box<Box<i32>>(Sqlite, "1" == Box::new(1i32)));
218+
test_type!(test_rc<Rc<i32>>(Sqlite, "1" == Rc::new(1i32)));
219+
220+
test_type!(test_box_str<Box<str>>(Sqlite, "'John'" == Box::<str>::from("John")));
221+
test_type!(test_cow_str<Cow<'_, str>>(Sqlite, "'Phil'" == Cow::<'static, str>::from("Phil")));
222+
223+
test_type!(test_box_slice<Box<[u8]>>(Sqlite, "X'01020304'" == Box::<[u8]>::from([1,2,3,4])));
224+
test_type!(test_cow_slice<Cow<'_, [u8]>>(Sqlite, "X'01020304'" == Cow::<'static, [u8]>::from(&[1,2,3,4])));
225+
215226
#[sqlx_macros::test]
216227
async fn test_text_adapter() -> anyhow::Result<()> {
217228
#[derive(sqlx::FromRow, Debug, PartialEq, Eq)]
@@ -254,49 +265,3 @@ CREATE TEMPORARY TABLE user_login (
254265

255266
Ok(())
256267
}
257-
258-
#[sqlx_macros::test]
259-
async fn test_smartpointers() -> anyhow::Result<()> {
260-
let mut conn = new::<Sqlite>().await?;
261-
262-
let user_age: (Arc<i32>, Cow<'static, i32>, Box<i32>, i32) =
263-
sqlx::query_as("SELECT $1, $2, $3, $4")
264-
.bind(Arc::new(1i32))
265-
.bind(Cow::<'_, i32>::Borrowed(&2i32))
266-
.bind(Box::new(3i32))
267-
.bind(Rc::new(4i32))
268-
.fetch_one(&mut conn)
269-
.await?;
270-
271-
assert!(user_age.0.as_ref() == &1);
272-
assert!(user_age.1.as_ref() == &2);
273-
assert!(user_age.2.as_ref() == &3);
274-
assert!(user_age.3 == 4);
275-
Ok(())
276-
}
277-
278-
#[sqlx_macros::test]
279-
async fn test_str_slice() -> anyhow::Result<()> {
280-
let mut conn = new::<Sqlite>().await?;
281-
282-
let box_str: Box<str> = "John".into();
283-
let box_slice: Box<[u8]> = [1, 2, 3, 4].into();
284-
let cow_str: Cow<'static, str> = "Phil".into();
285-
let cow_slice: Cow<'static, [u8]> = Cow::Borrowed(&[1, 2, 3, 4]);
286-
287-
let row = sqlx::query("SELECT $1, $2, $3, $4")
288-
.bind(&box_str)
289-
.bind(&box_slice)
290-
.bind(&cow_str)
291-
.bind(&cow_slice)
292-
.fetch_one(&mut conn)
293-
.await?;
294-
295-
let data: (Box<str>, Box<[u8]>, Cow<'_, str>, Cow<'_, [u8]>) = FromRow::from_row(&row)?;
296-
297-
assert!(data.0 == box_str);
298-
assert!(data.1 == box_slice);
299-
assert!(data.2 == cow_str);
300-
assert!(data.3 == cow_slice);
301-
Ok(())
302-
}

0 commit comments

Comments
 (0)