{"record":{"id":"24321f576f2df518","repo":"SeaQL/sea-orm","slug":"not-mock-connection","errorCode":null,"errorMessage":"Not mock connection","messagePattern":"Not mock connection","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sea-orm-sync/src/database/db_connection.rs","lineNumber":535,"sourceCode":"                    .map_err(TransactionError::Connection)?;\n                transaction.run(_callback)\n            }\n            DatabaseConnectionType::Disconnected => Err(conn_err(\"Disconnected\").into()),\n        }\n    }\n}\n\n#[cfg(feature = \"mock\")]\nimpl DatabaseConnection {\n    /// Generate a database connection for testing the Mock database\n    ///\n    /// # Panics\n    ///\n    /// Panics if [DbConn] is not a mock connection.\n    pub fn as_mock_connection(&self) -> &crate::MockDatabaseConnection {\n        match &self.inner {\n            DatabaseConnectionType::MockDatabaseConnection(mock_conn) => mock_conn,\n            _ => panic!(\"Not mock connection\"),\n        }\n    }\n\n    /// Get the transaction log as a collection Vec<[crate::Transaction]>\n    ///\n    /// # Panics\n    ///\n    /// Panics if the mocker mutex is being held by another thread.\n    pub fn into_transaction_log(self) -> Vec<crate::Transaction> {\n        let mut mocker = self\n            .as_mock_connection()\n            .get_mocker_mutex()\n            .lock()\n            .expect(\"Fail to acquire mocker\");\n        mocker.drain_transaction_log()\n    }\n}\n","sourceCodeStart":517,"sourceCodeEnd":553,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/sea-orm-sync/src/database/db_connection.rs#L517-L553","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nlet conn = Database::connect(\"sqlite://test.db\").unwrap();\nlet mock = conn.as_mock_connection(); // panics\n// after\nlet db = MockDatabase::new(DatabaseBackend::Sqlite);\nlet conn: DatabaseConnection = db.into_connection();\nlet mock = conn.as_mock_connection(); // ok","handlingStrategy":"type-guard","validationCode":"assert!(matches!(conn.get_database_backend(), _)); // mock connections report the backend set at MockDatabase::new; prefer checking construction\nlet conn = MockDatabase::new(DatabaseBackend::Sqlite).into_connection();","typeGuard":"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 */ }","tryCatchPattern":"// Rust panics are not catchable with Result; use catch_unwind only at test boundaries:\nlet result = std::panic::catch_unwind(|| conn.as_mock_connection());\nassert!(result.is_err(), \"expected mock connection\");","preventionTips":["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"],"tags":["panic","mock","testing","database"],"backgroundTag":"invalid-state-transition","analyzedSha":"e29bcd1b417c41a553b386fe94511d7c64a1c8ec","analyzedAt":"2026-09-10T11:31:52.468Z","contentChangedAt":"2026-09-10T11:31:52.468Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}