#[cfg(test)]
use sqlx::{Row, SqlitePool};
#[cfg(test)]
use crate::storage::run_migrations;
#[cfg(test)]
pub async fn create_test_pool() -> SqlitePool {
let pool = SqlitePool::connect("sqlite::memory:")
.await
.expect("Failed to create test database pool");
run_migrations(&pool)
.await
.expect("Failed to run migrations");
pool
}
#[cfg(test)]
pub async fn create_test_url_status_default(pool: &SqlitePool) -> i64 {
create_test_url_status(
pool,
"example.com",
"example.com",
200,
None,
1704067200000i64,
)
.await
}
#[cfg(test)]
pub async fn create_test_url_status(
pool: &SqlitePool,
domain: &str,
final_domain: &str,
status: i64,
run_id: Option<&str>,
timestamp: i64,
) -> i64 {
sqlx::query(
"INSERT INTO url_status (
initial_domain, final_domain, ip_address, http_status, http_status_text,
response_time_seconds, title, observed_at_ms, run_id
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING id",
)
.bind(domain)
.bind(final_domain)
.bind("192.0.2.1")
.bind(status)
.bind("OK")
.bind(1.5f64)
.bind("Test Page")
.bind(timestamp)
.bind(run_id)
.fetch_one(pool)
.await
.expect("Failed to insert test URL status")
.get::<i64, _>(0)
}
#[cfg(test)]
pub async fn create_test_run(pool: &SqlitePool, run_id: &str, timestamp: i64) -> String {
sqlx::query("INSERT INTO runs (run_id, start_time_ms, version, fingerprints_source, fingerprints_version, geoip_version) VALUES (?, ?, ?, ?, ?, ?)")
.bind(run_id)
.bind(timestamp)
.bind("0.1.6")
.bind("test://fingerprints")
.bind("1.0.0")
.bind("1.0.0")
.execute(pool)
.await
.expect("Failed to insert test run");
run_id.to_string()
}