MCPcopy Create free account
hub / github.com/erans/pgsqlite / test_db_handler_transactions

Function test_db_handler_transactions

tests/connection_pool_test.rs:30–71  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

28
29#[tokio::test]
30async fn test_db_handler_transactions() -> Result<(), Box<dyn std::error::Error>> {
31 let temp_dir = tempfile::tempdir()?;
32 let db_path = temp_dir.path().join("test_transactions.db");
33 let db_handler = Arc::new(DbHandler::new(db_path.to_str().unwrap())?);
34
35 // Create a session
36 let session_id = Uuid::new_v4();
37 db_handler.create_session_connection(session_id).await?;
38
39 // Create test table
40 db_handler.execute_with_session("CREATE TABLE test_tx (id INTEGER PRIMARY KEY, value INTEGER)", &session_id).await?;
41
42 // Test successful transaction
43 db_handler.begin_with_session(&session_id).await?;
44 db_handler.execute_with_session("INSERT INTO test_tx (value) VALUES (100)", &session_id).await?;
45 db_handler.execute_with_session("INSERT INTO test_tx (value) VALUES (200)", &session_id).await?;
46 db_handler.commit_with_session(&session_id).await?;
47
48 let result = db_handler.query_with_session("SELECT COUNT(*) FROM test_tx", &session_id).await?;
49 assert_eq!(result.rows.len(), 1);
50 // Check that we have 2 rows
51 let count_bytes = result.rows[0][0].as_ref().unwrap();
52 let count_str = std::str::from_utf8(count_bytes).unwrap();
53 let count: i64 = count_str.parse().unwrap();
54 assert_eq!(count, 2);
55
56 // Test rollback
57 db_handler.begin_with_session(&session_id).await?;
58 db_handler.execute_with_session("INSERT INTO test_tx (value) VALUES (300)", &session_id).await?;
59 db_handler.rollback_with_session(&session_id).await?;
60
61 let result = db_handler.query_with_session("SELECT COUNT(*) FROM test_tx", &session_id).await?;
62 let count_bytes = result.rows[0][0].as_ref().unwrap();
63 let count_str = std::str::from_utf8(count_bytes).unwrap();
64 let count: i64 = count_str.parse().unwrap();
65 assert_eq!(count, 2); // Should still be 2
66
67 // Clean up
68 db_handler.remove_session_connection(&session_id);
69
70 Ok(())
71}
72
73#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
74async fn test_db_handler_concurrent_access() -> Result<(), Box<dyn std::error::Error>> {

Callers

nothing calls this directly

Calls 7

begin_with_sessionMethod · 0.80
commit_with_sessionMethod · 0.80
query_with_sessionMethod · 0.80
rollback_with_sessionMethod · 0.80
execute_with_sessionMethod · 0.45

Tested by

no test coverage detected