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

Function test_transactions

tests/query_test.rs:129–188  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

127
128#[tokio::test]
129async fn test_transactions() {
130 // Use a temporary file instead of in-memory database
131 let test_id = Uuid::new_v4().to_string().replace("-", "");
132 let db_path = format!("/tmp/pgsqlite_test_{test_id}.db");
133 let db_path_clone = db_path.clone();
134
135 // Start test server
136 let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
137 let port = listener.local_addr().unwrap().port();
138
139 let server_handle = tokio::spawn(async move {
140 let db_handler = std::sync::Arc::new(
141 pgsqlite::session::DbHandler::new(&db_path_clone).unwrap()
142 );
143
144 let (stream, addr) = listener.accept().await.unwrap();
145 pgsqlite::handle_test_connection_with_pool(stream, addr, db_handler).await.unwrap();
146 });
147
148 tokio::time::sleep(Duration::from_millis(100)).await;
149
150 // Connect with tokio-postgres
151 let (client, connection) = timeout(
152 Duration::from_secs(5),
153 tokio_postgres::connect(
154 &format!("host=localhost port={port} dbname=test user=testuser"),
155 NoTls,
156 )
157 ).await.unwrap().unwrap();
158
159 tokio::spawn(connection);
160
161 // Create table
162 client.execute("CREATE TABLE counter (value INTEGER)", &[]).await.unwrap();
163 client.execute("INSERT INTO counter VALUES (0)", &[]).await.unwrap();
164
165 // Test transaction commit
166 client.execute("BEGIN", &[]).await.unwrap();
167 client.execute("UPDATE counter SET value = 1", &[]).await.unwrap();
168 client.execute("COMMIT", &[]).await.unwrap();
169
170 let rows = client.query("SELECT value FROM counter", &[]).await.unwrap();
171 assert_eq!(rows[0].get::<_, i32>(0), 1);
172
173 // Test transaction rollback
174 client.execute("BEGIN", &[]).await.unwrap();
175 client.execute("UPDATE counter SET value = 2", &[]).await.unwrap();
176 client.execute("ROLLBACK", &[]).await.unwrap();
177
178 let rows = client.query("SELECT value FROM counter", &[]).await.unwrap();
179 assert_eq!(rows[0].get::<_, i32>(0), 1); // Should still be 1
180
181 server_handle.abort();
182
183 // Clean up
184 let _ = std::fs::remove_file(&db_path);
185 let _ = std::fs::remove_file(format!("{db_path}-journal"));
186 let _ = std::fs::remove_file(format!("{db_path}-wal"));

Callers

nothing calls this directly

Calls 5

abortMethod · 0.80
cloneMethod · 0.45
executeMethod · 0.45
queryMethod · 0.45

Tested by

no test coverage detected