Making it simpler
All checks were successful
Rust Build / Check (pull_request) Successful in 1m14s
Rust Build / Test Suite (pull_request) Successful in 1m1s
Rust Build / Rustfmt (pull_request) Successful in 29s
Rust Build / Clippy (pull_request) Successful in 47s
Rust Build / build (pull_request) Successful in 1m13s
All checks were successful
Rust Build / Check (pull_request) Successful in 1m14s
Rust Build / Test Suite (pull_request) Successful in 1m1s
Rust Build / Rustfmt (pull_request) Successful in 29s
Rust Build / Clippy (pull_request) Successful in 47s
Rust Build / build (pull_request) Successful in 1m13s
This commit is contained in:
@@ -1,2 +1 @@
|
||||
DATABASE_URL=postgres://username:password@localhost/database_name
|
||||
TEST_DATABASE_URL=postgres://icarus_op_test:password@localhost/icarus_auth_test
|
||||
|
@@ -70,18 +70,18 @@ jobs:
|
||||
# --- Optional but Recommended: Database Migrations Step ---
|
||||
- name: Run Database Migrations
|
||||
env:
|
||||
# Define TEST_DATABASE_URL using service details and secrets
|
||||
TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||
# Define DATABASE_URL using service details and secrets
|
||||
DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||
# Make SSH agent available if migrations fetch private dependencies
|
||||
SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }}
|
||||
run: |
|
||||
echo "Running database migrations..."
|
||||
cargo install sqlx-cli --no-default-features --features native-tls,postgres
|
||||
sqlx database setup --database-url $TEST_DATABASE_URL
|
||||
sqlx database setup --database-url $DATABASE_URL
|
||||
- name: Run tests
|
||||
env:
|
||||
# Define TEST_DATABASE_URL for tests to use
|
||||
TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||
# Define DATABASE_URL for tests to use
|
||||
DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }}
|
||||
RUST_LOG: info # Optional: configure test log level
|
||||
# Make SSH agent available if tests fetch private dependencies
|
||||
SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }}
|
||||
|
13
src/lib.rs
13
src/lib.rs
@@ -10,13 +10,6 @@ mod keys {
|
||||
pub mod error {
|
||||
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
||||
}
|
||||
|
||||
pub mod test {
|
||||
pub const DBURL: &str = "TEST_DATABASE_URL";
|
||||
pub mod error {
|
||||
pub const ERROR: &str = "TEST_DATABASE_URL must be set in .env";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod connection_settings {
|
||||
@@ -44,10 +37,6 @@ pub mod db_pool {
|
||||
#[cfg(debug_assertions)] // Example: Only load .env in debug builds
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
env::var(keys::test::DBURL).expect(keys::test::error::ERROR)
|
||||
} else {
|
||||
env::var(keys::DBURL).expect(keys::error::ERROR)
|
||||
}
|
||||
env::var(keys::DBURL).expect(keys::error::ERROR)
|
||||
}
|
||||
}
|
||||
|
@@ -93,8 +93,7 @@ mod tests {
|
||||
}
|
||||
|
||||
pub async fn connect_to_db(db_name: &str) -> Result<sqlx::PgPool, sqlx::Error> {
|
||||
let db_url = std::env::var("TEST_DATABASE_URL")
|
||||
.expect("TEST_DATABASE_URL must be set for tests");
|
||||
let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set for tests");
|
||||
let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name);
|
||||
sqlx::PgPool::connect_with(options).await
|
||||
}
|
||||
@@ -123,7 +122,7 @@ mod tests {
|
||||
pub fn get_database_name() -> Result<String, Box<dyn std::error::Error>> {
|
||||
dotenvy::dotenv().ok(); // Load .env file if it exists
|
||||
|
||||
match std::env::var("TEST_DATABASE_URL") {
|
||||
match std::env::var("DATABASE_URL") {
|
||||
Ok(database_url) => {
|
||||
let parsed_url = url::Url::parse(&database_url)?;
|
||||
if parsed_url.scheme() == "postgres" || parsed_url.scheme() == "postgresql" {
|
||||
@@ -140,7 +139,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
// TEST_DATABASE_URL environment variable not found
|
||||
// DATABASE_URL environment variable not found
|
||||
Err("Error parsing".into())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user