(Almost) full test coverage
This commit is contained in:
parent
0639c5ca12
commit
544e380835
8 changed files with 417 additions and 100 deletions
|
|
@ -70,10 +70,12 @@ impl FetchedRSSChannel {
|
|||
|
||||
let rss_channel = rss::Channel::read_from(&bytestream[..])?;
|
||||
|
||||
Ok(Some(FetchedRSSChannel::parse(rss_channel)?))
|
||||
let now = Utc::now();
|
||||
|
||||
Ok(Some(FetchedRSSChannel::parse(rss_channel, now)?))
|
||||
}
|
||||
|
||||
fn parse(rss: rss::Channel) -> Result<Self> {
|
||||
fn parse(rss: rss::Channel, fetched_at: DateTime<Utc>) -> Result<Self> {
|
||||
Ok(FetchedRSSChannel {
|
||||
title: rss.title,
|
||||
link: Url::parse(&rss.link)?,
|
||||
|
|
@ -81,7 +83,73 @@ impl FetchedRSSChannel {
|
|||
|
||||
items: rss.items.into_iter().map(FetchedRSSItem::parse).collect(),
|
||||
|
||||
fetched_at: Utc::now(),
|
||||
fetched_at: fetched_at,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test_utils::{
|
||||
ITEM_TITLE, ITEM_GUID, ITEM_GUID2, ITEM_DESC, ITEM_CONT,
|
||||
CHANNEL_TITLE, CHANNEL_DESC, FEED1
|
||||
};
|
||||
use chrono::TimeZone;
|
||||
|
||||
fn create_guid(value: String) -> rss::Guid {
|
||||
rss::Guid { value, permalink: false }
|
||||
}
|
||||
|
||||
fn create_item(guid: rss::Guid) -> rss::Item {
|
||||
rss::ItemBuilder::default()
|
||||
.title(ITEM_TITLE.to_string())
|
||||
.guid(guid)
|
||||
.description(ITEM_DESC.to_string())
|
||||
.content(ITEM_CONT.to_string())
|
||||
.build()
|
||||
}
|
||||
|
||||
fn create_channel(items: Vec<rss::Item>) -> rss::Channel {
|
||||
rss::ChannelBuilder::default()
|
||||
.title(CHANNEL_TITLE.to_string())
|
||||
.description(CHANNEL_DESC.to_string())
|
||||
.link(FEED1.to_string())
|
||||
.items(items)
|
||||
.build()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_item() {
|
||||
let rss_guid = create_guid(ITEM_GUID.to_string());
|
||||
let rss_item = create_item(rss_guid);
|
||||
let item = FetchedRSSItem::parse(rss_item);
|
||||
|
||||
assert_eq!(item.guid, ITEM_GUID);
|
||||
assert_eq!(item.title, ITEM_TITLE);
|
||||
assert_eq!(item.description, ITEM_DESC);
|
||||
assert_eq!(item.content, ITEM_CONT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_feed() {
|
||||
let rss_guid = create_guid(ITEM_GUID.to_string());
|
||||
let rss_guid2 = create_guid(ITEM_GUID2.to_string());
|
||||
let rss_item = create_item(rss_guid);
|
||||
let rss_item2 = create_item(rss_guid2);
|
||||
|
||||
let rss_channel = create_channel([rss_item, rss_item2].to_vec());
|
||||
|
||||
let date: DateTime<Utc> = Utc.with_ymd_and_hms(2020,1,1,0,0,0).unwrap();
|
||||
|
||||
let channel = FetchedRSSChannel::parse(rss_channel, date).unwrap();
|
||||
|
||||
assert_eq!(channel.title, CHANNEL_TITLE);
|
||||
assert_eq!(channel.link.as_str(), FEED1);
|
||||
assert_eq!(channel.description, CHANNEL_DESC);
|
||||
assert_eq!(channel.fetched_at, date);
|
||||
assert_eq!(channel.items.len(), 2);
|
||||
assert!(channel.items.iter().any(|i| i.guid() == ITEM_GUID));
|
||||
assert!(channel.items.iter().any(|i| i.guid() == ITEM_GUID2));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue