Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions src/db/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,210 @@ fn quote_conn_value(value: &str) -> String {
let escaped = value.replace('\\', "\\\\").replace('\'', "\\'");
format!("'{}'", escaped)
}

#[cfg(test)]
mod tests {
use super::*;

// --- ConnectionConfig ---

#[test]
fn test_default_config() {
let config = ConnectionConfig::default();
assert_eq!(config.host, "localhost");
assert_eq!(config.port, 5432);
assert_eq!(config.database, "postgres");
assert_eq!(config.username, "postgres");
assert!(config.password.is_empty());
assert_eq!(config.ssl_mode, SslMode::Prefer);
assert!(!config.accept_invalid_certs);
assert!(config.ca_cert_path.is_none());
assert!(!config.use_aws_rds_certs);
}

#[test]
fn test_connection_string() {
let config = ConnectionConfig {
host: "myhost".into(),
port: 5433,
database: "mydb".into(),
username: "myuser".into(),
password: "mypass".into(),
ssl_mode: SslMode::Require,
..Default::default()
};
let s = config.connection_string();
assert!(s.contains("host='myhost'"));
assert!(s.contains("port=5433"));
assert!(s.contains("dbname='mydb'"));
assert!(s.contains("user='myuser'"));
assert!(s.contains("password='mypass'"));
assert!(s.contains("sslmode=require"));
}

#[test]
fn test_connection_string_ssl_modes() {
let modes = vec![
(SslMode::Disable, "disable"),
(SslMode::Prefer, "prefer"),
(SslMode::Require, "require"),
(SslMode::VerifyCa, "verify-ca"),
(SslMode::VerifyFull, "verify-full"),
];
for (mode, expected) in modes {
let config = ConnectionConfig {
ssl_mode: mode,
..Default::default()
};
assert!(
config
.connection_string()
.contains(&format!("sslmode={}", expected)),
"Expected sslmode={} for {:?}",
expected,
mode
);
}
}

#[test]
fn test_display_string() {
let config = ConnectionConfig {
username: "admin".into(),
host: "db.example.com".into(),
port: 5432,
database: "production".into(),
..Default::default()
};
assert_eq!(
config.display_string(),
"admin@db.example.com:5432/production"
);
}

#[test]
fn test_connection_string_escaping() {
let config = ConnectionConfig {
password: "pass'word\\test".into(),
..Default::default()
};
let s = config.connection_string();
assert!(s.contains("pass\\'word\\\\test"));
}

// --- AWS RDS detection ---

#[test]
fn test_is_aws_rds_host() {
let config = ConnectionConfig {
host: "mydb.abc123.us-east-1.rds.amazonaws.com".into(),
..Default::default()
};
assert!(config.is_aws_rds_host());
}

#[test]
fn test_is_not_aws_rds_host() {
let config = ConnectionConfig {
host: "localhost".into(),
..Default::default()
};
assert!(!config.is_aws_rds_host());
}

#[test]
fn test_should_use_aws_rds_certs_auto() {
let config = ConnectionConfig {
host: "mydb.abc123.us-east-1.rds.amazonaws.com".into(),
..Default::default()
};
assert!(config.should_use_aws_rds_certs());
}

#[test]
fn test_should_use_aws_rds_certs_explicit() {
let config = ConnectionConfig {
host: "custom-proxy.example.com".into(),
use_aws_rds_certs: true,
..Default::default()
};
assert!(config.should_use_aws_rds_certs());
}

// --- ConnectionManager ---

#[test]
fn test_new_manager() {
let mgr = ConnectionManager::new();
assert!(!mgr.is_connected());
assert_eq!(mgr.current_database, "postgres");
assert_eq!(mgr.current_schema, "public");
}

// --- SSL mode default ---

#[test]
fn test_ssl_mode_default() {
let mode = SslMode::default();
assert_eq!(mode, SslMode::Prefer);
}

// --- quote_conn_value ---

#[test]
fn test_quote_simple() {
assert_eq!(quote_conn_value("hello"), "'hello'");
}

#[test]
fn test_quote_with_special_chars() {
assert_eq!(quote_conn_value("it's"), "'it\\'s'");
assert_eq!(quote_conn_value("back\\slash"), "'back\\\\slash'");
}

// --- base64 decoder ---

#[test]
fn test_base64_decode() {
let decoded = base64_decode("SGVsbG8gV29ybGQ=").unwrap();
assert_eq!(String::from_utf8(decoded).unwrap(), "Hello World");
}

#[test]
fn test_base64_decode_no_padding() {
let decoded = base64_decode("SGVsbG8").unwrap();
assert_eq!(String::from_utf8(decoded).unwrap(), "Hello");
}

#[test]
fn test_base64_decode_with_newlines() {
let decoded = base64_decode("SGVs\nbG8=").unwrap();
assert_eq!(String::from_utf8(decoded).unwrap(), "Hello");
}

// --- Serialization ---

#[test]
fn test_config_serialization() {
let config = ConnectionConfig::default();
let toml_str = toml::to_string(&config).unwrap();
assert!(toml_str.contains("host"));
assert!(!toml_str.contains("password")); // password is skip_serializing
}

#[test]
fn test_config_deserialization() {
let toml_str = r#"
name = "Test"
host = "localhost"
port = 5432
database = "testdb"
username = "user"
ssl_mode = "Require"
"#;
let config: ConnectionConfig = toml::from_str(toml_str).unwrap();
assert_eq!(config.name, "Test");
assert_eq!(config.ssl_mode, SslMode::Require);
assert!(config.password.is_empty());
}
}
93 changes: 93 additions & 0 deletions src/db/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,99 @@ fn parse_rows(rows: &[Row], execution_time: Duration) -> QueryResult {
}
}

#[cfg(test)]
mod tests {
use super::*;

// --- CellValue display ---

#[test]
fn test_null_display() {
assert_eq!(CellValue::Null.display(), "NULL");
}

#[test]
fn test_bool_display() {
assert_eq!(CellValue::Bool(true).display(), "true");
assert_eq!(CellValue::Bool(false).display(), "false");
}

#[test]
fn test_integer_display() {
assert_eq!(CellValue::Int16(42).display(), "42");
assert_eq!(CellValue::Int32(-100).display(), "-100");
assert_eq!(CellValue::Int64(9_999_999).display(), "9999999");
}

#[test]
fn test_float_display() {
assert_eq!(CellValue::Float32(3.14).display(), "3.14");
assert_eq!(CellValue::Float64(2.718).display(), "2.718");
}

#[test]
fn test_text_display() {
assert_eq!(CellValue::Text("hello".into()).display(), "hello");
}

#[test]
fn test_bytes_display() {
assert_eq!(CellValue::Bytes(vec![1, 2, 3]).display(), "[3 bytes]");
}

#[test]
fn test_json_display() {
let val = serde_json::json!({"key": "value"});
let display = CellValue::Json(val).display();
assert!(display.contains("key"));
assert!(display.contains("value"));
}

#[test]
fn test_array_display() {
let arr = CellValue::Array(vec![
CellValue::Int32(1),
CellValue::Int32(2),
CellValue::Int32(3),
]);
assert_eq!(arr.display(), "{1, 2, 3}");
}

#[test]
fn test_unknown_display() {
assert_eq!(CellValue::Unknown("raw".into()).display(), "raw");
}

// --- CellValue display_width ---

#[test]
fn test_display_width() {
assert_eq!(CellValue::Null.display_width(), 4); // "NULL"
assert_eq!(CellValue::Text("hello".into()).display_width(), 5);
assert_eq!(CellValue::Int32(100).display_width(), 3);
}

// --- QueryResult ---

#[test]
fn test_empty_result() {
let r = QueryResult::empty();
assert!(r.columns.is_empty());
assert!(r.rows.is_empty());
assert_eq!(r.row_count, 0);
assert!(r.error.is_none());
assert!(r.affected_rows.is_none());
}

#[test]
fn test_error_result() {
let r = QueryResult::error("bad query".into(), Duration::from_millis(10));
assert!(r.error.is_some());
assert_eq!(r.error.unwrap(), "bad query");
assert!(r.rows.is_empty());
}
}

fn extract_value(row: &Row, idx: usize, pg_type: &Type) -> CellValue {
// Try to extract based on type
match *pg_type {
Expand Down
Loading