Commit 269800bf authored by Jordan Williams's avatar Jordan Williams Committed by Jordan Petridis
Browse files

Add initial test case to verify a cached image is refreshed

The truncate_db function has been made public to improve this test case.
This is not yet working and there will need to be more cases.
The source id is not found which causes the test to crash.
parent 7b46397d
......@@ -82,7 +82,6 @@ fn run_migration_on(connection: &SqliteConnection) -> Result<(), DataError> {
/// Reset the database into a clean state.
// Test share a Temp file db.
#[cfg(test)]
pub fn truncate_db() -> Result<(), DataError> {
let db = connection();
let con = db.get()?;
......
......@@ -271,11 +271,9 @@ pub fn cache_image(pd: &ShowCoverModel) -> Result<String, DownloadError> {
#[cfg(test)]
mod tests {
use super::*;
use crate::dbqueries;
use crate::pipeline::pipeline;
use crate::Source;
use crate::{dbqueries, Source};
use anyhow::Result;
use std::fs;
#[test]
......
......@@ -188,7 +188,7 @@ impl NewShow {
}
#[allow(dead_code)]
pub(crate) fn image_uri_hash(&self) -> Option<i64> {
pub fn image_uri_hash(&self) -> Option<i64> {
self.image_uri_hash
}
......
......@@ -207,9 +207,7 @@ mod tests {
use super::*;
use crate::database::truncate_db;
use crate::dbqueries;
use crate::models::NewShowBuilder;
use crate::models::Update;
use crate::models::{Insert, NewShow};
use crate::models::{Insert, NewShow, NewShowBuilder, Update};
use anyhow::Result;
use std::{thread, time};
......@@ -240,10 +238,7 @@ mod tests {
.unwrap()
};
static ref UPDATED_IMAGE_URI_INTERCEPTED: NewShow = {
let image_uri =
"http://static.megaphone.fm/podcasts/d5735a50-d904-11e6-8532-73c7de466ea6/image/\
uploads_2F1484252190700-qhn5krasklbce3dh-a797539282700ea0298a3a26f7e49b0b_\
2FIntercepted_COVER%2B_281_30.png";
let image_uri = "https://assets.fireside.fm/file/fireside-images/podcasts/images/f/f31a453c-fa15-491f-8618-3f71f1d565e5/cover.jpg?v=3";
NewShowBuilder::default()
.title("Intercepted with Jeremy Scahill")
......@@ -318,9 +313,7 @@ mod tests {
);
assert_eq!(
show.image_uri().unwrap(),
"http://static.megaphone.fm/podcasts/d5735a50-d904-11e6-8532-73c7de466ea6/image/\
uploads_2F1484252190700-qhn5krasklbce3dh-a797539282700ea0298a3a26f7e49b0b_\
2FIntercepted_COVER%2B_281_30.png"
"https://assets.fireside.fm/file/fireside-images/podcasts/images/f/f31a453c-fa15-491f-8618-3f71f1d565e5/cover.jpg?v=3"
);
assert_eq!(show.source_id(), 42);
Ok(())
......@@ -388,9 +381,7 @@ mod tests {
);
assert_eq!(
show.image_uri().unwrap(),
"http://static.megaphone.fm/podcasts/d5735a50-d904-11e6-8532-73c7de466ea6/image/\
uploads_2F1484252190700-qhn5krasklbce3dh-a797539282700ea0298a3a26f7e49b0b_\
2FIntercepted_COVER%2B_281_30.png"
"https://assets.fireside.fm/file/fireside-images/podcasts/images/f/f31a453c-fa15-491f-8618-3f71f1d565e5/cover.jpg?v=3"
);
assert_eq!(show.source_id(), 42);
Ok(())
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -563,6 +563,13 @@ pub(crate) fn on_export_clicked(window: &gtk::ApplicationWindow, sender: &Sender
mod tests {
use super::*;
use anyhow::Result;
use podcasts_data::database::truncate_db;
use podcasts_data::dbqueries;
use podcasts_data::pipeline::pipeline;
use podcasts_data::utils::get_download_folder;
use podcasts_data::{Save, Source};
use std::fs;
use std::path::PathBuf;
// use podcasts_data::Source;
// use podcasts_data::dbqueries;
......@@ -632,4 +639,75 @@ mod tests {
assert!(soundcloud_to_rss(&soundcloud_url).await.is_err());
Ok(())
}
#[test]
#[ignore]
fn should_refresh_cached_image_when_the_image_uri_changes() -> Result<()> {
truncate_db()?;
let mut original_feed = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
original_feed.push("resources/test/feeds/2018-01-20-LinuxUnplugged.xml");
let original_url = format!(
"{}{}",
"file:/",
fs::canonicalize(original_feed)?.to_str().unwrap()
);
println!("Made it here! (1)");
let mut source = Source::from_url(&original_url)?;
println!("Made it here! (2)");
source.set_http_etag(None);
source.set_last_modified(None);
let sid = source.save()?.id();
println!("Made it here! (3)");
let mut rt = tokio::runtime::Runtime::new()?;
rt.block_on(pipeline(vec![source]));
println!("Made it here! (4)");
println!("The source id is {}!", sid);
dbqueries::get_sources().unwrap().iter().for_each(|s| {
println!("{}:{}", s.id(), s.uri());
});
let original = dbqueries::get_podcast_from_source_id(sid)?;
println!("Made it here! (5)");
let original_image_uri = original.image_uri();
let original_image_uri_hash = original.image_uri_hash();
let original_image_cached = original.image_cached();
let download_folder = get_download_folder(&original.title())?;
let image_path = download_folder + "/cover.jpeg";
let original_image_file_size = fs::metadata(&image_path)?.len(); // 693,343
println!("Made it here! (6)");
// Update the URI and refresh the feed
let mut new_feed = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
new_feed.push("resources/test/feeds/2020-12-19-LinuxUnplugged.xml");
let mut source = dbqueries::get_source_from_id(sid)?;
let new_url = format!(
"{}{}",
"file:/",
fs::canonicalize(new_feed)?.to_str().unwrap()
);
source.set_uri(new_url);
source.set_http_etag(None);
source.set_last_modified(None);
source.save()?;
println!("Made it here! (7)");
let mut rt = tokio::runtime::Runtime::new()?;
rt.block_on(pipeline(vec![source]));
println!("Made it here! (8)");
let new = dbqueries::get_podcast_from_source_id(sid)?;
let new_image_uri = new.image_uri();
let new_image_uri_hash = new.image_uri_hash();
let new_image_cached = new.image_cached();
let new_image_file_size = fs::metadata(&image_path)?.len();
println!("Made it here! (9)");
assert_eq!(original.title(), new.title());
assert_ne!(original_image_uri, new_image_uri);
assert_ne!(original_image_uri_hash, new_image_uri_hash);
assert_ne!(original_image_cached, new_image_cached);
assert_ne!(original_image_file_size, new_image_file_size);
fs::remove_file(image_path)?;
Ok(())
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment