Skip to content

Commit

Permalink
test: add unit tests for entire project
Browse files Browse the repository at this point in the history
- Ensured comprehensive coverage for various functionalities.
- Verified correctness and robustness of the codebase.
  • Loading branch information
AndrielFR committed Jan 4, 2025
1 parent a6694cf commit b18f637
Show file tree
Hide file tree
Showing 11 changed files with 763 additions and 6 deletions.
42 changes: 42 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,45 @@ enum Action {
/// Search for media.
Search,
}

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

#[test]
fn test_with_timeout() {
let client = Client::with_timeout(30);

assert_eq!(client.timeout, 30);
assert!(client.api_token.is_none());
}

#[test]
fn test_with_token() {
let client = Client::with_token("test_token");

assert_eq!(client.timeout, 20);
assert_eq!(client.api_token, Some("test_token".to_string()));
}

#[test]
fn test_timeout() {
let client = Client::with_timeout(30).timeout(60);

assert_eq!(client.timeout, 60);
}

#[test]
fn test_token() {
let client = Client::with_timeout(30).token("new_token");

assert_eq!(client.api_token, Some("new_token".to_string()));
}

#[test]
fn test_get_query() {
let query = Client::get_query(MediaType::Anime, Action::Get).unwrap();

assert!(query.contains("query"));
}
}
54 changes: 54 additions & 0 deletions src/models/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,57 @@ impl std::fmt::Display for Color {
}
}
}

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

#[test]
fn test_hex_with_hex_color() {
let color = Color::Hex("#FF5733".to_string());

assert_eq!(color.hex(), Some("#FF5733"));
}

#[test]
fn test_hex_with_predefined_color() {
let color = Color::Blue;

assert_eq!(color.hex(), None);
}

#[test]
fn test_from_str_predefined_colors() {
assert_eq!(Color::from("blue"), Color::Blue);
assert_eq!(Color::from("PURPLE"), Color::Purple);
assert_eq!(Color::from("Pink"), Color::Pink);
assert_eq!(Color::from("orange"), Color::Orange);
assert_eq!(Color::from("RED"), Color::Red);
assert_eq!(Color::from("green"), Color::Green);
assert_eq!(Color::from("gray"), Color::Gray);
}

#[test]
fn test_from_str_hex_color() {
assert_eq!(Color::from("#FF5733"), Color::Hex("#FF5733".to_string()));
}

#[test]
fn test_from_string_predefined_colors() {
assert_eq!(Color::from("blue".to_string()), Color::Blue);
assert_eq!(Color::from("PURPLE".to_string()), Color::Purple);
assert_eq!(Color::from("Pink".to_string()), Color::Pink);
assert_eq!(Color::from("orange".to_string()), Color::Orange);
assert_eq!(Color::from("RED".to_string()), Color::Red);
assert_eq!(Color::from("green".to_string()), Color::Green);
assert_eq!(Color::from("gray".to_string()), Color::Gray);
}

#[test]
fn test_from_string_hex_color() {
assert_eq!(
Color::from("#FF5733".to_string()),
Color::Hex("#FF5733".to_string())
);
}
}
53 changes: 53 additions & 0 deletions src/models/cover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,56 @@ impl Cover {
}
}
}

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

#[test]
fn test_largest_with_extra_large() {
let cover = Cover {
extra_large: Some("https://example.com/extra_large.jpg".to_string()),
large: Some("https://example.com/large.jpg".to_string()),
medium: Some("https://example.com/medium.jpg".to_string()),
color: None,
};

assert_eq!(cover.largest(), Some("https://example.com/extra_large.jpg"));
}

#[test]
fn test_largest_with_large() {
let cover = Cover {
extra_large: None,
large: Some("https://example.com/large.jpg".to_string()),
medium: Some("https://example.com/medium.jpg".to_string()),
color: None,
};

assert_eq!(cover.largest(), Some("https://example.com/large.jpg"));
}

#[test]
fn test_largest_with_medium() {
let cover = Cover {
extra_large: None,
large: None,
medium: Some("https://example.com/medium.jpg".to_string()),
color: None,
};

assert_eq!(cover.largest(), Some("https://example.com/medium.jpg"));
}

#[test]
fn test_largest_with_none() {
let cover = Cover {
extra_large: None,
large: None,
medium: None,
color: None,
};

assert_eq!(cover.largest(), None);
}
}
94 changes: 93 additions & 1 deletion src/models/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//! This module contains the `Date` struct.
use chrono::{Datelike, NaiveDate};
use chrono::{Datelike, Local, NaiveDate};
use serde::{Deserialize, Serialize};

/// Represents a date.
Expand All @@ -24,6 +24,17 @@ impl Date {
Self { year, month, day }
}

/// Creates a new date from the current date.
pub fn now() -> Self {
let now = Local::now().naive_local().date();

Self {
year: Some(now.year()),
month: Some(now.month()),
day: Some(now.day()),
}
}

/// Returns the year of the date.
pub fn year(&self) -> Option<i32> {
self.year
Expand Down Expand Up @@ -141,3 +152,84 @@ impl std::fmt::Display for Date {
write!(f, "{}", self.as_string())
}
}

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

#[test]
fn test_new() {
let date = Date::new(Some(2023), Some(10), Some(5));

assert_eq!(date.year(), Some(2023));
assert_eq!(date.month(), Some(10));
assert_eq!(date.day(), Some(5));
}

#[test]
fn test_now() {
let date = Date::now();
let now = Local::now().naive_local().date();

assert_eq!(date.year(), Some(now.year()));
assert_eq!(date.month(), Some(now.month()));
assert_eq!(date.day(), Some(now.day()));
}

#[test]
fn test_year() {
let date = Date::new(Some(2023), None, None);

assert_eq!(date.year(), Some(2023));
}

#[test]
fn test_month() {
let date = Date::new(None, Some(10), None);

assert_eq!(date.month(), Some(10));
}

#[test]
fn test_day() {
let date = Date::new(None, None, Some(5));

assert_eq!(date.day(), Some(5));
}

#[test]
fn test_format() {
let date = Date::new(Some(2023), Some(10), Some(5));
let formatted = date.format("{yyyy}-{mm}-{dd}");

assert_eq!(formatted, "2023-10-05");
}

#[test]
fn test_as_date() {
let date = Date::new(Some(2023), Some(10), Some(5));
let naive_date = date.as_date();

assert_eq!(naive_date.year(), 2023);
assert_eq!(naive_date.month(), 10);
assert_eq!(naive_date.day(), 5);
}

#[test]
fn test_as_string() {
let date = Date::new(Some(2023), Some(10), Some(5));
let date_string = date.as_string();

assert_eq!(date_string, "2023-10-05");
}

#[test]
fn test_is_valid() {
let valid_date = Date::new(Some(2023), Some(10), Some(5));
let invalid_date = Date::new(Some(2023), None, Some(5));

assert!(valid_date.is_valid());
assert!(!invalid_date.is_valid());
}
}
49 changes: 49 additions & 0 deletions src/models/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,52 @@ impl std::fmt::Display for Format {
write!(f, "{}", self.name())
}
}

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

#[test]
fn test_format_name() {
assert_eq!(Format::Tv.name(), "TV");
assert_eq!(Format::TvShort.name(), "TV Short");
assert_eq!(Format::Movie.name(), "Movie");
assert_eq!(Format::Special.name(), "Special");
assert_eq!(Format::Ova.name(), "OVA");
assert_eq!(Format::Ona.name(), "ONA");
assert_eq!(Format::Music.name(), "Music");
assert_eq!(Format::Manga.name(), "Manga");
assert_eq!(Format::Novel.name(), "Novel");
assert_eq!(Format::OneShot.name(), "One-Shot");
}

#[test]
fn test_from_str() {
assert_eq!(Format::from("tv"), Format::Tv);
assert_eq!(Format::from("TV_SHORT"), Format::TvShort);
assert_eq!(Format::from("movie"), Format::Movie);
assert_eq!(Format::from("SPECIAL"), Format::Special);
assert_eq!(Format::from("ova"), Format::Ova);
assert_eq!(Format::from("ONA"), Format::Ona);
assert_eq!(Format::from("music"), Format::Music);
assert_eq!(Format::from("MANGA"), Format::Manga);
assert_eq!(Format::from("novel"), Format::Novel);
assert_eq!(Format::from("ONE_SHOT"), Format::OneShot);
assert_eq!(Format::from("unknown"), Format::Tv); // Default case
}

#[test]
fn test_from_string() {
assert_eq!(Format::from("tv".to_string()), Format::Tv);
assert_eq!(Format::from("TV_SHORT".to_string()), Format::TvShort);
assert_eq!(Format::from("movie".to_string()), Format::Movie);
assert_eq!(Format::from("SPECIAL".to_string()), Format::Special);
assert_eq!(Format::from("ova".to_string()), Format::Ova);
assert_eq!(Format::from("ONA".to_string()), Format::Ona);
assert_eq!(Format::from("music".to_string()), Format::Music);
assert_eq!(Format::from("MANGA".to_string()), Format::Manga);
assert_eq!(Format::from("novel".to_string()), Format::Novel);
assert_eq!(Format::from("ONE_SHOT".to_string()), Format::OneShot);
assert_eq!(Format::from("unknown".to_string()), Format::Tv); // Default case
}
}
35 changes: 35 additions & 0 deletions src/models/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,38 @@ impl Image {
}
}
}

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

#[test]
fn test_largest_with_large() {
let image = Image {
large: "https://example.com/large.jpg".to_string(),
medium: "https://example.com/medium.jpg".to_string(),
};

assert_eq!(image.largest(), "https://example.com/large.jpg");
}

#[test]
fn test_largest_with_empty_large() {
let image = Image {
large: "".to_string(),
medium: "https://example.com/medium.jpg".to_string(),
};

assert_eq!(image.largest(), "https://example.com/medium.jpg");
}

#[test]
fn test_largest_with_empty_large_and_medium() {
let image = Image {
large: "".to_string(),
medium: "".to_string(),
};

assert_eq!(image.largest(), "");
}
}
Loading

0 comments on commit b18f637

Please sign in to comment.