Skip to content

Commit 6cea7e2

Browse files
abonandermehcode
authored andcommitted
macros: add proper test for sqlite using database file
1 parent 8328e07 commit 6cea7e2

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ target/
99

1010
# Environment
1111
.env
12+
13+
tests/fixtures/*.sqlite-shm
14+
tests/fixtures/*.sqlite-wal

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ required-features = [ "mysql", "macros" ]
7676
name = "sqlite"
7777
required-features = [ "sqlite" ]
7878

79+
[[test]]
80+
name = "sqlite-macros"
81+
required-features = [ "sqlite" ]
82+
7983
[[test]]
8084
name = "sqlite-raw"
8185
required-features = [ "sqlite" ]

sqlx-core/src/sqlite/types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Display for SqliteTypeInfo {
6464

6565
impl TypeInfo for SqliteTypeInfo {
6666
fn compatible(&self, other: &Self) -> bool {
67-
self.affinity == other.affinity
67+
self.r#type == other.r#type || self.affinity == other.affinity
6868
}
6969

7070
fn is_null_type(&self) -> bool {

tests/fixtures/test-db.sqlite

8 KB
Binary file not shown.

tests/sqlite-macros.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ use sqlx_test::new;
33

44
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
55
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
6-
async fn macro_select_from_cte() -> anyhow::Result<()> {
6+
async fn macro_select() -> anyhow::Result<()> {
77
let mut conn = new::<Sqlite>().await?;
8-
let account = sqlx::query!(
9-
"with accounts(id, name) as (values (1, 'Herp Derpinson')) select * from accounts"
10-
)
11-
.fetch_one(&mut conn)
12-
.await?;
8+
let account = sqlx::query!("select * from accounts")
9+
.fetch_one(&mut conn)
10+
.await?;
1311

1412
println!("{:?}", account);
1513
println!("{}: {}", account.id, account.name);
@@ -19,14 +17,11 @@ async fn macro_select_from_cte() -> anyhow::Result<()> {
1917

2018
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
2119
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
22-
async fn macro_select_from_cte_bind() -> anyhow::Result<()> {
20+
async fn macro_select_bind() -> anyhow::Result<()> {
2321
let mut conn = new::<Sqlite>().await?;
24-
let account = sqlx::query!(
25-
"with accounts(id, name) as (select 1, 'Herp Derpinson') select * from accounts where id = ?",
26-
1i32
27-
)
28-
.fetch_one(&mut conn)
29-
.await?;
22+
let account = sqlx::query!("select * from accounts where id = ?", 1i32)
23+
.fetch_one(&mut conn)
24+
.await?;
3025

3126
println!("{:?}", account);
3227
println!("{}: {}", account.id, account.name);
@@ -36,24 +31,21 @@ async fn macro_select_from_cte_bind() -> anyhow::Result<()> {
3631

3732
#[derive(Debug)]
3833
struct RawAccount {
39-
r#type: i32,
40-
name: Option<String>,
34+
id: i32,
35+
name: String,
4136
}
4237

4338
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
4439
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
4540
async fn test_query_as_raw() -> anyhow::Result<()> {
4641
let mut conn = new::<Sqlite>().await?;
4742

48-
let account = sqlx::query_as!(
49-
RawAccount,
50-
"SELECT * from (select 1 as type, cast(null as char) as name) accounts"
51-
)
52-
.fetch_one(&mut conn)
53-
.await?;
43+
let account = sqlx::query_as!(RawAccount, "SELECT * from accounts")
44+
.fetch_one(&mut conn)
45+
.await?;
5446

55-
assert_eq!(None, account.name);
56-
assert_eq!(1, account.r#type);
47+
assert_eq!(1, account.id);
48+
assert_eq!("Herp Derpinson", account.name);
5749

5850
println!("{:?}", account);
5951

0 commit comments

Comments
 (0)