test: added more tests (#16)
Some checks failed
Some checks failed
Co-authored-by: Sharang Parnerkar <parnerkarsharang@gmail.com> Reviewed-on: #16
This commit was merged in pull request #16.
This commit is contained in:
@@ -23,3 +23,61 @@ pub struct NewsCard {
|
||||
pub thumbnail_url: Option<String>,
|
||||
pub published_at: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn news_card_serde_round_trip() {
|
||||
let card = NewsCard {
|
||||
title: "AI Breakthrough".into(),
|
||||
source: "techcrunch.com".into(),
|
||||
summary: "New model released".into(),
|
||||
content: "Full article content here".into(),
|
||||
category: "AI".into(),
|
||||
url: "https://example.com/article".into(),
|
||||
thumbnail_url: Some("https://example.com/thumb.jpg".into()),
|
||||
published_at: "2025-06-01".into(),
|
||||
};
|
||||
let json = serde_json::to_string(&card).expect("serialize NewsCard");
|
||||
let back: NewsCard = serde_json::from_str(&json).expect("deserialize NewsCard");
|
||||
assert_eq!(card, back);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn news_card_thumbnail_none() {
|
||||
let card = NewsCard {
|
||||
title: "No Thumb".into(),
|
||||
source: "bbc.com".into(),
|
||||
summary: "Summary".into(),
|
||||
content: "Content".into(),
|
||||
category: "Tech".into(),
|
||||
url: "https://bbc.com/article".into(),
|
||||
thumbnail_url: None,
|
||||
published_at: "2025-06-01".into(),
|
||||
};
|
||||
let json = serde_json::to_string(&card).expect("serialize");
|
||||
let back: NewsCard = serde_json::from_str(&json).expect("deserialize");
|
||||
assert_eq!(card, back);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn news_card_thumbnail_some() {
|
||||
let card = NewsCard {
|
||||
title: "With Thumb".into(),
|
||||
source: "cnn.com".into(),
|
||||
summary: "Summary".into(),
|
||||
content: "Content".into(),
|
||||
category: "News".into(),
|
||||
url: "https://cnn.com/article".into(),
|
||||
thumbnail_url: Some("https://cnn.com/img.jpg".into()),
|
||||
published_at: "2025-06-01".into(),
|
||||
};
|
||||
let json = serde_json::to_string(&card).expect("serialize");
|
||||
assert!(json.contains("img.jpg"));
|
||||
let back: NewsCard = serde_json::from_str(&json).expect("deserialize");
|
||||
assert_eq!(card.thumbnail_url, back.thumbnail_url);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user