-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathmod.rs
More file actions
84 lines (67 loc) · 2.33 KB
/
mod.rs
File metadata and controls
84 lines (67 loc) · 2.33 KB
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
82
83
84
use crate::error::Error;
use crate::pool::PoolOptions;
use crate::testing::{FixtureSnapshot, TestArgs, TestContext, TestSupport};
use crate::{Sqlite, SqliteConnectOptions};
use std::future::Future;
use std::path::{Path, PathBuf};
pub(crate) use sqlx_core::testing::*;
const BASE_PATH: &str = "target/sqlx/test-dbs";
impl TestSupport for Sqlite {
fn test_context(
args: &TestArgs,
) -> impl Future<Output = Result<TestContext<Self>, Error>> + Send + '_ {
test_context(args)
}
async fn cleanup_test(db_name: &str) -> Result<(), Error> {
crate::fs::remove_file(db_name).await?;
Ok(())
}
async fn cleanup_test_dbs() -> Result<Option<usize>, Error> {
crate::fs::remove_dir_all(BASE_PATH).await?;
Ok(None)
}
async fn snapshot(_conn: &mut Self::Connection) -> Result<FixtureSnapshot<Self>, Error> {
todo!()
}
fn db_name(args: &TestArgs) -> String {
convert_path(args.test_path)
}
}
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()) {
crate::fs::create_dir_all(parent_path)
.await
.expect("failed to create folders");
}
if Path::exists(db_path.as_ref()) {
crate::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),
// This doesn't really matter for SQLite as the databases are independent of each other.
// The main limitation is going to be the number of concurrent running tests.
pool_opts: PoolOptions::new().max_connections(1000),
db_name: db_path,
locking: true,
})
}
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");
}