This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
//! Opt-in integration tests against a real PostgreSQL server.
|
||||
//!
|
||||
//! Run with:
|
||||
//!
|
||||
//! ```bash
|
||||
//! OMG_PGS_TEST_DATABASE_URL=postgresql://user:password@localhost:5432/db \
|
||||
//! cargo test --test database
|
||||
//! ```
|
||||
//!
|
||||
//! The tests only read `pg_catalog`, run `SELECT 1`, and attempt to resolve a
|
||||
//! table that should not exist. They do not create or modify database objects.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use omg_pgs::db::{DbManager, Validation, execute};
|
||||
|
||||
fn test_url() -> Option<String> {
|
||||
std::env::var("OMG_PGS_TEST_DATABASE_URL")
|
||||
.ok()
|
||||
.filter(|url| !url.trim().is_empty())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn validates_statements_and_loads_catalog() {
|
||||
let Some(url) = test_url() else {
|
||||
eprintln!("skipping: OMG_PGS_TEST_DATABASE_URL is not set");
|
||||
return;
|
||||
};
|
||||
|
||||
let manager = DbManager::new();
|
||||
let database = manager.database(&url).await.unwrap();
|
||||
database.check_connection().await.unwrap();
|
||||
|
||||
assert_eq!(database.validate("SELECT 1").await, Validation::Ok);
|
||||
|
||||
match database
|
||||
.validate("SELECT * FROM omg_pgs_table_that_does_not_exist")
|
||||
.await
|
||||
{
|
||||
Validation::Failed { code, .. } => assert_eq!(code, "42P01"),
|
||||
other => panic!("expected SQLSTATE 42P01, got {other:?}"),
|
||||
}
|
||||
|
||||
let schema = database.schema(false).await.unwrap();
|
||||
assert!(!schema.schemas.is_empty());
|
||||
assert!(!schema.search_path.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn executes_a_simple_query() {
|
||||
let Some(url) = test_url() else {
|
||||
eprintln!("skipping: OMG_PGS_TEST_DATABASE_URL is not set");
|
||||
return;
|
||||
};
|
||||
|
||||
let outcome = execute(&url, "SELECT 1 AS value", Duration::from_secs(20))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(outcome.columns, vec!["value"]);
|
||||
assert_eq!(outcome.rows, vec![vec![Some("1".to_string())]]);
|
||||
}
|
||||
Reference in New Issue
Block a user