use std::collections::HashSet;
use wiserone::quotes::{Quotes, Quote};
#[test]
fn test_quote_struct() {
let quote = Quote {
quote_text: "Test quote".to_string(),
author: "Test author".to_string(),
date_added: "2024-01-21".to_string(),
image_url: "http://example.com/image.jpg".to_string(),
};
assert_eq!(quote.quote_text, "Test quote");
assert_eq!(quote.author, "Test author");
assert_eq!(quote.date_added, "2024-01-21");
assert_eq!(quote.image_url, "http://example.com/image.jpg");
}
#[test]
fn test_quotes_struct_initialization() {
let quotes_vec = vec![
Quote {
quote_text: "Quote 1".to_string(),
author: "Author 1".to_string(),
date_added: "2024-01-21".to_string(),
image_url: "http://example.com/image1.jpg".to_string(),
},
Quote {
quote_text: "Quote 2".to_string(),
author: "Author 2".to_string(),
date_added: "2024-01-22".to_string(),
image_url: "http://example.com/image2.jpg".to_string(),
},
];
let quotes = Quotes::new(quotes_vec.clone());
assert_eq!(quotes.quotes, quotes_vec);
}
#[test]
fn test_random_quote_selection() {
let quotes_vec = vec![
Quote {
quote_text: "Test quote 1".to_string(),
author: "Author 1".to_string(),
date_added: "2024-01-01".to_string(),
image_url: "http://example.com/image1.jpg".to_string(),
},
];
let mut quotes = Quotes::new(quotes_vec);
let selected_quote_text = match quotes.select_random_quote() {
Ok(quote) => quote.quote_text.clone(),
Err(e) => panic!("Failed to select a random quote: {}", e),
};
let quotes_texts: HashSet<_> = quotes.quotes.iter().map(|q| &q.quote_text).collect();
assert!(quotes_texts.contains(&selected_quote_text));
}
#[test]
fn test_all_quotes_selection() {
let quotes_vec = vec![
Quote {
quote_text: "Test quote 1".to_string(),
author: "Author 1".to_string(),
date_added: "2024-01-01".to_string(),
image_url: "http://example.com/image1.jpg".to_string(),
},
Quote {
quote_text: "Test quote 2".to_string(),
author: "Author 2".to_string(),
date_added: "2024-01-02".to_string(),
image_url: "http://example.com/image2.jpg".to_string(),
},
];
let quotes = Quotes::new(quotes_vec.clone());
let all_quotes = match quotes.select_all_quotes() {
Ok(quotes) => quotes,
Err(e) => panic!("Failed to select all quotes: {}", e),
};
assert_eq!(all_quotes.len(), quotes_vec.len());
assert_eq!(all_quotes[0].quote_text, quotes_vec[0].quote_text);
assert_eq!(all_quotes[1].quote_text, quotes_vec[1].quote_text);
assert_eq!(all_quotes[0].author, quotes_vec[0].author);
assert_eq!(all_quotes[1].author, quotes_vec[1].author);
assert_eq!(all_quotes[0].date_added, quotes_vec[0].date_added);
assert_eq!(all_quotes[1].date_added, quotes_vec[1].date_added);
assert_eq!(all_quotes[0].image_url, quotes_vec[0].image_url);
assert_eq!(all_quotes[1].image_url, quotes_vec[1].image_url);
}
#[test]
fn test_quote_equality() {
let quote1 = Quote {
quote_text: "Same quote".to_string(),
author: "Same author".to_string(),
date_added: "2024-01-21".to_string(),
image_url: "http://example.com/image.jpg".to_string(),
};
let quote2 = quote1.clone();
assert_eq!(quote1, quote2);
let quote3 = Quote {
quote_text: "Different quote".to_string(),
author: "Same author".to_string(),
date_added: "2024-01-21".to_string(),
image_url: "http://example.com/image.jpg".to_string(),
};
assert_ne!(quote1, quote3);
}
#[test]
fn test_select_random_quote_empty_vector() {
let mut quotes = Quotes::new(Vec::new());
assert!(quotes.select_random_quote().is_err());
}
#[test]
fn test_select_all_quotes_empty_vector() {
let quotes = Quotes::new(Vec::new());
match quotes.select_all_quotes() {
Ok(_) => panic!("Expected an error for empty quotes vector, but got Ok"),
Err(e) => assert_eq!(e.to_string(), "No available quotes"),
}
}
#[test]
fn test_quote_serialization_deserialization() {
let quote = Quote {
quote_text: "Test quote".to_string(),
author: "Test author".to_string(),
date_added: "2024-01-21".to_string(),
image_url: "http://example.com/image.jpg".to_string(),
};
let serialized = serde_json::to_string("e).unwrap();
let deserialized: Quote = serde_json::from_str(&serialized).unwrap();
assert_eq!(quote, deserialized);
}
#[test]
fn test_invalid_quote_creation() {
let quote = Quote {
quote_text: "".to_string(),
author: "".to_string(),
date_added: "2024-01-21".to_string(),
image_url: "http://example.com/image.jpg".to_string(),
};
assert_eq!(quote.quote_text, "");
assert_eq!(quote.author, "");
}