2023-04-10 20:48:44 +02:00
|
|
|
use std::{
|
2023-05-21 22:28:51 +02:00
|
|
|
collections::HashSet,
|
2023-04-10 20:48:44 +02:00
|
|
|
fs,
|
2023-04-13 20:32:33 +02:00
|
|
|
path::PathBuf,
|
2023-04-10 20:48:44 +02:00
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
|
|
|
|
|
|
|
use once_cell::sync::Lazy;
|
2023-04-10 00:13:18 +02:00
|
|
|
|
|
|
|
use musichoard::{
|
|
|
|
library::{
|
2023-04-14 16:21:25 +02:00
|
|
|
beets::{executor::BeetsLibraryProcessExecutor, BeetsLibrary},
|
2023-05-20 00:02:39 +02:00
|
|
|
Field, ILibrary, Item, Query,
|
2023-04-10 00:13:18 +02:00
|
|
|
},
|
|
|
|
Artist,
|
|
|
|
};
|
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
use crate::testlib::COLLECTION;
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
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>>>> =
|
2023-04-13 15:29:14 +02:00
|
|
|
Lazy::new(|| {
|
|
|
|
Arc::new(Mutex::new(BeetsLibrary::new(
|
2023-04-14 16:21:25 +02:00
|
|
|
BeetsLibraryProcessExecutor::default(),
|
2023-04-13 15:29:14 +02:00
|
|
|
)))
|
|
|
|
});
|
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
pub static BEETS_TEST_CONFIG: Lazy<Arc<Mutex<BeetsLibrary<BeetsLibraryProcessExecutor>>>> =
|
2023-04-13 15:29:14 +02:00
|
|
|
Lazy::new(|| {
|
|
|
|
Arc::new(Mutex::new(BeetsLibrary::new(
|
2023-05-21 22:28:51 +02:00
|
|
|
BeetsLibraryProcessExecutor::default().config(Some(&*BEETS_TEST_CONFIG_PATH)),
|
2023-04-13 15:29:14 +02:00
|
|
|
)))
|
|
|
|
});
|
2023-04-10 20:48:44 +02:00
|
|
|
|
2023-05-20 00:02:39 +02:00
|
|
|
fn artist_to_items(artist: &Artist) -> Vec<Item> {
|
|
|
|
let mut items = vec![];
|
|
|
|
|
|
|
|
for album in artist.albums.iter() {
|
|
|
|
for track in album.tracks.iter() {
|
|
|
|
items.push(Item {
|
|
|
|
album_artist: artist.id.name.clone(),
|
|
|
|
album_year: album.id.year,
|
|
|
|
album_title: album.id.title.clone(),
|
|
|
|
track_number: track.id.number,
|
|
|
|
track_title: track.id.title.clone(),
|
|
|
|
track_artist: track.artist.clone(),
|
|
|
|
track_format: track.quality.format,
|
|
|
|
track_bitrate: track.quality.bitrate,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
items
|
|
|
|
}
|
|
|
|
|
|
|
|
fn artists_to_items(artists: &[Artist]) -> Vec<Item> {
|
|
|
|
let mut items = vec![];
|
|
|
|
for artist in artists.iter() {
|
|
|
|
items.append(&mut artist_to_items(artist));
|
|
|
|
}
|
|
|
|
items
|
|
|
|
}
|
|
|
|
|
2023-04-10 00:13:18 +02:00
|
|
|
#[test]
|
|
|
|
fn test_no_config_list() {
|
2023-04-10 20:48:44 +02:00
|
|
|
let beets_arc = BEETS_EMPTY_CONFIG.clone();
|
|
|
|
let beets = &mut beets_arc.lock().unwrap();
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2023-04-14 10:24:24 +02:00
|
|
|
let output = beets.list(&Query::new()).unwrap();
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2023-05-20 00:02:39 +02:00
|
|
|
let expected: Vec<Item> = vec![];
|
2023-04-10 00:13:18 +02:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
2023-04-13 20:32:33 +02:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_config() {
|
2023-04-14 16:21:25 +02:00
|
|
|
let mut beets = BeetsLibrary::new(BeetsLibraryProcessExecutor::default().config(Some(
|
2023-04-13 20:32:33 +02:00
|
|
|
&PathBuf::from("./tests/files/library/config-does-not-exist.yml"),
|
|
|
|
)));
|
|
|
|
|
2023-04-14 10:24:24 +02:00
|
|
|
let result = beets.list(&Query::new());
|
2023-04-13 20:32:33 +02:00
|
|
|
assert!(result.is_err());
|
|
|
|
assert!(!result.unwrap_err().to_string().is_empty());
|
|
|
|
}
|
|
|
|
|
2023-04-10 00:13:18 +02:00
|
|
|
#[test]
|
|
|
|
fn test_full_list() {
|
2023-04-10 20:48:44 +02:00
|
|
|
let beets_arc = BEETS_TEST_CONFIG.clone();
|
|
|
|
let beets = &mut beets_arc.lock().unwrap();
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2023-04-14 10:24:24 +02:00
|
|
|
let output = beets.list(&Query::new()).unwrap();
|
2023-05-20 00:02:39 +02:00
|
|
|
let expected: Vec<Item> = artists_to_items(&COLLECTION);
|
2023-05-21 22:28:51 +02:00
|
|
|
|
|
|
|
let output: HashSet<_> = output.iter().collect();
|
|
|
|
let expected: HashSet<_> = expected.iter().collect();
|
2023-04-10 00:13:18 +02:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_album_artist_query() {
|
2023-04-10 20:48:44 +02:00
|
|
|
let beets_arc = BEETS_TEST_CONFIG.clone();
|
|
|
|
let beets = &mut beets_arc.lock().unwrap();
|
2023-04-10 00:13:18 +02:00
|
|
|
|
|
|
|
let output = beets
|
2023-04-14 10:24:24 +02:00
|
|
|
.list(Query::new().include(Field::AlbumArtist(String::from("Аркона"))))
|
2023-04-10 00:13:18 +02:00
|
|
|
.unwrap();
|
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
let expected: Vec<Item> = artists_to_items(&COLLECTION[4..5]);
|
2023-04-10 00:13:18 +02:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_album_title_query() {
|
2023-04-10 20:48:44 +02:00
|
|
|
let beets_arc = BEETS_TEST_CONFIG.clone();
|
|
|
|
let beets = &mut beets_arc.lock().unwrap();
|
2023-04-10 00:13:18 +02:00
|
|
|
|
|
|
|
let output = beets
|
2023-04-14 10:24:24 +02:00
|
|
|
.list(&Query::new().include(Field::AlbumTitle(String::from("Slovo"))))
|
2023-04-10 00:13:18 +02:00
|
|
|
.unwrap();
|
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
let expected: Vec<Item> = artists_to_items(&COLLECTION[4..5]);
|
2023-04-10 00:13:18 +02:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_exclude_query() {
|
2023-04-10 20:48:44 +02:00
|
|
|
let beets_arc = BEETS_TEST_CONFIG.clone();
|
|
|
|
let beets = &mut beets_arc.lock().unwrap();
|
2023-04-10 00:13:18 +02:00
|
|
|
|
|
|
|
let output = beets
|
2023-04-14 10:24:24 +02:00
|
|
|
.list(&Query::new().exclude(Field::AlbumArtist(String::from("Аркона"))))
|
2023-04-10 00:13:18 +02:00
|
|
|
.unwrap();
|
2023-05-21 22:28:51 +02:00
|
|
|
let expected: Vec<Item> = artists_to_items(&COLLECTION[..4]);
|
2023-04-10 00:13:18 +02:00
|
|
|
|
2023-05-21 22:28:51 +02:00
|
|
|
let output: HashSet<_> = output.iter().collect();
|
|
|
|
let expected: HashSet<_> = expected.iter().collect();
|
2023-04-10 00:13:18 +02:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|