From 6cea7e2c1b5b998dd2bbd70ac69b33f0473cccd3 Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Mon, 16 Mar 2020 21:05:48 -0700 Subject: [PATCH] macros: add proper test for sqlite using database file --- .gitignore | 3 +++ Cargo.toml | 4 ++++ sqlx-core/src/sqlite/types/mod.rs | 2 +- tests/fixtures/test-db.sqlite | Bin 0 -> 8192 bytes tests/sqlite-macros.rs | 38 ++++++++++++------------------ 5 files changed, 23 insertions(+), 24 deletions(-) create mode 100644 tests/fixtures/test-db.sqlite diff --git a/.gitignore b/.gitignore index bcdb795e..49cd284b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ target/ # Environment .env + +tests/fixtures/*.sqlite-shm +tests/fixtures/*.sqlite-wal diff --git a/Cargo.toml b/Cargo.toml index a27399e9..f6a905ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,6 +76,10 @@ required-features = [ "mysql", "macros" ] name = "sqlite" required-features = [ "sqlite" ] +[[test]] +name = "sqlite-macros" +required-features = [ "sqlite" ] + [[test]] name = "sqlite-raw" required-features = [ "sqlite" ] diff --git a/sqlx-core/src/sqlite/types/mod.rs b/sqlx-core/src/sqlite/types/mod.rs index 50475af0..14f887bb 100644 --- a/sqlx-core/src/sqlite/types/mod.rs +++ b/sqlx-core/src/sqlite/types/mod.rs @@ -64,7 +64,7 @@ impl Display for SqliteTypeInfo { impl TypeInfo for SqliteTypeInfo { fn compatible(&self, other: &Self) -> bool { - self.affinity == other.affinity + self.r#type == other.r#type || self.affinity == other.affinity } fn is_null_type(&self) -> bool { diff --git a/tests/fixtures/test-db.sqlite b/tests/fixtures/test-db.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..513e8e9d1d5bdc52d93ce15bc615d54a9e46e7ff GIT binary patch literal 8192 zcmeI#y9&ZE6b9fV2tq-0b1EEgQ4kkL7prvZ;^hTmjT&rIsVUgem+(D(1JlyFy37BQ z anyhow::Result<()> { +async fn macro_select() -> anyhow::Result<()> { let mut conn = new::().await?; - let account = sqlx::query!( - "with accounts(id, name) as (values (1, 'Herp Derpinson')) select * from accounts" - ) - .fetch_one(&mut conn) - .await?; + let account = sqlx::query!("select * from accounts") + .fetch_one(&mut conn) + .await?; println!("{:?}", account); println!("{}: {}", account.id, account.name); @@ -19,14 +17,11 @@ async fn macro_select_from_cte() -> anyhow::Result<()> { #[cfg_attr(feature = "runtime-async-std", async_std::test)] #[cfg_attr(feature = "runtime-tokio", tokio::test)] -async fn macro_select_from_cte_bind() -> anyhow::Result<()> { +async fn macro_select_bind() -> anyhow::Result<()> { let mut conn = new::().await?; - let account = sqlx::query!( - "with accounts(id, name) as (select 1, 'Herp Derpinson') select * from accounts where id = ?", - 1i32 - ) - .fetch_one(&mut conn) - .await?; + let account = sqlx::query!("select * from accounts where id = ?", 1i32) + .fetch_one(&mut conn) + .await?; println!("{:?}", account); println!("{}: {}", account.id, account.name); @@ -36,8 +31,8 @@ async fn macro_select_from_cte_bind() -> anyhow::Result<()> { #[derive(Debug)] struct RawAccount { - r#type: i32, - name: Option, + id: i32, + name: String, } #[cfg_attr(feature = "runtime-async-std", async_std::test)] @@ -45,15 +40,12 @@ struct RawAccount { async fn test_query_as_raw() -> anyhow::Result<()> { let mut conn = new::().await?; - let account = sqlx::query_as!( - RawAccount, - "SELECT * from (select 1 as type, cast(null as char) as name) accounts" - ) - .fetch_one(&mut conn) - .await?; + let account = sqlx::query_as!(RawAccount, "SELECT * from accounts") + .fetch_one(&mut conn) + .await?; - assert_eq!(None, account.name); - assert_eq!(1, account.r#type); + assert_eq!(1, account.id); + assert_eq!("Herp Derpinson", account.name); println!("{:?}", account);