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

Function test_create_insert_select

tests/query_test.rs:72–126  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

70
71#[tokio::test]
72async fn test_create_insert_select() {
73 // Use a temporary file instead of in-memory database
74 let test_id = Uuid::new_v4().to_string().replace("-", "");
75 let db_path = format!("/tmp/pgsqlite_test_{test_id}.db");
76 let db_path_clone = db_path.clone();
77
78 // Start test server
79 let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
80 let port = listener.local_addr().unwrap().port();
81
82 let server_handle = tokio::spawn(async move {
83 let db_handler = std::sync::Arc::new(
84 pgsqlite::session::DbHandler::new(&db_path_clone).unwrap()
85 );
86
87 let (stream, addr) = listener.accept().await.unwrap();
88 pgsqlite::handle_test_connection_with_pool(stream, addr, db_handler).await.unwrap();
89 });
90
91 tokio::time::sleep(Duration::from_millis(100)).await;
92
93 // Connect with tokio-postgres
94 let (client, connection) = timeout(
95 Duration::from_secs(5),
96 tokio_postgres::connect(
97 &format!("host=localhost port={port} dbname=test user=testuser"),
98 NoTls,
99 )
100 ).await.unwrap().unwrap();
101
102 tokio::spawn(connection);
103
104 // Create table
105 client.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT)", &[])
106 .await.unwrap();
107
108 // Insert data
109 let inserted = client.execute("INSERT INTO users (id, email) VALUES (1, 'test@example.com')", &[])
110 .await.unwrap();
111 assert_eq!(inserted, 1);
112
113 // Query data
114 let rows = client.query("SELECT * FROM users", &[]).await.unwrap();
115 assert_eq!(rows.len(), 1);
116 assert_eq!(rows[0].get::<_, i32>(0), 1);
117 assert_eq!(rows[0].get::<_, &str>(1), "test@example.com");
118
119 server_handle.abort();
120
121 // Clean up
122 let _ = std::fs::remove_file(&db_path);
123 let _ = std::fs::remove_file(format!("{db_path}-journal"));
124 let _ = std::fs::remove_file(format!("{db_path}-wal"));
125 let _ = std::fs::remove_file(format!("{db_path}-shm"));
126}
127
128#[tokio::test]
129async fn test_transactions() {

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