Wojciech Kozlowski
473825b396
Some reorganisation Remove unnecessary trait Basic example working Handle errors Handle dates Expand scope of MusicBrainz reference Type the musicbrainz refs Explicit constructors for str Handle MBIDs for albums Add search to the API Handle primary and secondary types Simplify AlbumDate Passing unit tests Tests pass Prevent compiler/clippy warnings Finish unit tests Clippy Remove old deserialize version
121 lines
3.3 KiB
Rust
121 lines
3.3 KiB
Rust
use std::{
|
|
collections::HashSet,
|
|
fs,
|
|
path::PathBuf,
|
|
sync::{Arc, Mutex},
|
|
};
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use musichoard::{
|
|
external::library::beets::{executor::BeetsLibraryProcessExecutor, BeetsLibrary},
|
|
interface::library::{Field, ILibrary, Item, Query},
|
|
};
|
|
|
|
use crate::library::testmod::LIBRARY_ITEMS;
|
|
|
|
pub static BEETS_TEST_CONFIG_PATH: Lazy<PathBuf> =
|
|
Lazy::new(|| fs::canonicalize("./tests/files/library/config.yml").unwrap());
|
|
|
|
pub static BEETS_EMPTY_CONFIG: Lazy<Arc<Mutex<BeetsLibrary<BeetsLibraryProcessExecutor>>>> =
|
|
Lazy::new(|| {
|
|
Arc::new(Mutex::new(BeetsLibrary::new(
|
|
BeetsLibraryProcessExecutor::default(),
|
|
)))
|
|
});
|
|
|
|
pub static BEETS_TEST_CONFIG: Lazy<Arc<Mutex<BeetsLibrary<BeetsLibraryProcessExecutor>>>> =
|
|
Lazy::new(|| {
|
|
Arc::new(Mutex::new(BeetsLibrary::new(
|
|
BeetsLibraryProcessExecutor::default().config(Some(&*BEETS_TEST_CONFIG_PATH)),
|
|
)))
|
|
});
|
|
|
|
#[test]
|
|
fn test_no_config_list() {
|
|
let beets_arc = BEETS_EMPTY_CONFIG.clone();
|
|
let beets = &mut beets_arc.lock().unwrap();
|
|
|
|
let output = beets.list(&Query::new()).unwrap();
|
|
|
|
let expected: Vec<Item> = vec![];
|
|
assert_eq!(output, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_config() {
|
|
let mut beets = BeetsLibrary::new(BeetsLibraryProcessExecutor::default().config(Some(
|
|
&PathBuf::from("./tests/files/library/config-does-not-exist.yml"),
|
|
)));
|
|
|
|
let result = beets.list(&Query::new());
|
|
assert!(result.is_err());
|
|
assert!(!result.unwrap_err().to_string().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn test_full_list() {
|
|
let beets_arc = BEETS_TEST_CONFIG.clone();
|
|
let beets = &mut beets_arc.lock().unwrap();
|
|
|
|
let output = beets.list(&Query::new()).unwrap();
|
|
let expected: Vec<Item> = LIBRARY_ITEMS.to_owned();
|
|
|
|
let output: HashSet<_> = output.iter().collect();
|
|
let expected: HashSet<_> = expected.iter().collect();
|
|
assert_eq!(output, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn test_album_artist_query() {
|
|
let beets_arc = BEETS_TEST_CONFIG.clone();
|
|
let beets = &mut beets_arc.lock().unwrap();
|
|
|
|
let output = beets
|
|
.list(Query::new().include(Field::AlbumArtist(String::from("Аркона"))))
|
|
.unwrap();
|
|
|
|
let expected: Vec<Item> = LIBRARY_ITEMS
|
|
.iter()
|
|
.filter(|it| it.album_artist == "Аркона")
|
|
.cloned()
|
|
.collect();
|
|
assert_eq!(output, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn test_album_title_query() {
|
|
let beets_arc = BEETS_TEST_CONFIG.clone();
|
|
let beets = &mut beets_arc.lock().unwrap();
|
|
|
|
let output = beets
|
|
.list(Query::new().include(Field::AlbumTitle(String::from("Slovo"))))
|
|
.unwrap();
|
|
|
|
let expected: Vec<Item> = LIBRARY_ITEMS
|
|
.iter()
|
|
.filter(|it| it.album_title == "Slovo")
|
|
.cloned()
|
|
.collect();
|
|
assert_eq!(output, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn test_exclude_query() {
|
|
let beets_arc = BEETS_TEST_CONFIG.clone();
|
|
let beets = &mut beets_arc.lock().unwrap();
|
|
|
|
let output = beets
|
|
.list(Query::new().exclude(Field::AlbumArtist(String::from("Аркона"))))
|
|
.unwrap();
|
|
let expected: Vec<Item> = LIBRARY_ITEMS
|
|
.iter()
|
|
.filter(|it| it.album_artist != "Аркона")
|
|
.cloned()
|
|
.collect();
|
|
|
|
let output: HashSet<_> = output.iter().collect();
|
|
let expected: HashSet<_> = expected.iter().collect();
|
|
assert_eq!(output, expected);
|
|
}
|