Add tests for feed::find
This commit is contained in:
parent
50da088518
commit
d8861d6f7e
4 changed files with 37 additions and 10 deletions
|
@ -1,16 +1,16 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use crate::fetch::FetchEnv;
|
use crate::fetch::FetchGet;
|
||||||
|
|
||||||
pub fn find(site: &str, channel_name: &str, e: &FetchEnv) -> Result<String> {
|
pub fn find(site: &str, channel_name: &str, e: &FetchGet) -> Result<String> {
|
||||||
if let Some(channel_prefix) = channel_name.chars().next() {
|
if let Some(channel_prefix) = channel_name.chars().next() {
|
||||||
if channel_prefix != '@' {
|
if channel_prefix != '@' {
|
||||||
return Err(format!("Channel Name must begin with an '@': {}", channel_name).into());
|
return Err(format!("Channel Name must begin with an '@': {}", channel_name).into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let channel_url = format!("{}{}", site, channel_name);
|
let channel_url = format!("{}{}", site, channel_name);
|
||||||
let response = (e.get)(&channel_url)?;
|
let response = (e)(&channel_url)?;
|
||||||
let rss_url = scraper::Html::parse_document(&response.text()?)
|
let rss_url = scraper::Html::parse_document(&response)
|
||||||
.select(&scraper::Selector::parse("link[title='RSS']").unwrap())
|
.select(&scraper::Selector::parse("link[title='RSS']").unwrap())
|
||||||
.next()
|
.next()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -23,10 +23,37 @@ pub fn find(site: &str, channel_name: &str, e: &FetchEnv) -> Result<String> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::fetch::Response;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn finds_rss_url() -> Result<()> {
|
fn finds_rss_url() -> Result<()> {
|
||||||
// TODO: need to inject wrapper for reqwest::blocking::get
|
//given
|
||||||
|
let fetch_get = &(get as FetchGet);
|
||||||
|
//when
|
||||||
|
let result = find("site", "@channel", &fetch_get)?;
|
||||||
|
//then
|
||||||
|
assert_eq!(result, "the-rss-url");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn error_if_channel_name_is_invalid() -> Result<()> {
|
||||||
|
//given
|
||||||
|
let fetch_get = &(get as FetchGet);
|
||||||
|
//when
|
||||||
|
let result = find("site", "invalid-channel-name", &fetch_get);
|
||||||
|
//then
|
||||||
|
assert!(result.is_err());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(_url: &str) -> Result<Response> {
|
||||||
|
Ok(r#"
|
||||||
|
<html>
|
||||||
|
<link title="RSS" href="the-rss-url">
|
||||||
|
</html>
|
||||||
|
"#
|
||||||
|
.to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use crate::FetchEnv;
|
use crate::fetch::FetchGet;
|
||||||
|
|
||||||
mod find;
|
mod find;
|
||||||
mod get;
|
mod get;
|
||||||
|
@ -15,5 +15,5 @@ pub struct FeedEnv {
|
||||||
pub get: FeedGet,
|
pub get: FeedGet,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type FeedFind = fn(&str, &str, &FetchEnv) -> Result<String>;
|
pub type FeedFind = fn(&str, &str, &FetchGet) -> Result<String>;
|
||||||
pub type FeedGet = fn(&str) -> Result<Feed>;
|
pub type FeedGet = fn(&str) -> Result<Feed>;
|
||||||
|
|
|
@ -27,8 +27,8 @@ pub fn download(link: &Link) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Response = reqwest::blocking::Response;
|
pub type Response = String;
|
||||||
|
|
||||||
pub fn get(url: &str) -> Result<Response> {
|
pub fn get(url: &str) -> Result<Response> {
|
||||||
Ok(reqwest::blocking::get(url)?)
|
Ok(reqwest::blocking::get(url)?.text()?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ pub struct Env {
|
||||||
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
|
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
|
||||||
for channel_name in subscriptions::lines_from(subscriptions)? {
|
for channel_name in subscriptions::lines_from(subscriptions)? {
|
||||||
println!("Channel: {}", channel_name);
|
println!("Channel: {}", channel_name);
|
||||||
let feed_url = (e.feed.find)(site, &channel_name, &e.fetch)?;
|
let feed_url = (e.feed.find)(site, &channel_name, &e.fetch.get)?;
|
||||||
for entry in (e.feed.get)(&feed_url)?.entries() {
|
for entry in (e.feed.get)(&feed_url)?.entries() {
|
||||||
if let Some(link) = entry.links().get(0).cloned() {
|
if let Some(link) = entry.links().get(0).cloned() {
|
||||||
if !(e.history.find)(&link, history)? {
|
if !(e.history.find)(&link, history)? {
|
||||||
|
|
Loading…
Add table
Reference in a new issue