Skip to content

Commit

Permalink
Fixed tests for /search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
agersant committed Sep 22, 2024
1 parent a683dfd commit 5babd5d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
### Server

- API version is now 8.0
- Clients are now expected to send their preferred API major version in a `Accept-Version` header. Omitting this defaults to `7`, but will become an error in future Polaris releases.
- Clients are now expected to send their preferred API major version in a `Accept-Version` header. Omitting this currently defaults to `7`, but will become an error in future Polaris releases. Support for API version 7 will be removed in a future release.
- Most API responses now support gzip compression.
- Added support for multi-value metadata for the following fields: `artist`, `album artist`, `genre`, `label`, `lyricist` and `composer`.
- Added support for multi-value metadata for the following fields: `artist`, `album artist`, `composer`, `genre`, `label` and `lyricist`.
- The response format of the `/browse`, `/flatten` and `/get_playlist` endpoints has been modified.
- Added new endpoints to query albums and artists
- Added new endpoints to query albums and artists.
- The `/random` and `/recent` albums are deprecated in favor of `/albums/random` and `/albums/recent`. These endpoints now have optional parameters for RNG seeding and pagination.
- The `/search/<query>` endpoint now requires a non-empty query (`/search/` now returns HTTP status code 404, regardless of API version).
- The response format of the `/search/<query>` endpoint has changed.
- Added a new `/get_songs` endpoint which returns song metadata in bulk.
- Added a new `/peaks` endpoint which returns audio signal peaks that can be used to draw waveform visualizations.
- The `/thumbnail` endpoint supports a new size labeled `tiny` which returns 40x40px images.
Expand Down
14 changes: 12 additions & 2 deletions src/server/axum/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,24 @@ async fn get_search(
_auth: Auth,
api_version: APIMajorVersion,
State(index_manager): State<index::Manager>,
Path(query): Path<String>, // TODO return dto::SongList
Path(query): Path<String>,
) -> Response {
let paths = match index_manager.search(query).await {
Ok(f) => f,
Err(e) => return APIError::from(e).into_response(),
};
let song_list = make_song_list(paths, &index_manager).await;
song_list_to_response(song_list, api_version)
match api_version {
APIMajorVersion::V7 => Json(
song_list
.paths
.iter()
.map(|p| dto::v7::CollectionFile::Song(p.into()))
.collect::<Vec<_>>(),
)
.into_response(),
APIMajorVersion::V8 => Json(song_list).into_response(),
}
}

async fn get_playlists(
Expand Down
16 changes: 2 additions & 14 deletions src/server/dto/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl VecExt for Vec<String> {
}
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Song {
pub path: PathBuf,
pub track_number: Option<i64>,
Expand All @@ -300,19 +300,7 @@ impl From<&PathBuf> for Song {
fn from(path: &PathBuf) -> Self {
Self {
path: path.clone(),
track_number: None,
disc_number: None,
title: None,
artist: None,
album_artist: None,
year: None,
album: None,
artwork: None,
duration: None,
lyricist: None,
composer: None,
genre: None,
label: None,
..Default::default()
}
}
}
Expand Down
42 changes: 29 additions & 13 deletions src/server/test/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,38 +278,47 @@ async fn recent_golden_path_api_v7() {
#[tokio::test]
async fn search_requires_auth() {
let mut service = ServiceType::new(&test_name!()).await;
let request = protocol::search::<V8>("");
let request = protocol::search::<V8>("rhapsody");
let response = service.fetch(&request).await;
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn search_without_query() {
async fn search_with_query() {
let mut service = ServiceType::new(&test_name!()).await;
service.complete_initial_setup().await;
service.login_admin().await;
service.index().await;
service.login().await;

let request = protocol::search::<V8>("");
let response = service
.fetch_json::<_, Vec<dto::BrowserEntry>>(&request)
.await;
assert_eq!(response.status(), StatusCode::OK);
let request = protocol::search::<V8>("door");
let response = service.fetch_json::<_, dto::SongList>(&request).await;
let songs = response.body();

let path: PathBuf = [
TEST_MOUNT_NAME,
"Khemmis",
"Hunted",
"04 - Beyond The Door.mp3",
]
.iter()
.collect();
assert_eq!(songs.paths, vec![path]);
}

#[tokio::test]
async fn search_with_query() {
async fn search_with_query_v7() {
let mut service = ServiceType::new(&test_name!()).await;
service.complete_initial_setup().await;
service.login_admin().await;
service.index().await;
service.login().await;

let request = protocol::search::<V8>("door");
let request = protocol::search::<V7>("door");
let response = service
.fetch_json::<_, Vec<dto::BrowserEntry>>(&request)
.fetch_json::<_, Vec<dto::v7::CollectionFile>>(&request)
.await;
let results = response.body();
assert_eq!(results.len(), 1);
let songs = response.body();

let path: PathBuf = [
TEST_MOUNT_NAME,
Expand All @@ -319,5 +328,12 @@ async fn search_with_query() {
]
.iter()
.collect();
assert_eq!(results[0].path, path);

assert_eq!(
*songs,
vec![dto::v7::CollectionFile::Song(dto::v7::Song {
path,
..Default::default()
})]
);
}

0 comments on commit 5babd5d

Please sign in to comment.