@@ -5,18 +5,25 @@ use tokio_postgres::Config;
5
5
use crate :: pool:: postgres:: make_client;
6
6
use crate :: Pool ;
7
7
8
+ enum TestDb {
9
+ Postgres {
10
+ original_db_url : String ,
11
+ db_name : String ,
12
+ } ,
13
+ SQLite ,
14
+ }
15
+
8
16
/// Represents a connection to a Postgres database that can be
9
17
/// used in integration tests to test logic that interacts with
10
18
/// a database.
11
19
pub ( crate ) struct TestContext {
12
- db_name : String ,
13
- original_db_url : String ,
20
+ test_db : TestDb ,
14
21
// Pre-cached client to avoid creating unnecessary connections in tests
15
22
client : Pool ,
16
23
}
17
24
18
25
impl TestContext {
19
- async fn new ( db_url : & str ) -> Self {
26
+ async fn new_postgres ( db_url : & str ) -> Self {
20
27
let config: Config = db_url. parse ( ) . expect ( "Cannot parse connection string" ) ;
21
28
22
29
// Create a new database that will be used for this specific test
@@ -62,8 +69,18 @@ impl TestContext {
62
69
let pool = Pool :: open ( test_db_url. as_str ( ) ) ;
63
70
64
71
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 ,
67
84
client : pool,
68
85
}
69
86
}
@@ -77,25 +94,37 @@ impl TestContext {
77
94
// First, we need to stop using the database
78
95
drop ( self . client ) ;
79
96
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
+ }
88
113
}
89
114
}
90
115
116
+ /// Runs a test against an actual database.
117
+ /// Checks both Postgres and SQLite.
91
118
pub ( crate ) async fn run_db_test < F , Fut > ( f : F )
92
119
where
93
- F : FnOnce ( TestContext ) -> Fut ,
120
+ F : Fn ( TestContext ) -> Fut ,
94
121
Fut : Future < Output = anyhow:: Result < TestContext > > ,
95
122
{
123
+ // Postgres
96
124
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" ) ;
99
128
ctx. finish ( ) . await ;
100
129
} else {
101
130
// The github CI does not yet support running containers on Windows,
@@ -109,4 +138,10 @@ where
109
138
) ;
110
139
}
111
140
}
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 ;
112
147
}
0 commit comments