1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::error::Error;
use crate::pool::PoolOptions;
use crate::sqlite::{Sqlite, SqliteConnectOptions};
use crate::testing::{FixtureSnapshot, TestArgs, TestContext, TestSupport};
use futures_core::future::BoxFuture;
use std::path::{Path, PathBuf};
const BASE_PATH: &str = "target/sqlx/test-dbs";
impl TestSupport for Sqlite {
fn test_context(args: &TestArgs) -> BoxFuture<'_, Result<TestContext<Self>, Error>> {
Box::pin(async move {
let res = test_context(args).await;
res
})
}
fn cleanup_test(db_name: &str) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move { Ok(sqlx_rt::fs::remove_file(db_name).await?) })
}
fn cleanup_test_dbs() -> BoxFuture<'static, Result<Option<usize>, Error>> {
Box::pin(async move {
sqlx_rt::fs::remove_dir_all(BASE_PATH).await?;
Ok(None)
})
}
fn snapshot(
_conn: &mut Self::Connection,
) -> BoxFuture<'_, Result<FixtureSnapshot<Self>, Error>> {
todo!()
}
}
async fn test_context(args: &TestArgs) -> Result<TestContext<Sqlite>, Error> {
let db_path = convert_path(args.test_path);
if let Some(parent_path) = Path::parent(db_path.as_ref()) {
sqlx_rt::fs::create_dir_all(parent_path)
.await
.expect("failed to create folders");
}
if Path::exists(db_path.as_ref()) {
sqlx_rt::fs::remove_file(&db_path)
.await
.expect("failed to remove database from previous test run");
}
Ok(TestContext {
connect_opts: SqliteConnectOptions::new()
.filename(&db_path)
.create_if_missing(true),
pool_opts: PoolOptions::new().max_connections(1000),
db_name: db_path,
})
}
fn convert_path(test_path: &str) -> String {
let mut path = PathBuf::from(BASE_PATH);
for segment in test_path.split("::") {
path.push(segment);
}
path.set_extension("sqlite");
path.into_os_string()
.into_string()
.expect("path should be UTF-8")
}
#[test]
fn test_convert_path() {
let path = convert_path("foo::bar::baz::quux");
assert_eq!(path, "target/sqlx/test-dbs/foo/bar/baz/quux.sqlite");
}