Skip to content

Commit

Permalink
refactor(vrd): 🎨 improved log_entry_async and updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 4, 2024
1 parent c140ce0 commit 0ad9424
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
8 changes: 3 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ pub fn create_log_entry(
}

Check failure on line 158 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Format

Diff in /home/runner/work/vrd/vrd/src/lib.rs

/// Log an entry asynchronously.
pub async fn log_entry_async(entry: Log) {
entry
.log()
.await
.unwrap_or_else(|e| eprintln!("Failed to log entry: {}", e));
pub async fn log_entry_async(entry: Log) -> Result<(), Box<dyn std::error::Error>> {
entry.log().await?;
Ok(())

Check warning on line 163 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L162-L163

Added lines #L162 - L163 were not covered by tests
}
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//!
//! ```toml
//! [dependencies]
//! vrd = "0.1.0"
//! vrd = "0.0.1"
//! ```
//!
//! Then, use the library in your Rust code:
Expand Down Expand Up @@ -75,7 +75,16 @@ fn main() {
&error_message,

Check failure on line 75 in src/main.rs

View workflow job for this annotation

GitHub Actions / Format

Diff in /home/runner/work/vrd/vrd/src/main.rs
);

runtime.block_on(async { log_entry_async(log_entry).await });
match runtime.block_on(async { log_entry_async(log_entry).await }) {

Check warning on line 78 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L78

Added line #L78 was not covered by tests
Ok(_) => {
// Logging successful
println!("Error logged successfully.");

Check warning on line 81 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L81

Added line #L81 was not covered by tests
}
Err(e) => {

Check warning on line 83 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L83

Added line #L83 was not covered by tests
// Logging failed
eprintln!("Failed to log error: {}", e);
}

Check warning on line 86 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L85-L86

Added lines #L85 - L86 were not covered by tests
}

process::exit(1);
}
Expand Down
34 changes: 29 additions & 5 deletions tests/test_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
#[cfg(test)]
mod tests {
use rlg::{log_format::LogFormat, log_level::LogLevel};
use vrd::create_log_entry;
use uuid::Uuid;
use vrd::{create_log_entry, log_entry_async};

// Test creating a log entry
#[test]
fn test_create_log_entry() {
// Arrange
let uuid = "test-uuid";
let iso = "2023-06-10T12:34:56Z";
let level = LogLevel::INFO;
let message = "Test log message";

// Act
let log_entry = create_log_entry(uuid, iso, level, message);

// Assert
assert_eq!(log_entry.session_id, uuid);
assert_eq!(log_entry.time, iso);
assert_eq!(log_entry.level.to_string(), "INFO");
Expand All @@ -25,18 +30,37 @@ mod tests {
assert_eq!(log_entry.format, LogFormat::JSON);
}

// Test logging asynchronously
#[tokio::test]
async fn test_log_entry_async() {
// Arrange
let uuid = "test-uuid";
let iso = "2023-06-10T12:34:56Z";
let level = LogLevel::INFO;
let message = "Test log message";

let log_entry = create_log_entry(uuid, iso, level, message);

// Act
let result = log_entry_async(log_entry).await;

// Assert
// Assert that the log entry was logged successfully
assert!(log_entry.session_id == "test-uuid");
assert!(log_entry.time == "2023-06-10T12:34:56Z");
assert!(log_entry.level == LogLevel::INFO);
assert!(message == "Test log message");
assert!(result.is_ok());
}

// Test UUID generation
#[test]
fn test_uuid_generation() {
// Act
// Test generating multiple UUIDs
let uuid1 = Uuid::new_v4().to_string();
let uuid2 = Uuid::new_v4().to_string();

// Assert
// Ensure UUIDs are generated successfully and are unique
assert!(!uuid1.is_empty());
assert!(!uuid2.is_empty());
assert_ne!(uuid1, uuid2);
}
}

0 comments on commit 0ad9424

Please sign in to comment.