use std::path::PathBuf;
fn create_temp_dir(suffix: &str) -> PathBuf {
let temp_dir = std::env::temp_dir().join(format!(
"pg2any_compression_test_{}_{}",
suffix,
std::process::id()
));
std::fs::create_dir_all(&temp_dir).unwrap();
temp_dir
}
#[test]
fn test_compression_env_var_parsing() {
std::env::remove_var("PG2ANY_ENABLE_COMPRESSION");
assert_eq!(
std::env::var("PG2ANY_ENABLE_COMPRESSION").ok().as_deref() == Some("true")
|| std::env::var("PG2ANY_ENABLE_COMPRESSION").ok().as_deref() == Some("1"),
false,
"Default should be false"
);
std::env::set_var("PG2ANY_ENABLE_COMPRESSION", "true");
assert_eq!(
std::env::var("PG2ANY_ENABLE_COMPRESSION").ok().as_deref() == Some("true"),
true
);
std::env::set_var("PG2ANY_ENABLE_COMPRESSION", "1");
assert_eq!(
std::env::var("PG2ANY_ENABLE_COMPRESSION").ok().as_deref() == Some("1"),
true
);
std::env::set_var("PG2ANY_ENABLE_COMPRESSION", "false");
assert_eq!(
std::env::var("PG2ANY_ENABLE_COMPRESSION").ok().as_deref() == Some("true")
|| std::env::var("PG2ANY_ENABLE_COMPRESSION").ok().as_deref() == Some("1"),
false
);
std::env::remove_var("PG2ANY_ENABLE_COMPRESSION");
}
#[test]
fn test_compression_file_extensions() {
let temp_dir = create_temp_dir("ext_test");
let sql_file = temp_dir.join("tx_001_001.sql");
std::fs::write(&sql_file, "SELECT 1;").unwrap();
assert!(sql_file.exists());
assert!(sql_file.to_string_lossy().ends_with(".sql"));
assert!(!sql_file.to_string_lossy().ends_with(".sql.gz"));
let gz_file = temp_dir.join("tx_002_002.sql.gz");
std::fs::write(&gz_file, &[0x1f, 0x8b, 0x08]).unwrap(); assert!(gz_file.exists());
assert!(gz_file.to_string_lossy().ends_with(".sql.gz"));
let _ = std::fs::remove_dir_all(&temp_dir);
}