Skip to content

Commit

Permalink
Fixing and testing into_json of various types
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Feb 17, 2022
1 parent 858953b commit 2157712
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/query/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ impl FromQueryResult for JsonValue {
}
};
}
macro_rules! compatible_mysql_type {
( $type: ty ) => {
if <$type as Type<MySql>>::compatible(col_type) {
map.insert(
col.to_owned(),
json!(res.try_get::<Option<$type>>(pre, &col)?),
);
continue;
}
};
}
match_mysql_type!(bool);
match_mysql_type!(i8);
match_mysql_type!(i16);
Expand All @@ -40,6 +51,20 @@ impl FromQueryResult for JsonValue {
match_mysql_type!(f32);
match_mysql_type!(f64);
match_mysql_type!(String);
#[cfg(feature = "with-chrono")]
match_mysql_type!(chrono::NaiveDate);
#[cfg(feature = "with-chrono")]
match_mysql_type!(chrono::NaiveTime);
#[cfg(feature = "with-chrono")]
match_mysql_type!(chrono::NaiveDateTime);
#[cfg(feature = "with-chrono")]
match_mysql_type!(chrono::DateTime<chrono::Utc>);
#[cfg(feature = "with-uuid")]
match_mysql_type!(uuid::Uuid);
#[cfg(feature = "with-rust_decimal")]
match_mysql_type!(rust_decimal::Decimal);
#[cfg(feature = "with-json")]
compatible_mysql_type!(serde_json::Value);
}
Ok(JsonValue::Object(map))
}
Expand All @@ -66,6 +91,17 @@ impl FromQueryResult for JsonValue {
}
};
}
macro_rules! compatible_postgres_type {
( $type: ty ) => {
if <$type as Type<Postgres>>::compatible(col_type) {
map.insert(
col.to_owned(),
json!(res.try_get::<Option<$type>>(pre, &col)?),
);
continue;
}
};
}
match_postgres_type!(bool);
match_postgres_type!(i8);
match_postgres_type!(i16);
Expand All @@ -77,7 +113,21 @@ impl FromQueryResult for JsonValue {
// match_postgres_type!(u64); // unsupported by SQLx Postgres
match_postgres_type!(f32);
match_postgres_type!(f64);
match_postgres_type!(String);
#[cfg(feature = "with-chrono")]
match_postgres_type!(chrono::NaiveDate);
#[cfg(feature = "with-chrono")]
match_postgres_type!(chrono::NaiveTime);
#[cfg(feature = "with-chrono")]
match_postgres_type!(chrono::NaiveDateTime);
#[cfg(feature = "with-chrono")]
match_postgres_type!(chrono::DateTime<chrono::FixedOffset>);
#[cfg(feature = "with-uuid")]
match_postgres_type!(uuid::Uuid);
#[cfg(feature = "with-rust_decimal")]
match_postgres_type!(rust_decimal::Decimal);
#[cfg(feature = "with-json")]
compatible_postgres_type!(serde_json::Value);
compatible_postgres_type!(String);
}
Ok(JsonValue::Object(map))
}
Expand Down Expand Up @@ -116,6 +166,14 @@ impl FromQueryResult for JsonValue {
match_sqlite_type!(f32);
match_sqlite_type!(f64);
match_sqlite_type!(String);
#[cfg(feature = "with-chrono")]
match_sqlite_type!(chrono::NaiveDate);
#[cfg(feature = "with-chrono")]
match_sqlite_type!(chrono::NaiveTime);
#[cfg(feature = "with-chrono")]
match_sqlite_type!(chrono::NaiveDateTime);
#[cfg(feature = "with-uuid")]
match_sqlite_type!(uuid::Uuid);
}
Ok(JsonValue::Object(map))
}
Expand Down
63 changes: 63 additions & 0 deletions tests/timestamp_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod common;
pub use common::{features::*, setup::*, TestContext};
use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel};
use serde_json::json;

#[sea_orm_macros::test]
#[cfg(any(
Expand Down Expand Up @@ -34,6 +35,37 @@ pub async fn create_applog(db: &DatabaseConnection) -> Result<(), DbErr> {
assert_eq!(log.id, res.last_insert_id);
assert_eq!(Applog::find().one(db).await?, Some(log.clone()));

#[cfg(feature = "sqlx-sqlite")]
assert_eq!(
Applog::find().into_json().one(db).await?,
Some(json!({
"id": 1,
"action": "Testing",
"json": r#""HI""#,
"created_at": "2021-09-17 09:50:20",
}))
);
#[cfg(feature = "sqlx-mysql")]
assert_eq!(
Applog::find().into_json().one(db).await?,
Some(json!({
"id": 1,
"action": "Testing",
"json": "HI",
"created_at": "2021-09-17T09:50:20Z",
}))
);
#[cfg(feature = "sqlx-postgres")]
assert_eq!(
Applog::find().into_json().one(db).await?,
Some(json!({
"id": 1,
"action": "Testing",
"json": "HI",
"created_at": "2021-09-17T09:50:20+00:00",
}))
);

Ok(())
}

Expand All @@ -52,5 +84,36 @@ pub async fn create_satellites_log(db: &DatabaseConnection) -> Result<(), DbErr>
assert_eq!(archive.id, res.last_insert_id);
assert_eq!(Satellite::find().one(db).await?, Some(archive.clone()));

#[cfg(feature = "sqlx-sqlite")]
assert_eq!(
Satellite::find().into_json().one(db).await?,
Some(json!({
"id": 1,
"satellite_name": "Sea-00001-2022",
"launch_date": "2022-01-07 12:11:23",
"deployment_date": "2022-01-07 12:11:23",
}))
);
#[cfg(feature = "sqlx-mysql")]
assert_eq!(
Satellite::find().into_json().one(db).await?,
Some(json!({
"id": 1,
"satellite_name": "Sea-00001-2022",
"launch_date": "2022-01-07T12:11:23Z",
"deployment_date": "2022-01-07T12:11:23Z",
}))
);
#[cfg(feature = "sqlx-postgres")]
assert_eq!(
Satellite::find().into_json().one(db).await?,
Some(json!({
"id": 1,
"satellite_name": "Sea-00001-2022",
"launch_date": "2022-01-07T12:11:23+00:00",
"deployment_date": "2022-01-07T12:11:23+00:00",
}))
);

Ok(())
}

0 comments on commit 2157712

Please sign in to comment.