11use axum:: { middleware, response:: IntoResponse } ;
22use axum_sqlx_tx:: State ;
33use sqlx:: { sqlite:: SqliteArguments , Arguments as _} ;
4- use tempfile:: NamedTempFile ;
54use tower:: ServiceExt ;
65
76type Tx = axum_sqlx_tx:: Tx < sqlx:: Sqlite > ;
87
98#[ tokio:: test]
109async 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]
2827async 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]
4544async 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]
5958async 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]
7776async 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]
174167async 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]
203193async 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]
214204async 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]
223213async 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 )
302289where
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
338322struct MyExtractorError ( axum_sqlx_tx:: Error ) ;
0 commit comments