Skip to content

Commit 1f30d4d

Browse files
committed
Use live db for tests
1 parent c5b43a8 commit 1f30d4d

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/app_config.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ impl AppConfig {
5353
config.configuration_directory.clone_from(config_dir);
5454
}
5555

56-
if let Some(config_dir) = &cli.config_dir {
57-
config.configuration_directory.clone_from(config_dir);
58-
}
59-
6056
config.configuration_directory = std::fs::canonicalize(&config.configuration_directory)
6157
.unwrap_or_else(|_| config.configuration_directory.clone());
6258

@@ -507,17 +503,24 @@ impl DevOrProd {
507503
}
508504
}
509505

506+
#[must_use]
507+
pub fn test_database_url() -> String {
508+
std::env::var("DATABASE_URL").unwrap_or_else(|_| "sqlite::memory:".to_string())
509+
}
510+
510511
#[cfg(test)]
511512
pub mod tests {
513+
pub use super::test_database_url;
512514
use super::AppConfig;
513515

514516
#[must_use]
515517
pub fn test_config() -> AppConfig {
516518
serde_json::from_str::<AppConfig>(
517-
r#"{
518-
"database_url": "sqlite::memory:",
519-
"listen_on": "localhost:8080"
520-
}"#,
519+
&serde_json::json!({
520+
"database_url": test_database_url(),
521+
"listen_on": "localhost:8080"
522+
})
523+
.to_string(),
521524
)
522525
.unwrap()
523526
}

src/webserver/database/sql_to_json.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ pub fn row_to_string(row: &AnyRow) -> Option<String> {
112112

113113
#[cfg(test)]
114114
mod tests {
115+
use crate::app_config::tests::test_database_url;
116+
115117
use super::*;
116118
use sqlx::Connection;
117119

@@ -124,8 +126,7 @@ mod tests {
124126

125127
fn db_specific_test(db_type: &str) -> Option<String> {
126128
setup_logging();
127-
let db_url =
128-
std::env::var("DATABASE_URL").unwrap_or_else(|_| "sqlite://:memory:".to_string());
129+
let db_url = test_database_url();
129130
if db_url.starts_with(db_type) {
130131
Some(db_url)
131132
} else {
@@ -137,8 +138,7 @@ mod tests {
137138
#[actix_web::test]
138139
async fn test_row_to_json() -> anyhow::Result<()> {
139140
use sqlx::Connection;
140-
let db_url =
141-
std::env::var("DATABASE_URL").unwrap_or_else(|_| "sqlite://:memory:".to_string());
141+
let db_url = test_database_url();
142142
let mut c = sqlx::AnyConnection::connect(&db_url).await?;
143143
let row = sqlx::query(
144144
"SELECT \

tests/index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use actix_web::{
1212
HttpResponse,
1313
};
1414
use sqlpage::{
15-
app_config::AppConfig,
15+
app_config::{test_database_url, AppConfig},
1616
webserver::{self, http::main_handler},
1717
AppState,
1818
};
@@ -580,7 +580,7 @@ async fn req_path_with_app_data(
580580
}
581581

582582
pub fn test_config() -> AppConfig {
583-
let db_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| "sqlite::memory:".to_string());
583+
let db_url = test_database_url();
584584
serde_json::from_str::<AppConfig>(&format!(
585585
r#"{{
586586
"database_url": "{}",

0 commit comments

Comments
 (0)