@@ -5,18 +5,25 @@ use tokio_postgres::Config;
55use crate :: pool:: postgres:: make_client;
66use crate :: Pool ;
77
8+ enum TestDb {
9+ Postgres {
10+ original_db_url : String ,
11+ db_name : String ,
12+ } ,
13+ SQLite ,
14+ }
15+
816/// Represents a connection to a Postgres database that can be
917/// used in integration tests to test logic that interacts with
1018/// a database.
1119pub ( crate ) struct TestContext {
12- db_name : String ,
13- original_db_url : String ,
20+ test_db : TestDb ,
1421 // Pre-cached client to avoid creating unnecessary connections in tests
1522 client : Pool ,
1623}
1724
1825impl TestContext {
19- async fn new ( db_url : & str ) -> Self {
26+ async fn new_postgres ( db_url : & str ) -> Self {
2027 let config: Config = db_url. parse ( ) . expect ( "Cannot parse connection string" ) ;
2128
2229 // Create a new database that will be used for this specific test
@@ -62,8 +69,18 @@ impl TestContext {
6269 let pool = Pool :: open ( test_db_url. as_str ( ) ) ;
6370
6471 Self {
65- db_name,
66- original_db_url : db_url. to_string ( ) ,
72+ test_db : TestDb :: Postgres {
73+ original_db_url : db_url. to_string ( ) ,
74+ db_name,
75+ } ,
76+ client : pool,
77+ }
78+ }
79+
80+ async fn new_sqlite ( ) -> Self {
81+ let pool = Pool :: open ( ":memory:" ) ;
82+ Self {
83+ test_db : TestDb :: SQLite ,
6784 client : pool,
6885 }
6986 }
@@ -77,25 +94,37 @@ impl TestContext {
7794 // First, we need to stop using the database
7895 drop ( self . client ) ;
7996
80- // Then we need to connect to the default database and drop our test DB
81- let client = make_client ( & self . original_db_url )
82- . await
83- . expect ( "Cannot connect to database" ) ;
84- client
85- . execute ( & format ! ( "DROP DATABASE {}" , self . db_name) , & [ ] )
86- . await
87- . unwrap ( ) ;
97+ match self . test_db {
98+ TestDb :: Postgres {
99+ original_db_url,
100+ db_name,
101+ } => {
102+ // Then we need to connect to the default database and drop our test DB
103+ let client = make_client ( & original_db_url)
104+ . await
105+ . expect ( "Cannot connect to database" ) ;
106+ client
107+ . execute ( & format ! ( "DROP DATABASE {db_name}" ) , & [ ] )
108+ . await
109+ . unwrap ( ) ;
110+ }
111+ TestDb :: SQLite => { }
112+ }
88113 }
89114}
90115
116+ /// Runs a test against an actual database.
117+ /// Checks both Postgres and SQLite.
91118pub ( crate ) async fn run_db_test < F , Fut > ( f : F )
92119where
93- F : FnOnce ( TestContext ) -> Fut ,
120+ F : Fn ( TestContext ) -> Fut ,
94121 Fut : Future < Output = anyhow:: Result < TestContext > > ,
95122{
123+ // Postgres
96124 if let Ok ( db_url) = std:: env:: var ( "TEST_DB_URL" ) {
97- let ctx = TestContext :: new ( & db_url) . await ;
98- let ctx = f ( ctx) . await . expect ( "Test failed" ) ;
125+ eprintln ! ( "Running test with Postgres" ) ;
126+ let ctx = TestContext :: new_postgres ( & db_url) . await ;
127+ let ctx = f ( ctx) . await . expect ( "Postgres test failed" ) ;
99128 ctx. finish ( ) . await ;
100129 } else {
101130 // The github CI does not yet support running containers on Windows,
@@ -109,4 +138,10 @@ where
109138 ) ;
110139 }
111140 }
141+
142+ // SQLite
143+ eprintln ! ( "Running test with SQLite" ) ;
144+ let ctx = TestContext :: new_sqlite ( ) . await ;
145+ let ctx = f ( ctx) . await . expect ( "SQLite test failed" ) ;
146+ ctx. finish ( ) . await ;
112147}
0 commit comments