i5-add-tests (part 4) #10

Merged
kemitix merged 39 commits from i5-add-tests into main 2023-08-05 06:50:01 +01:00
5 changed files with 60 additions and 30 deletions
Showing only changes of commit e54c136f5a - Show all commits

View file

@ -23,16 +23,18 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::errors::Error; use crate::test_utils::{
mock_fetch_as_text_with_rss_url, stub_download_as_mp3, stub_fetch_as_bytes,
};
use super::*; use super::*;
#[test] #[test]
fn finds_rss_url() -> Result<()> { fn finds_rss_url() -> Result<()> {
//given //given
let network_env = NetworkEnv { let network_env = NetworkEnv {
fetch_as_text: dummy_fetch_as_text, fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
fetch_as_bytes: dummy_fetch_as_bytes, fetch_as_bytes: stub_fetch_as_bytes(),
download_as_mp3: dummy_download_as_mp3, download_as_mp3: stub_download_as_mp3(),
}; };
//when //when
let result = find("site", "@channel", &network_env)?; let result = find("site", "@channel", &network_env)?;
@ -45,9 +47,9 @@ mod tests {
fn error_if_channel_name_is_invalid() -> Result<()> { fn error_if_channel_name_is_invalid() -> Result<()> {
//given //given
let network_env = NetworkEnv { let network_env = NetworkEnv {
fetch_as_text: dummy_fetch_as_text, fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
fetch_as_bytes: dummy_fetch_as_bytes, fetch_as_bytes: stub_fetch_as_bytes(),
download_as_mp3: dummy_download_as_mp3, download_as_mp3: stub_download_as_mp3(),
}; };
//when //when
let result = find("site", "invalid-channel-name", &network_env); let result = find("site", "invalid-channel-name", &network_env);
@ -55,19 +57,4 @@ mod tests {
assert!(result.is_err()); assert!(result.is_err());
Ok(()) Ok(())
} }
fn dummy_fetch_as_text(_url: &str) -> Result<String> {
Ok(r#"
<html>
<link title="RSS" href="the-rss-url">
</html>
"#
.to_string())
}
fn dummy_fetch_as_bytes(_url: &str) -> Result<bytes::Bytes> {
Err(Error::message("Not implemented"))
}
fn dummy_download_as_mp3(_url: &str) -> Result<()> {
Err(Error::message("Not implemented"))
}
} }

View file

@ -34,3 +34,15 @@ pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()>
} }
Ok(()) Ok(())
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn downloads_two_items_from_two_feeds_ignoring_existing_items_in_history() -> Result<()> {
//given
//when
//then
Ok(())
}
}

View file

@ -2,17 +2,23 @@ use std::process::Command;
use crate::prelude::*; use crate::prelude::*;
pub type NetworkFetchAsTextFn = Box<dyn Fn(&str) -> Result<String>>;
pub type NetworkFetchAsBytesFn = Box<dyn Fn(&str) -> Result<bytes::Bytes>>;
pub type NetworkDownloadAsMp3Fn = Box<dyn Fn(&str) -> Result<()>>;
pub struct NetworkEnv { pub struct NetworkEnv {
pub fetch_as_text: fn(url: &str) -> Result<String>, pub fetch_as_text: NetworkFetchAsTextFn,
pub fetch_as_bytes: fn(url: &str) -> Result<bytes::Bytes>, pub fetch_as_bytes: NetworkFetchAsBytesFn,
pub download_as_mp3: fn(url: &str) -> Result<()>, pub download_as_mp3: NetworkDownloadAsMp3Fn,
} }
impl Default for NetworkEnv { impl Default for NetworkEnv {
fn default() -> Self { fn default() -> Self {
Self { Self {
fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?), fetch_as_text: Box::new(|url| Ok(reqwest::blocking::get(url)?.text()?)),
fetch_as_bytes: |url| Ok(reqwest::blocking::get(url)?.bytes()?), fetch_as_bytes: Box::new(|url| Ok(reqwest::blocking::get(url)?.bytes()?)),
download_as_mp3: |url| { download_as_mp3: Box::new(|url| {
let cmd = "yt-dlp"; let cmd = "yt-dlp";
// println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href); // println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href);
let output = Command::new(cmd) let output = Command::new(cmd)
@ -26,7 +32,7 @@ impl Default for NetworkEnv {
println!("{}", String::from_utf8(output.stdout)?); println!("{}", String::from_utf8(output.stdout)?);
} }
Ok(()) Ok(())
}, }),
} }
} }
} }

View file

@ -1,3 +1,6 @@
mod env; mod env;
pub use env::NetworkDownloadAsMp3Fn;
pub use env::NetworkEnv; pub use env::NetworkEnv;
pub use env::NetworkFetchAsBytesFn;
pub use env::NetworkFetchAsTextFn;

View file

@ -6,7 +6,11 @@ use std::{
use tempfile::{tempdir, TempDir}; use tempfile::{tempdir, TempDir};
use crate::prelude::*; use crate::{
errors::Error,
network::{NetworkDownloadAsMp3Fn, NetworkFetchAsBytesFn, NetworkFetchAsTextFn},
prelude::*,
};
pub fn create_text_file(name: &str, data: &[u8]) -> Result<(TempDir, String)> { pub fn create_text_file(name: &str, data: &[u8]) -> Result<(TempDir, String)> {
let data = from_utf8(data)?; let data = from_utf8(data)?;
@ -23,3 +27,21 @@ pub fn read_text_file(file_name: &str) -> Result<Vec<String>> {
.map(String::from) .map(String::from)
.collect()) .collect())
} }
pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextFn {
Box::new(move |_url: &str| {
Ok(format!(
r#"
<html>
<link title="RSS" href="{}">
</html>
"#,
url
))
})
}
pub fn stub_fetch_as_bytes() -> NetworkFetchAsBytesFn {
Box::new(|_url: &str| Err(Error::message("Not implemented")))
}
pub fn stub_download_as_mp3() -> NetworkDownloadAsMp3Fn {
Box::new(|_url: &str| Err(Error::message("Not implemented")))
}