use crate::utils::browser::BrowserHelpers;
use playwright::api::{page, Page};
use std::error::Error;
use std::fs;
use std::path::Path;
pub async fn test_create_and_query_database_flow(
page: &Page,
username: &str,
) -> Result<(), Box<dyn Error>> {
let expected_title = "Entity 0 - ayb";
assert_eq!(page.title().await?, expected_title);
BrowserHelpers::screenshot_compare(&page, "dashboard_before_database_creation", &[]).await?;
page.click_builder("button:has-text('Create database')")
.timeout(3000.0)
.click()
.await?;
BrowserHelpers::screenshot_compare(&page, "create_database_form", &[]).await?;
page.fill_builder("input[name='database_slug']", "test.sqlite")
.timeout(1000.0)
.fill()
.await?;
BrowserHelpers::screenshot_compare(&page, "database_form_filled", &[]).await?;
page.click_builder("button[type='submit']:has-text('Create database')")
.timeout(5000.0)
.click()
.await?;
BrowserHelpers::screenshot_compare(&page, "database_created", &[]).await?;
let database_page_title = format!("Explore {}/test.sqlite - ayb", username);
assert_eq!(page.title().await?, database_page_title);
let create_table_query = "CREATE TABLE test_table(fname varchar, lname varchar);";
page.fill_builder("textarea[name='query']", create_table_query)
.timeout(1000.0)
.fill()
.await?;
BrowserHelpers::screenshot_compare(&page, "create_table_query", &[]).await?;
page.click_builder("button:has-text('Run query')")
.timeout(5000.0)
.click()
.await?;
BrowserHelpers::screenshot_compare(&page, "table_created", &[]).await?;
let insert_query1 =
"INSERT INTO test_table (fname, lname) VALUES (\"the first\", \"the last\");";
page.fill_builder("textarea[name='query']", "")
.timeout(1000.0)
.fill()
.await?;
page.fill_builder("textarea[name='query']", insert_query1)
.timeout(1000.0)
.fill()
.await?;
page.click_builder("button:has-text('Run query')")
.timeout(5000.0)
.click()
.await?;
let insert_query2 =
"INSERT INTO test_table (fname, lname) VALUES (\"the first2\", \"the last2\");";
page.fill_builder("textarea[name='query']", "")
.timeout(1000.0)
.fill()
.await?;
page.fill_builder("textarea[name='query']", insert_query2)
.timeout(1000.0)
.fill()
.await?;
page.click_builder("button:has-text('Run query')")
.timeout(5000.0)
.click()
.await?;
BrowserHelpers::screenshot_compare(&page, "data_inserted", &[]).await?;
let select_query = "SELECT * FROM test_table;";
page.fill_builder("textarea[name='query']", "")
.timeout(1000.0)
.fill()
.await?;
page.fill_builder("textarea[name='query']", select_query)
.timeout(1000.0)
.fill()
.await?;
BrowserHelpers::screenshot_compare(&page, "select_query", &[]).await?;
page.click_builder("button:has-text('Run query')")
.timeout(5000.0)
.click()
.await?;
BrowserHelpers::screenshot_compare(&page, "query_results", &[]).await?;
let query_results = page.inner_text("#query-results", None).await?;
let expected_results = "Download CSV\nDownload JSON\nfname\tlname\nthe first\tthe last\nthe first2\tthe last2\n2 rows";
assert_eq!(
query_results.trim(),
expected_results,
"Query results should exactly match expected content"
);
let (download_event, _) = tokio::join!(
page.expect_event(page::EventType::Download),
page.click_builder("button:has-text('Download CSV')")
.timeout(3000.0)
.click()
);
let download = match download_event? {
page::Event::Download(d) => d,
_ => return Err("Expected download event".into()),
};
let download_path = format!("./test_download_{}.csv", std::process::id());
download.save_as(&download_path).await?;
let actual_csv_content = fs::read_to_string(&download_path)?;
let expected_csv_content = "fname,lname\nthe first,the last\nthe first2,the last2\n";
assert_eq!(
actual_csv_content.trim(),
expected_csv_content.trim(),
"Downloaded CSV content should match expected data"
);
if Path::new(&download_path).exists() {
fs::remove_file(&download_path)?;
}
let (download_event, _) = tokio::join!(
page.expect_event(page::EventType::Download),
page.click_builder("button:has-text('Download JSON')")
.timeout(3000.0)
.click()
);
let download = match download_event? {
page::Event::Download(d) => d,
_ => return Err("Expected download event".into()),
};
let download_path = format!("./test_download_{}.json", std::process::id());
download.save_as(&download_path).await?;
let actual_json_content = fs::read_to_string(&download_path)?;
let expected_json_content = r#"{"fields":["fname","lname"],"rows":[["the first","the last"],["the first2","the last2"]]}"#;
let actual_json: serde_json::Value = serde_json::from_str(&actual_json_content)?;
let expected_json: serde_json::Value = serde_json::from_str(expected_json_content)?;
assert_eq!(
actual_json, expected_json,
"Downloaded JSON content should match expected data"
);
if Path::new(&download_path).exists() {
fs::remove_file(&download_path)?;
}
BrowserHelpers::screenshot_compare(&page, "database_test_complete", &[]).await?;
Ok(())
}