i5-add-tests (part 2) #7

Merged
kemitix merged 10 commits from i5-add-tests into main 2023-07-28 18:35:41 +01:00
4 changed files with 37 additions and 10 deletions
Showing only changes of commit d8861d6f7e - Show all commits

View file

@ -1,16 +1,16 @@
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 channel_prefix != '@' {
return Err(format!("Channel Name must begin with an '@': {}", channel_name).into());
}
}
let channel_url = format!("{}{}", site, channel_name);
let response = (e.get)(&channel_url)?;
let rss_url = scraper::Html::parse_document(&response.text()?)
let response = (e)(&channel_url)?;
let rss_url = scraper::Html::parse_document(&response)
.select(&scraper::Selector::parse("link[title='RSS']").unwrap())
.next()
.unwrap()
@ -23,10 +23,37 @@ pub fn find(site: &str, channel_name: &str, e: &FetchEnv) -> Result<String> {
#[cfg(test)]
mod tests {
use crate::fetch::Response;
use super::*;
#[test]
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(())
}
#[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())
}
}

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
use crate::FetchEnv;
use crate::fetch::FetchGet;
mod find;
mod get;
@ -15,5 +15,5 @@ pub struct FeedEnv {
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>;

View file

@ -27,8 +27,8 @@ pub fn download(link: &Link) -> Result<()> {
Ok(())
}
pub type Response = reqwest::blocking::Response;
pub type Response = String;
pub fn get(url: &str) -> Result<Response> {
Ok(reqwest::blocking::get(url)?)
Ok(reqwest::blocking::get(url)?.text()?)
}

View file

@ -19,7 +19,7 @@ pub struct Env {
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
for channel_name in subscriptions::lines_from(subscriptions)? {
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() {
if let Some(link) = entry.links().get(0).cloned() {
if !(e.history.find)(&link, history)? {