Skip to content

Commit

Permalink
fix test with mem db
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Feb 3, 2025
1 parent 32cc65b commit 13eecc8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ fn create_default_database(configuration_directory: &Path) -> String {
prefix + ":memory:?cache=shared"
}

#[cfg(any(test, not(feature = "lambda-web")))]
fn encode_uri(path: &Path) -> std::borrow::Cow<str> {
const ASCII_SET: &percent_encoding::AsciiSet = &percent_encoding::NON_ALPHANUMERIC
.remove(b'-')
Expand Down
20 changes: 15 additions & 5 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl FileSystem {

// If not in local fs and we have db_fs, check database
if !local_exists {
log::debug!("File {path:?} not found in local filesystem, checking database");
if let Some(db_fs) = &self.db_fs_queries {
return db_fs.file_exists(app_state, path).await;
}
Expand Down Expand Up @@ -226,7 +227,7 @@ impl DbFsQueries {
db_kind: AnyKind,
) -> anyhow::Result<AnyStatement<'static>> {
let exists_query = format!(
"SELECT 1 from sqlpage_files WHERE path = {}",
"SELECT 1 from sqlpage_files WHERE path = {} LIMIT 1",
make_placeholder(db_kind, 1),
);
let param_types: &[AnyTypeInfo; 1] = &[<str as Type<Postgres>>::type_info().into()];
Expand Down Expand Up @@ -282,11 +283,20 @@ impl DbFsQueries {
}

async fn file_exists(&self, app_state: &AppState, path: &Path) -> anyhow::Result<bool> {
self.exists
let query = self
.exists
.query_as::<(i32,)>()
.bind(path.display().to_string())
.fetch_optional(&app_state.db.connection)
.await
.bind(path.display().to_string());
log::trace!(
"Checking if file {path:?} exists by executing query: \n\
{}\n\
with parameters: {:?}",
self.exists.sql(),
(path,)
);
let result = query.fetch_optional(&app_state.db.connection).await;
log::debug!("DB File exists result: {:?}", result);
result
.map(|result| result.is_some())
.with_context(|| format!("Unable to check if {path:?} exists in the database"))
}
Expand Down
16 changes: 15 additions & 1 deletion tests/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,13 @@ async fn test_failed_copy_followed_by_query() -> actix_web::Result<()> {
}

#[actix_web::test]
async fn test_routing_with_db_fs_and_prefix() {
async fn test_routing_with_db_fs() {
let mut config = test_config();
// ignore this test if we are running on a temporary in-memory database
if config.database_url.contains("memory") {
return;
}

config.site_prefix = "/prefix/".to_string();
let state = AppState::init(&config).await.unwrap();

Expand All @@ -730,6 +735,7 @@ async fn test_routing_with_db_fs_and_prefix() {
};
state.db.connection.execute(insert_sql).await.unwrap();

let state = AppState::init(&config).await.unwrap();
let app_data = actix_web::web::Data::new(state);

// Test on_db.sql
Expand All @@ -743,7 +749,15 @@ async fn test_routing_with_db_fs_and_prefix() {
body_str.contains("Hi from db !"),
"{body_str}\nexpected to contain: Hi from db !"
);
}

#[actix_web::test]
async fn test_routing_with_prefix() {
let mut config = test_config();
config.site_prefix = "/prefix/".to_string();
let state = AppState::init(&config).await.unwrap();

let app_data = actix_web::web::Data::new(state);
// Test basic routing with prefix
let resp = req_path_with_app_data(
"/prefix/tests/sql_test_files/it_works_simple.sql",
Expand Down

0 comments on commit 13eecc8

Please sign in to comment.