id3-image-rs/tests/test_processing.rs

258 lines
6.3 KiB
Rust
Raw Normal View History

2019-02-17 14:11:07 +00:00
use std::env;
use std::fs;
use std::ops::Deref;
2022-03-22 16:44:13 +00:00
use std::path::{Path, PathBuf};
2019-02-17 14:11:07 +00:00
use tempfile::TempDir;
2022-03-22 16:44:13 +00:00
2022-03-22 16:56:46 +00:00
use id3_image_rs::*;
2019-02-17 14:11:07 +00:00
struct Fixture {
2019-02-24 14:20:38 +00:00
path: PathBuf,
source: PathBuf,
_tempdir: TempDir,
}
2019-02-24 14:08:12 +00:00
impl Fixture {
2019-02-24 14:20:38 +00:00
fn blank(fixture_filename: &str) -> Self {
2019-02-24 14:08:12 +00:00
let root_dir = &env::var("CARGO_MANIFEST_DIR").expect("$CARGO_MANIFEST_DIR");
2019-02-24 14:20:38 +00:00
let mut source = PathBuf::from(root_dir);
source.push("tests/fixtures");
source.push(&fixture_filename);
2019-02-24 14:08:12 +00:00
let tempdir = tempfile::tempdir().unwrap();
2019-02-24 14:20:38 +00:00
let mut path = PathBuf::from(&tempdir.path());
path.push(&fixture_filename);
2019-02-24 14:08:12 +00:00
2022-03-22 16:50:38 +00:00
Fixture {
_tempdir: tempdir,
source,
path,
}
2019-02-24 14:20:38 +00:00
}
2019-02-24 14:08:12 +00:00
2019-02-24 14:20:38 +00:00
fn copy(fixture_filename: &str) -> Self {
let fixture = Fixture::blank(fixture_filename);
fs::copy(&fixture.source, &fixture.path).unwrap();
fixture
}
}
impl Deref for Fixture {
type Target = Path;
fn deref(&self) -> &Self::Target {
2019-02-24 14:20:38 +00:00
self.path.deref()
2019-02-17 14:11:07 +00:00
}
}
fn read_tag(path: &Path) -> id3::Tag {
id3::Tag::read_from_path(path).unwrap()
}
#[test]
fn test_unsuccessful_image_embedding() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1_no_image.mp3");
2019-02-24 14:20:38 +00:00
let image = Fixture::copy("attempt_1.jpg");
// Nonexistent files
assert!(embed_image(&song, &PathBuf::from("nonexistent.jpg")).is_err());
assert!(embed_image(&PathBuf::from("nonexistent.mp3"), &image).is_err());
2022-03-22 16:50:38 +00:00
assert!(embed_image(
&PathBuf::from("nonexistent.mp3"),
&PathBuf::from("nonexistent.jpg")
)
.is_err());
// Wrong kinds of files
assert!(embed_image(&image, &song).is_err());
assert!(embed_image(&song, &song).is_err());
assert!(embed_image(&image, &image).is_err());
}
2019-02-17 14:11:07 +00:00
#[test]
fn test_successful_jpeg_image_embedding() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1_no_image.mp3");
2019-02-24 14:20:38 +00:00
let image = Fixture::copy("attempt_1.jpg");
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
2022-03-22 16:44:13 +00:00
assert_eq!(tag.pictures().count(), 0);
2019-02-17 14:11:07 +00:00
embed_image(&song, &image).unwrap();
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
}
2021-02-22 19:56:59 +00:00
#[test]
fn test_successful_jpeg_image_embedding_with_a_broken_file() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1_broken.mp3");
2021-02-22 19:56:59 +00:00
let image = Fixture::copy("attempt_1.jpg");
embed_image(&song, &image).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
}
2019-02-17 14:11:07 +00:00
#[test]
fn test_successful_png_image_embedding() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1_no_image.mp3");
2019-02-24 14:20:38 +00:00
let image = Fixture::copy("attempt_1.png");
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
2022-03-22 16:44:13 +00:00
assert_eq!(tag.pictures().count(), 0);
2019-02-17 14:11:07 +00:00
embed_image(&song, &image).unwrap();
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
}
2022-03-22 16:44:13 +00:00
#[test]
fn test_successful_png_image_embedding_from_memory() {
let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::copy("attempt_1.png");
let tag = read_tag(&song);
assert_eq!(tag.pictures().count(), 0);
embed_image_from_memory(&song, &image::open(&*image).unwrap()).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
}
#[test]
fn test_successful_png_image_embedding_and_extracting() {
let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::copy("attempt_1.png");
let tag = read_tag(&song);
assert_eq!(tag.pictures().count(), 0);
extract_first_image_as_img(&song).unwrap_err();
embed_image(&song, &image).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
extract_first_image_as_img(&song).unwrap();
}
2019-02-17 14:11:07 +00:00
#[test]
fn test_successful_image_embedding_in_a_file_that_already_has_an_image() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1.mp3");
2019-02-24 14:20:38 +00:00
let image = Fixture::copy("attempt_1.jpg");
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
embed_image(&song, &image).unwrap();
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
}
#[test]
2019-02-24 14:20:38 +00:00
fn test_removing_and_adding_an_image() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1.mp3");
2019-02-24 14:20:38 +00:00
let image = Fixture::copy("attempt_1.jpg");
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
remove_images(&song).unwrap();
let tag = read_tag(&song);
2022-03-22 16:44:13 +00:00
assert_eq!(tag.pictures().count(), 0);
embed_image(&song, &image).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
}
2019-02-24 14:20:38 +00:00
2021-02-22 19:56:59 +00:00
#[test]
fn test_removing_and_adding_an_image_to_a_broken_file() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1_broken.mp3");
2021-02-22 19:56:59 +00:00
let image = Fixture::copy("attempt_1.jpg");
remove_images(&song).unwrap();
let tag = read_tag(&song);
2022-03-22 16:44:13 +00:00
assert_eq!(tag.pictures().count(), 0);
2021-02-22 19:56:59 +00:00
embed_image(&song, &image).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
}
2019-02-24 14:20:38 +00:00
#[test]
fn test_extracting_a_jpg_image() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1.mp3");
2019-02-24 14:20:38 +00:00
let image = Fixture::blank("attempt_1.jpg");
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
assert!(!image.exists());
extract_first_image(&song, &image).unwrap();
2021-02-22 19:56:59 +00:00
assert!(image.exists());
}
#[test]
fn test_extracting_a_jpg_image_from_a_broken_file() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1_broken.mp3");
2021-02-22 19:56:59 +00:00
let image = Fixture::blank("attempt_1.jpg");
extract_first_image(&song, &image).unwrap();
2019-02-24 14:20:38 +00:00
assert!(image.exists());
}
#[test]
fn test_extracting_a_png_image() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1.mp3");
2019-02-24 14:20:38 +00:00
let image = Fixture::blank("attempt_1.png");
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
assert!(!image.exists());
extract_first_image(&song, &image).unwrap();
assert!(image.exists());
}
#[test]
fn test_overwriting_an_existing_image() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1.mp3");
2019-02-24 14:20:38 +00:00
let image = Fixture::copy("attempt_1.jpg");
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
assert!(image.exists());
extract_first_image(&song, &image).unwrap();
assert!(image.exists());
}
#[test]
fn test_extracting_an_image_with_no_pictures() {
2022-03-22 16:44:13 +00:00
let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::blank("attempt_1.jpg");
let tag = read_tag(&song);
2022-03-22 16:44:13 +00:00
assert_eq!(tag.pictures().count(), 0);
assert!(!image.exists());
assert!(extract_first_image(&song, &image).is_err());
assert!(!image.exists());
}