()
| 5 | |
| 6 | #[tokio::test] |
| 7 | async fn test_simple_select() { |
| 8 | // Use a temporary file instead of in-memory database |
| 9 | let test_id = Uuid::new_v4().to_string().replace("-", ""); |
| 10 | let db_path = format!("/tmp/pgsqlite_test_{test_id}.db"); |
| 11 | let db_path_clone = db_path.clone(); |
| 12 | |
| 13 | // Start test server |
| 14 | let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 15 | let port = listener.local_addr().unwrap().port(); |
| 16 | |
| 17 | let server_handle = tokio::spawn(async move { |
| 18 | // Create SQLite database with test data |
| 19 | let db_handler = std::sync::Arc::new( |
| 20 | pgsqlite::session::DbHandler::new(&db_path_clone).unwrap() |
| 21 | ); |
| 22 | |
| 23 | // Initialize test data |
| 24 | db_handler.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)").await.unwrap(); |
| 25 | db_handler.execute("INSERT INTO test (id, name) VALUES (1, 'Alice'), (2, 'Bob')").await.unwrap(); |
| 26 | |
| 27 | // Accept connection |
| 28 | let (stream, addr) = listener.accept().await.unwrap(); |
| 29 | |
| 30 | // Handle connection |
| 31 | pgsqlite::handle_test_connection_with_pool(stream, addr, db_handler).await.unwrap(); |
| 32 | }); |
| 33 | |
| 34 | // Give server time to start |
| 35 | tokio::time::sleep(Duration::from_millis(100)).await; |
| 36 | |
| 37 | // Connect with tokio-postgres |
| 38 | let (client, connection) = timeout( |
| 39 | Duration::from_secs(5), |
| 40 | tokio_postgres::connect( |
| 41 | &format!("host=localhost port={port} dbname=test user=testuser"), |
| 42 | NoTls, |
| 43 | ) |
| 44 | ).await.unwrap().unwrap(); |
| 45 | |
| 46 | // Spawn connection handler |
| 47 | tokio::spawn(async move { |
| 48 | if let Err(e) = connection.await { |
| 49 | eprintln!("connection error: {e}"); |
| 50 | } |
| 51 | }); |
| 52 | |
| 53 | // Execute query |
| 54 | let rows = client.query("SELECT id, name FROM test ORDER BY id", &[]).await.unwrap(); |
| 55 | |
| 56 | assert_eq!(rows.len(), 2); |
| 57 | assert_eq!(rows[0].get::<_, i32>(0), 1); |
| 58 | assert_eq!(rows[0].get::<_, &str>(1), "Alice"); |
| 59 | assert_eq!(rows[1].get::<_, i32>(0), 2); |
| 60 | assert_eq!(rows[1].get::<_, &str>(1), "Bob"); |
| 61 | |
| 62 | server_handle.abort(); |
| 63 | |
| 64 | // Clean up |
nothing calls this directly
no test coverage detected