Skip to content

Commit 4a9a99c

Browse files
committed
refactor: use in-memory sqlite for tests and examples
This avoids having to manage temporary files, and lets us drop the `tempfile` dev dependency.
1 parent 1940658 commit 4a9a99c

File tree

3 files changed

+14
-32
lines changed

3 files changed

+14
-32
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@ tower-service = "0.3"
4141
axum-sqlx-tx = { path = ".", features = ["runtime-tokio-rustls", "sqlite"] }
4242
axum = "0.6.4"
4343
hyper = "0.14.17"
44-
tempfile = "3.3.0"
4544
tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] }
4645
tower = "0.4.12"

examples/example.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ type Tx = axum_sqlx_tx::Tx<sqlx::Sqlite>;
1111
#[tokio::main]
1212
async fn main() -> Result<(), Box<dyn Error>> {
1313
// You can use any sqlx::Pool
14-
let db = tempfile::NamedTempFile::new()?;
15-
let pool = sqlx::SqlitePool::connect(&format!("sqlite://{}", db.path().display())).await?;
14+
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await?;
1615

1716
// Create a table (in a real application you might run migrations)
1817
sqlx::query("CREATE TABLE IF NOT EXISTS numbers (number INT PRIMARY KEY);")

tests/lib.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use axum::{middleware, response::IntoResponse};
22
use axum_sqlx_tx::State;
33
use sqlx::{sqlite::SqliteArguments, Arguments as _};
4-
use tempfile::NamedTempFile;
54
use tower::ServiceExt;
65

76
type Tx = axum_sqlx_tx::Tx<sqlx::Sqlite>;
87

98
#[tokio::test]
109
async fn commit_on_success() {
11-
let (_db, pool, response) = build_app(|mut tx: Tx| async move {
10+
let (pool, response) = build_app(|mut tx: Tx| async move {
1211
let (_, name) = insert_user(&mut tx, 1, "huge hackerman").await;
1312
format!("hello {name}")
1413
})
@@ -26,7 +25,7 @@ async fn commit_on_success() {
2625

2726
#[tokio::test]
2827
async fn commit_on_redirection() {
29-
let (_db, pool, response) = build_app(|mut tx: Tx| async move {
28+
let (pool, response) = build_app(|mut tx: Tx| async move {
3029
let (_, _) = insert_user(&mut tx, 1, "john redirect").await;
3130
http::StatusCode::SEE_OTHER
3231
})
@@ -43,7 +42,7 @@ async fn commit_on_redirection() {
4342

4443
#[tokio::test]
4544
async fn rollback_on_error() {
46-
let (_db, pool, response) = build_app(|mut tx: Tx| async move {
45+
let (pool, response) = build_app(|mut tx: Tx| async move {
4746
insert_user(&mut tx, 1, "michael oxmaul").await;
4847
http::StatusCode::BAD_REQUEST
4948
})
@@ -57,7 +56,7 @@ async fn rollback_on_error() {
5756

5857
#[tokio::test]
5958
async fn explicit_commit() {
60-
let (_db, pool, response) = build_app(|mut tx: Tx| async move {
59+
let (pool, response) = build_app(|mut tx: Tx| async move {
6160
insert_user(&mut tx, 1, "michael oxmaul").await;
6261
tx.commit().await.unwrap();
6362
http::StatusCode::BAD_REQUEST
@@ -75,10 +74,7 @@ async fn explicit_commit() {
7574

7675
#[tokio::test]
7776
async fn extract_from_middleware_and_handler() {
78-
let db = NamedTempFile::new().unwrap();
79-
let pool = sqlx::SqlitePool::connect(&format!("sqlite://{}", db.path().display()))
80-
.await
81-
.unwrap();
77+
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
8278

8379
sqlx::query("CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY, name TEXT);")
8480
.execute(&pool)
@@ -146,10 +142,7 @@ async fn substates() {
146142
}
147143
}
148144

149-
let db = NamedTempFile::new().unwrap();
150-
let pool = sqlx::SqlitePool::connect(&format!("sqlite://{}", db.path().display()))
151-
.await
152-
.unwrap();
145+
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
153146

154147
let (state, layer) = Tx::setup(pool);
155148

@@ -172,10 +165,7 @@ async fn substates() {
172165

173166
#[tokio::test]
174167
async fn missing_layer() {
175-
let db = NamedTempFile::new().unwrap();
176-
let pool = sqlx::SqlitePool::connect(&format!("sqlite://{}", db.path().display()))
177-
.await
178-
.unwrap();
168+
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
179169

180170
// Note that we have to explicitly ignore the `_layer`, making it hard to do this accidentally.
181171
let (state, _layer) = Tx::setup(pool);
@@ -201,7 +191,7 @@ async fn missing_layer() {
201191

202192
#[tokio::test]
203193
async fn overlapping_extractors() {
204-
let (_, _, response) = build_app(|_: Tx, _: Tx| async move {}).await;
194+
let (_, response) = build_app(|_: Tx, _: Tx| async move {}).await;
205195

206196
assert!(response.status.is_server_error());
207197
assert_eq!(
@@ -212,7 +202,7 @@ async fn overlapping_extractors() {
212202

213203
#[tokio::test]
214204
async fn extractor_error_override() {
215-
let (_, _, response) =
205+
let (_, response) =
216206
build_app(|_: Tx, _: axum_sqlx_tx::Tx<sqlx::Sqlite, MyExtractorError>| async move {}).await;
217207

218208
assert!(response.status.is_client_error());
@@ -221,10 +211,7 @@ async fn extractor_error_override() {
221211

222212
#[tokio::test]
223213
async fn layer_error_override() {
224-
let db = NamedTempFile::new().unwrap();
225-
let pool = sqlx::SqlitePool::connect(&format!("sqlite://{}", db.path().display()))
226-
.await
227-
.unwrap();
214+
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
228215

229216
sqlx::query("CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY);")
230217
.execute(&pool)
@@ -298,15 +285,12 @@ struct Response {
298285
body: axum::body::Bytes,
299286
}
300287

301-
async fn build_app<H, T>(handler: H) -> (NamedTempFile, sqlx::SqlitePool, Response)
288+
async fn build_app<H, T>(handler: H) -> (sqlx::SqlitePool, Response)
302289
where
303290
H: axum::handler::Handler<T, State<sqlx::Sqlite>, axum::body::Body>,
304291
T: 'static,
305292
{
306-
let db = NamedTempFile::new().unwrap();
307-
let pool = sqlx::SqlitePool::connect(&format!("sqlite://{}", db.path().display()))
308-
.await
309-
.unwrap();
293+
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
310294

311295
sqlx::query("CREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY, name TEXT);")
312296
.execute(&pool)
@@ -332,7 +316,7 @@ where
332316
let status = response.status();
333317
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
334318

335-
(db, pool, Response { status, body })
319+
(pool, Response { status, body })
336320
}
337321

338322
struct MyExtractorError(axum_sqlx_tx::Error);

0 commit comments

Comments
 (0)