use sqlx::sqlite::{Sqlite, SqliteRow}; use sqlx_core::row::Row; use sqlx_test::new; use sqlx_test::test_type; test_type!(null>(Sqlite, "NULL" == None:: )); test_type!(bool(Sqlite, "FALSE" == false, "TRUE" == true)); test_type!(i32(Sqlite, "94101" == 94101_i32)); test_type!(i64(Sqlite, "9358295312" == 9358295312_i64)); // NOTE: This behavior can be surprising. Floating-point parameters are widening to double which can // result in strange rounding. test_type!(f32(Sqlite, "3.1410000324249268" == 3.141f32 as f64 as f32)); test_type!(f64(Sqlite, "939399419.1225182" == 939399419.1225182_f64)); test_type!(str(Sqlite, "'this is foo'" == "this is foo", "cast(x'7468697320006973206E756C2D636F6E7461696E696E67' as text)" == "this \0is nul-containing", "''" == "" )); test_type!(bytes>(Sqlite, "X'DEADBEEF'" == vec![0xDE_u8, 0xAD, 0xBE, 0xEF], "X''" == Vec::::new(), "X'0000000052'" == vec![0_u8, 0, 0, 0, 0x52] )); #[cfg(feature = "json")] mod json_tests { use super::*; use serde_json::{json, Value as JsonValue}; use sqlx::types::Json; use sqlx_test::test_type; test_type!(json( Sqlite, "'\"Hello, World\"'" == json!("Hello, World"), "'\"😎\"'" == json!("😎"), "'\"🙋‍♀️\"'" == json!("🙋‍♀️"), "'[\"Hello\",\"World!\"]'" == json!(["Hello", "World!"]) )); #[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq)] struct Friend { name: String, age: u32, } test_type!(json_struct>( Sqlite, "\'{\"name\":\"Joe\",\"age\":33}\'" == Json(Friend { name: "Joe".to_string(), age: 33 }) )); // NOTE: This is testing recursive (and transparent) usage of the `Json` wrapper. You don't // need to wrap the Vec in Json<_> to make the example work. #[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)] struct Customer { json_column: Json>, } test_type!(json_struct_json_column>( Sqlite, "\'{\"json_column\":[1,2]}\'" == Json(Customer { json_column: Json(vec![1, 2]) }) )); #[sqlx_macros::test] async fn it_json_extracts() -> anyhow::Result<()> { let mut conn = new::().await?; let value = sqlx::query("select JSON_EXTRACT(JSON('{ \"number\": 42 }'), '$.number') = ?1") .bind(42_i32) .try_map(|row: SqliteRow| row.try_get::(0)) .fetch_one(&mut conn) .await?; assert_eq!(true, value); Ok(()) } } #[cfg(feature = "chrono")] mod chrono { use super::*; use sqlx::types::chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc}; test_type!(chrono_naive_date_time(Sqlite, "datetime('2019-01-02 05:10:20')" == NaiveDate::from_ymd(2019, 1, 2).and_hms(5, 10, 20) )); test_type!(chrono_date_time_utc>(Sqlite, "datetime('1996-12-20T00:39:57+00:00')" == Utc.ymd(1996, 12, 20).and_hms(0, 39, 57) )); test_type!(chrono_date_time_fixed_offset>(Sqlite, "datetime('2016-11-08T03:50:23-05:00')" == FixedOffset::west(5 * 3600).ymd(2016, 11, 08).and_hms(3, 50, 23) )); }