diff --git a/Cargo.toml b/Cargo.toml index a39ad537..a8f34c93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,6 +61,10 @@ required-features = [ "postgres" ] name = "postgres-types" required-features = [ "postgres" ] +[[test]] +name = "postgres-types-chrono" +required-features = [ "postgres", "chrono" ] + [[test]] name = "mysql-types" required-features = [ "mysql" ] diff --git a/tests/postgres-types-chrono.rs b/tests/postgres-types-chrono.rs new file mode 100644 index 00000000..c6242152 --- /dev/null +++ b/tests/postgres-types-chrono.rs @@ -0,0 +1,81 @@ +use sqlx::types::chrono::{DateTime, NaiveDate, NaiveTime, Utc}; +use sqlx::{Connection, PgConnection, Row}; + +async fn connect() -> anyhow::Result { + Ok(PgConnection::open(dotenv::var("DATABASE_URL")?).await?) +} + +#[async_std::test] +async fn postgres_chrono_date() -> anyhow::Result<()> { + let mut conn = connect().await?; + + let value = NaiveDate::from_ymd(2019, 1, 2); + + let row = sqlx::query("SELECT DATE '2019-01-02' = $1, $1") + .bind(&value) + .fetch_one(&mut conn) + .await?; + + assert!(row.get::(0)); + assert_eq!(value, row.get(1)); + + Ok(()) +} + +#[async_std::test] +async fn mysql_chrono_date_time() -> anyhow::Result<()> { + let mut conn = connect().await?; + + let value = NaiveDate::from_ymd(2019, 1, 2).and_hms(5, 10, 20); + + let row = sqlx::query("SELECT '2019-01-02 05:10:20' = $1, $1") + .bind(&value) + .fetch_one(&mut conn) + .await?; + + assert!(row.get::(0)); + assert_eq!(value, row.get(1)); + + Ok(()) +} + +#[async_std::test] +async fn postgres_chrono_time() -> anyhow::Result<()> { + let mut conn = connect().await?; + + let value = NaiveTime::from_hms_micro(5, 10, 20, 115100); + + let row = sqlx::query("SELECT TIME '05:10:20.115100' = $1, TIME '05:10:20.115100'") + .bind(&value) + .fetch_one(&mut conn) + .await?; + + assert!(row.get::(0)); + assert_eq!(value, row.get(1)); + + Ok(()) +} + +#[async_std::test] +async fn postgres_chrono_timestamp_tz() -> anyhow::Result<()> { + let mut conn = connect().await?; + + let value = DateTime::::from_utc( + NaiveDate::from_ymd(2019, 1, 2).and_hms_micro(5, 10, 20, 115100), + Utc, + ); + + let row = sqlx::query( + "SELECT TIMESTAMPTZ '2019-01-02 05:10:20.115100' = $1, TIMESTAMPTZ '2019-01-02 05:10:20.115100'", + ) + .bind(&value) + .fetch_one(&mut conn) + .await?; + + assert!(row.get::(0)); + + let out: DateTime = row.get(1); + assert_eq!(value, out); + + Ok(()) +}