SeaQL/sea-orm · error
Not mock connection
Error message
Not mock connection
What it means
as_mock_connection() unwraps the DatabaseConnection's inner enum to a MockDatabaseConnection. It panics when the connection was created as a real database (sqlx pool or rusqlite) or is Disconnected, because there is no mock connection to return. The library panics rather than returning Result because this is a test-support API whose misuse is a programming error.
Source
Thrown at sea-orm-sync/src/database/db_connection.rs:535
.map_err(TransactionError::Connection)?;
transaction.run(_callback)
}
DatabaseConnectionType::Disconnected => Err(conn_err("Disconnected").into()),
}
}
}
#[cfg(feature = "mock")]
impl DatabaseConnection {
/// Generate a database connection for testing the Mock database
///
/// # Panics
///
/// Panics if [DbConn] is not a mock connection.
pub fn as_mock_connection(&self) -> &crate::MockDatabaseConnection {
match &self.inner {
DatabaseConnectionType::MockDatabaseConnection(mock_conn) => mock_conn,
_ => panic!("Not mock connection"),
}
}
/// Get the transaction log as a collection Vec<[crate::Transaction]>
///
/// # Panics
///
/// Panics if the mocker mutex is being held by another thread.
pub fn into_transaction_log(self) -> Vec<crate::Transaction> {
let mut mocker = self
.as_mock_connection()
.get_mocker_mutex()
.lock()
.expect("Fail to acquire mocker");
mocker.drain_transaction_log()
}
}
View on GitHub (pinned to e29bcd1b41)
Solutions
- Build the connection with MockDatabase::new(DatabaseBackend::Postgres).into_connection() before calling as_mock_connection
- Check the backend first with conn.get_database_backend() / whether it is a mock before unwrapping
- In tests, ensure the connection under test comes from the mock fixture, not a real .connect() call
Example fix
// before
let conn = Database::connect("sqlite://test.db").unwrap();
let mock = conn.as_mock_connection(); // panics
// after
let db = MockDatabase::new(DatabaseBackend::Sqlite);
let conn: DatabaseConnection = db.into_connection();
let mock = conn.as_mock_connection(); // ok Defensive patterns
Strategy: type-guard
Validate before calling
assert!(matches!(conn.get_database_backend(), _)); // mock connections report the backend set at MockDatabase::new; prefer checking construction let conn = MockDatabase::new(DatabaseBackend::Sqlite).into_connection();
Type guard
fn is_mock(conn: &DatabaseConnection) -> bool { std::panic::catch_unwind(|| conn.as_mock_connection()).is_ok() } // or track construction: fn from_mock(conn: &DatabaseConnection) -> bool { /* store a flag at build time */ } Try / catch
// Rust panics are not catchable with Result; use catch_unwind only at test boundaries: let result = std::panic::catch_unwind(|| conn.as_mock_connection()); assert!(result.is_err(), "expected mock connection");
Prevention
- Always derive test connections from MockDatabase::new(...).into_connection()
- Never mix real connect() and mock accessors in the same fixture
- Add a helper constructor in test code that returns (DatabaseConnection, MockDatabase) as a pair
When it happens
Trigger: Calling as_mock_connection() on a DatabaseConnection built via Database::connect (real backend), connect_stream, or one that is Disconnected; only a connection obtained through MockDatabase::new(...).into_connection() satisfies the match.
Common situations: Unit tests where the fixture set up a real SQLite/Postgres connection instead of MockDatabase; copy-pasted test code mixing real and mock connections; calling the accessor on the global/default connection by mistake.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Not proxy connection
- There is no open transaction to commit
- There is no open transaction to rollback
- There is no open transaction to commit
- There is no open transaction to rollback
AI-assisted analysis of SeaQL/sea-orm@e29bcd1b41 (2026-09-10).
Data as JSON: /api/errors/24321f576f2df518.
Report an issue: GitHub.