feat(test): add tests

This commit is contained in:
2026-01-22 17:41:59 +08:00
parent e36bd23d39
commit 760ad2e5f8
4 changed files with 125 additions and 0 deletions

1
.env.example Normal file
View File

@@ -0,0 +1 @@
DATABASE_URL=postgres://user:password@ip:port/db

View File

@@ -36,3 +36,19 @@ pub struct LogRecord {
pub module: String, pub module: String,
pub trace_id: Option<String>, pub trace_id: Option<String>,
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_level_ordering() {
assert!(LogLevel::ERROR > LogLevel::INFO);
assert!(LogLevel::DEBUG < LogLevel::FATAL);
}
#[test]
fn test_level_display() {
assert_eq!(LogLevel::INFO.to_string(), "INFO");
}
}

61
tests/db_test.rs Normal file
View File

@@ -0,0 +1,61 @@
use std::env;
use chrono::Utc;
// tests/db_test.rs
use dotenv::dotenv;
use rust_logger::model::{LogLevel, LogRecord};
use rust_logger::outputs::LogOutput;
use rust_logger::outputs::postgres::PostgresOutput;
use sqlx::postgres::PgPoolOptions;
#[sqlx::test] // sqlx 提供的强大宏,自动为每个测试创建一个临时数据库
async fn test_postgres_write() {
dotenv().ok();
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&db_url)
.await
.unwrap();
// 1. 准备表结构 (需要在测试库里建表)
sqlx::query(
r#"
CREATE TABLE IF NOT EXISTS app_logs (
id BIGSERIAL PRIMARY KEY,
service_name VARCHAR(50),
log_level VARCHAR(10),
message TEXT,
module VARCHAR(100),
created_at TIMESTAMPTZ,
trace_id VARCHAR(64)
)
"#,
)
.execute(&pool)
.await
.unwrap();
// 2. 初始化 Output
let output = PostgresOutput::new(pool.clone());
// 3. 写入日志
// ... 构造 record ...
let record = LogRecord {
service_name: "db_test".into(),
timestamp: Utc::now(),
level: LogLevel::INFO,
message: "Hello Integration Test Db".into(),
module: "tests".into(),
trace_id: Some("123-abc".into()),
};
output.write(&record).await;
// 4. 验证是否写入
let count: i64 =
sqlx::query_scalar("SELECT count(*) FROM app_logs WHERE service_name = 'db_test'")
.fetch_one(&pool)
.await
.unwrap();
assert!(count > 1);
}

47
tests/file_test.rs Normal file
View File

@@ -0,0 +1,47 @@
// tests/file_test.rs
use chrono::Utc;
use rust_logger::model::{LogLevel, LogRecord};
use rust_logger::outputs::LogOutput;
use rust_logger::outputs::file::FileOutput;
use std::fs;
use std::path::Path;
#[tokio::test] // 使用 tokio 的测试宏
async fn test_file_output_write() {
let dir = "test_logs";
let prefix = "test-service";
// 1. 清理环境 (确保之前跑剩下的文件删掉)
if Path::new(dir).exists() {
fs::remove_dir_all(dir).unwrap();
}
// 2. 初始化 Output
let output = FileOutput::new(dir, prefix)
.await
.expect("Failed to create output");
// 3. 构造一条假日志
let record = LogRecord {
service_name: "file_test".into(),
timestamp: Utc::now(),
level: LogLevel::INFO,
message: "Hello Integration Test File".into(),
module: "tests".into(),
trace_id: Some("123-abc".into()),
};
// 4. 写入
output.write(&record).await;
// 5. 验证文件是否存在且内容正确
let mut entries = fs::read_dir(dir).unwrap();
let entry = entries.next().unwrap().unwrap();
let content = fs::read_to_string(entry.path()).unwrap();
assert!(content.contains("Hello Integration Test"));
assert!(content.contains("[INFO]"));
// 6. 清理
fs::remove_dir_all(dir).unwrap();
}