Skip to content

Commit 13eecc8

Browse files
committed
fix test with mem db
1 parent 32cc65b commit 13eecc8

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/app_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ fn create_default_database(configuration_directory: &Path) -> String {
447447
prefix + ":memory:?cache=shared"
448448
}
449449

450+
#[cfg(any(test, not(feature = "lambda-web")))]
450451
fn encode_uri(path: &Path) -> std::borrow::Cow<str> {
451452
const ASCII_SET: &percent_encoding::AsciiSet = &percent_encoding::NON_ALPHANUMERIC
452453
.remove(b'-')

src/filesystem.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl FileSystem {
144144

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

284285
async fn file_exists(&self, app_state: &AppState, path: &Path) -> anyhow::Result<bool> {
285-
self.exists
286+
let query = self
287+
.exists
286288
.query_as::<(i32,)>()
287-
.bind(path.display().to_string())
288-
.fetch_optional(&app_state.db.connection)
289-
.await
289+
.bind(path.display().to_string());
290+
log::trace!(
291+
"Checking if file {path:?} exists by executing query: \n\
292+
{}\n\
293+
with parameters: {:?}",
294+
self.exists.sql(),
295+
(path,)
296+
);
297+
let result = query.fetch_optional(&app_state.db.connection).await;
298+
log::debug!("DB File exists result: {:?}", result);
299+
result
290300
.map(|result| result.is_some())
291301
.with_context(|| format!("Unable to check if {path:?} exists in the database"))
292302
}

tests/index.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,13 @@ async fn test_failed_copy_followed_by_query() -> actix_web::Result<()> {
709709
}
710710

711711
#[actix_web::test]
712-
async fn test_routing_with_db_fs_and_prefix() {
712+
async fn test_routing_with_db_fs() {
713713
let mut config = test_config();
714+
// ignore this test if we are running on a temporary in-memory database
715+
if config.database_url.contains("memory") {
716+
return;
717+
}
718+
714719
config.site_prefix = "/prefix/".to_string();
715720
let state = AppState::init(&config).await.unwrap();
716721

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

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

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

754+
#[actix_web::test]
755+
async fn test_routing_with_prefix() {
756+
let mut config = test_config();
757+
config.site_prefix = "/prefix/".to_string();
758+
let state = AppState::init(&config).await.unwrap();
759+
760+
let app_data = actix_web::web::Data::new(state);
747761
// Test basic routing with prefix
748762
let resp = req_path_with_app_data(
749763
"/prefix/tests/sql_test_files/it_works_simple.sql",

0 commit comments

Comments
 (0)