|
| 1 | +use common::*; |
| 2 | +mod common; |
| 3 | + |
| 4 | +fn _get_mysql_connection_url() -> String { |
| 5 | + if std::env::var("MYSQL_DATABASE_URL").is_ok() { |
| 6 | + std::env::var("MYSQL_DATABASE_URL").unwrap() |
| 7 | + } else { |
| 8 | + panic!("Please set the environment variable MYSQL_DATABASE_URL to run this test.") |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +#[tokio::test] |
| 13 | +async fn test_storage_macro_dbc_mysql( |
| 14 | +) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { |
| 15 | + let (_ir, _is, cl) = set_up_test_env_single_user().await?; |
| 16 | + |
| 17 | + cl.create_entry( |
| 18 | + "storage_macro_test:db:url", |
| 19 | + _get_mysql_connection_url().as_bytes(), |
| 20 | + ) |
| 21 | + .await?; |
| 22 | + |
| 23 | + cl.create_entry( |
| 24 | + "storage_macro_test:db:create_db", |
| 25 | + b"CREATE DATABASE IF NOT EXISTS test_db" as &[u8], |
| 26 | + ) |
| 27 | + .await?; |
| 28 | + |
| 29 | + // Create a table and insert dummy data |
| 30 | + cl.create_entry( |
| 31 | + "storage_macro_test:db:create_table", |
| 32 | + b"CREATE TABLE IF NOT EXISTS users (name VARCHAR(255), age INT)" as &[u8], |
| 33 | + ) |
| 34 | + .await?; |
| 35 | + |
| 36 | + cl.create_entry( |
| 37 | + "storage_macro_test:db:insert_data", |
| 38 | + b"INSERT INTO users VALUES ('Alice', 20)" as &[u8], |
| 39 | + ) |
| 40 | + .await?; |
| 41 | + |
| 42 | + cl.create_entry( |
| 43 | + "storage_macro_test:db:query_users", |
| 44 | + b"SELECT * FROM users WHERE name = ? AND age = ?" as &[u8], |
| 45 | + ) |
| 46 | + .await?; |
| 47 | + |
| 48 | + cl.create_entry( |
| 49 | + "storage_macro_test:db:cleanup", |
| 50 | + b"DROP TABLE IF EXISTS users" as &[u8], |
| 51 | + ) |
| 52 | + .await?; |
| 53 | + |
| 54 | + cl.read_entry("storage_macro_test:db:$dbc:create_db") |
| 55 | + .await?; |
| 56 | + cl.read_entry("storage_macro_test:db:$dbc:create_table") |
| 57 | + .await?; |
| 58 | + cl.read_entry("storage_macro_test:db:$dbc:insert_data") |
| 59 | + .await?; |
| 60 | + let query_result = cl |
| 61 | + .read_entry("storage_macro_test:db:$dbc:query_users:'Alice':'20'") |
| 62 | + .await?; |
| 63 | + |
| 64 | + let stringified = String::from_utf8(query_result.clone())?; |
| 65 | + println!("{}", stringified); |
| 66 | + assert_eq!( |
| 67 | + stringified, |
| 68 | + r#"{"rows":[{"values":[{"Bytes":"QWxpY2U"},{"Int":20}],"columns":[{"name":"name","column_type":"VARCHAR"},{"name":"age","column_type":"INT"}]}],"affected_row_count":0}"# |
| 69 | + ); |
| 70 | + |
| 71 | + let deserialized: rdbc2::dbc::QueryResult = serde_json::from_slice(&query_result)?; |
| 72 | + assert_eq!(deserialized.rows.len(), 1); |
| 73 | + |
| 74 | + // Test query string parsing order |
| 75 | + cl.create_entry( |
| 76 | + "storage_macro_test:db:query_users2", |
| 77 | + b"SELECT * FROM users WHERE name = ? AND age = ?" as &[u8], |
| 78 | + ) |
| 79 | + .await?; |
| 80 | + |
| 81 | + cl.create_entry( |
| 82 | + "storage_macro_test:db:query_users2:additional", |
| 83 | + b"SELECT * FROM users WHERE name = ?" as &[u8], |
| 84 | + ) |
| 85 | + .await?; |
| 86 | + |
| 87 | + let result = cl |
| 88 | + .read_entry("storage_macro_test:db:$dbc:query_users2:additional:'Alice'") |
| 89 | + .await?; |
| 90 | + |
| 91 | + assert_eq!( |
| 92 | + String::from_utf8(result)?, |
| 93 | + r#"{"rows":[{"values":[{"Bytes":"QWxpY2U"},{"Int":20}],"columns":[{"name":"name","column_type":"VARCHAR"},{"name":"age","column_type":"INT"}]}],"affected_row_count":0}"# |
| 94 | + ); |
| 95 | + |
| 96 | + cl.read_entry("storage_macro_test:db:$dbc:cleanup").await?; |
| 97 | + |
| 98 | + Ok(()) |
| 99 | +} |
0 commit comments