Skip to content

DatabaseConnectionFailed->RedbDatabaseError->DatabaseAlreadyOpen during testing #321

@aramrw

Description

@aramrw
[cfg(test)]
mod db_tests {
    use crate::{database::dictionary_database::Queries, Yomichan};
    use pretty_assertions::assert_eq;

    #[test]
    fn lookup_exact() {
        let ycd = Yomichan::new("./a/yomichan/data.yc").unwrap();
        let res = ycd.lookup_exact("日本語").unwrap();
        let first = res.first().unwrap();
        assert_eq!(
            ("日本語", "にほんご"),
            (&*first.expression, &*first.reading)
        )
    }

    #[test]
    fn bulk_lookup_term() {
        let ycd = Yomichan::new("./a/yomichan/data.yc").unwrap();
        let res = ycd.bulk_lookup_term(Queries::Exact(&["日本語"])).unwrap();
        let first = res.first().unwrap();
        assert_eq!(("日本語", "にほんご"), (&*first.term, &*first.reading))
    }
}

They both pass and fail, depending on which one comes first.

running 2 tests
- test database::handlers::db_tests::lookup_exact ... FAILED
+ test database::handlers::db_tests::bulk_lookup_term ... ok

failures:
---- database::handlers::db_tests::lookup_exact stdout ----
- thread 'database::handlers::db_tests::lookup_exact' panicked at src\database\handlers.rs:434:57:
called `Result::unwrap()` on an `Err` value: DatabaseConnectionFailed(RedbDatabaseError(DatabaseAlreadyOpen))
running 2 tests
- test database::handlers::db_tests::bulk_lookup_term ... FAILED
+ test database::handlers::db_tests::lookup_exact ... ok

failures:

---- database::handlers::db_tests::bulk_lookup_term stdout ----
- thread 'database::handlers::db_tests::bulk_lookup_term' panicked at src\database\handlers.rs:445:57:
called `Result::unwrap()` on an `Err` value: DatabaseConnectionFailed(RedbDatabaseError(DatabaseAlreadyOpen))

Why is the database still open when this function returns? Shouldnt it have been dropped and closed by the time bulk_lookup_term() is called in the test? And vice versa. I'm not sure what I'm doing wrong or how to fix it

/// Queries multiple terms via text.
pub fn lookup_exact<Q: AsRef<str> + Debug>(&self, query: Q) -> Result<VecDBTermEntry, DBError> {
        let query = query.as_ref();
        let db = DBBuilder::new().open(&DB_MODELS, &self.db_path)?;
        let rtx = db.r_transaction()?;

        let mut exps = query_sw(&rtx, DatabaseTermEntryKey::expression, query)?;
        let readings = query_sw(&rtx, DatabaseTermEntryKey::reading, query)?;

        if exps.is_empty() && readings.is_empty() {
            return Err(DBError::Query(format!("no entries found for: {}", query)));
        }

        exps.extend(readings);
        Ok(exps)
}
fn query_sw<'a, R: native_db::ToInput>(
    rtx: &RTransaction,
    key: impl ToKeyDefinition<KeyOptions>,
    query: impl ToKey + 'a,
) -> Result<Vec<R>, DBError> {
    Ok(rtx
        .scan()
        .secondary(key)?
        .start_with(query)
        .collect::<Result<Vec<R>, db_type::Error>>()?)
}

Thank you for the help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions