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