- Add lib crate exposing modules for integration testing - Add dev-dependencies: tokio-test 0.4, tempfile - Refactor parse_csv_fields() as pure function for unit testing - Add field validation (minimum 16 fields required) - Fix repository last_insert_id using SELECT LAST_INSERT_ID() - Add 10 lib tests for CSV parsing and date formatting - Add 10 config tests for environment configuration - Add 7 import tests for CSV file parsing - Add 6 models tests for database structs - Add 12 repository tests for CRUD operations
106 lines
3.1 KiB
Rust
106 lines
3.1 KiB
Rust
//! Tests for the config module.
|
|
//!
|
|
//! AI AGENT NOTE: These tests verify configuration loading, environment
|
|
//! parsing, and database connection URL generation.
|
|
|
|
use invoice_generator::config::{Config, DatabaseConfig, Env};
|
|
|
|
/// Tests that Env::default() returns Prod.
|
|
#[test]
|
|
fn env_default_is_prod() {
|
|
assert_eq!(Env::default(), Env::Prod);
|
|
}
|
|
|
|
/// Tests Env::from_str with valid short forms.
|
|
#[test]
|
|
fn env_from_str_valid_short() {
|
|
assert_eq!("prod".parse::<Env>().unwrap(), Env::Prod);
|
|
assert_eq!("dev".parse::<Env>().unwrap(), Env::Dev);
|
|
assert_eq!("test".parse::<Env>().unwrap(), Env::Test);
|
|
}
|
|
|
|
/// Tests Env::from_str with valid long forms (aliases).
|
|
#[test]
|
|
fn env_from_str_valid_aliases() {
|
|
assert_eq!("production".parse::<Env>().unwrap(), Env::Prod);
|
|
assert_eq!("development".parse::<Env>().unwrap(), Env::Dev);
|
|
assert_eq!("testing".parse::<Env>().unwrap(), Env::Test);
|
|
}
|
|
|
|
/// Tests Env::from_str is case-insensitive.
|
|
#[test]
|
|
fn env_from_str_case_insensitive() {
|
|
assert_eq!("PROD".parse::<Env>().unwrap(), Env::Prod);
|
|
assert_eq!("Dev".parse::<Env>().unwrap(), Env::Dev);
|
|
assert_eq!("TEST".parse::<Env>().unwrap(), Env::Test);
|
|
}
|
|
|
|
/// Tests Env::from_str with invalid value returns error.
|
|
#[test]
|
|
fn env_from_str_invalid() {
|
|
let result: Result<Env, _> = "invalid".parse();
|
|
assert!(result.is_err());
|
|
assert!(result.unwrap_err().contains("Unknown environment"));
|
|
}
|
|
|
|
/// Tests Env::as_str returns correct string.
|
|
#[test]
|
|
fn env_as_str() {
|
|
assert_eq!(Env::Prod.as_str(), "prod");
|
|
assert_eq!(Env::Dev.as_str(), "dev");
|
|
assert_eq!(Env::Test.as_str(), "test");
|
|
}
|
|
|
|
/// Tests Env::database_name returns correct database names.
|
|
#[test]
|
|
fn env_database_name() {
|
|
assert_eq!(Env::Prod.database_name(), "rusty_petroleum");
|
|
assert_eq!(Env::Dev.database_name(), "rusty_petroleum_dev");
|
|
assert_eq!(Env::Test.database_name(), "rusty_petroleum_test");
|
|
}
|
|
|
|
/// Tests DatabaseConfig::connection_url without password.
|
|
#[test]
|
|
fn db_connection_url_without_password() {
|
|
let config = DatabaseConfig {
|
|
host: "localhost".to_string(),
|
|
port: 3306,
|
|
user: "test_user".to_string(),
|
|
password: "".to_string(),
|
|
name: "test_db".to_string(),
|
|
};
|
|
|
|
let url = config.connection_url();
|
|
assert_eq!(url, "mysql://test_user@localhost:3306/test_db");
|
|
}
|
|
|
|
/// Tests DatabaseConfig::connection_url with password.
|
|
#[test]
|
|
fn db_connection_url_with_password() {
|
|
let config = DatabaseConfig {
|
|
host: "localhost".to_string(),
|
|
port: 3306,
|
|
user: "test_user".to_string(),
|
|
password: "secret".to_string(),
|
|
name: "test_db".to_string(),
|
|
};
|
|
|
|
let url = config.connection_url();
|
|
assert_eq!(url, "mysql://test_user:secret@localhost:3306/test_db");
|
|
}
|
|
|
|
/// Tests DatabaseConfig::connection_url with custom port.
|
|
#[test]
|
|
fn db_connection_url_custom_port() {
|
|
let config = DatabaseConfig {
|
|
host: "127.0.0.1".to_string(),
|
|
port: 3307,
|
|
user: "user".to_string(),
|
|
password: "pass".to_string(),
|
|
name: "mydb".to_string(),
|
|
};
|
|
|
|
let url = config.connection_url();
|
|
assert_eq!(url, "mysql://user:pass@127.0.0.1:3307/mydb");
|
|
}
|