use serde::Deserialize;
use crate::{Group, Torrent};
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TorrentResponse {
pub group: Group,
pub torrent: Torrent,
}
#[cfg(feature = "mock")]
impl TorrentResponse {
#[must_use]
pub fn mock() -> Self {
Self {
group: Group::mock(),
torrent: Torrent::mock(),
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
mod tests {
use super::*;
const OPS_RESPONSE: &str = include_str!("../tests/fixtures/torrent_response_ops.json");
const RED_RESPONSE: &str = include_str!("../tests/fixtures/torrent_response_red.json");
const MINIMAL_RESPONSE: &str = include_str!("../tests/fixtures/torrent_response_minimal.json");
#[test]
fn deserialize_ops_torrent_response() {
let response: TorrentResponse = serde_json::from_str(OPS_RESPONSE).unwrap();
assert_eq!(response.torrent.trumpable, Some(false));
assert!(response.torrent.lossy_web_approved.is_none());
assert!(response.torrent.lossy_master_approved.is_none());
assert!(response.torrent.is_neutralleech.is_none());
assert!(response.torrent.is_freeload.is_none());
assert!(response.group.bb_body.is_none());
assert_eq!(response.group.id, 16352);
assert_eq!(response.group.name, "Test Album");
assert_eq!(response.torrent.id, 1_520_678);
assert_eq!(response.torrent.media, "WEB");
assert_eq!(response.torrent.format, "FLAC");
assert_eq!(response.torrent.encoding, "Lossless");
}
#[test]
fn deserialize_red_torrent_response() {
let response: TorrentResponse = serde_json::from_str(RED_RESPONSE).unwrap();
assert!(response.group.bb_body.is_some());
assert_eq!(response.torrent.trumpable, Some(false));
assert_eq!(response.torrent.lossy_web_approved, Some(false));
assert_eq!(response.torrent.lossy_master_approved, Some(false));
assert_eq!(response.torrent.is_neutralleech, Some(false));
assert_eq!(response.torrent.is_freeload, Some(false));
assert_eq!(response.torrent.has_snatched, Some(false));
assert_eq!(response.group.id, 8126);
assert_eq!(response.group.name, "Test Album");
assert_eq!(response.torrent.id, 12483);
assert_eq!(response.torrent.media, "WEB");
}
#[test]
fn deserialize_minimal_torrent_response() {
let response: TorrentResponse = serde_json::from_str(MINIMAL_RESPONSE).unwrap();
assert_eq!(response.group.id, 3);
assert_eq!(response.group.name, "Minimal Album");
assert!(response.group.tags.is_empty());
assert!(response.group.music_info.is_none());
assert_eq!(response.torrent.id, 3000);
assert_eq!(response.torrent.format, "MP3");
assert!(!response.torrent.remastered);
assert!(response.torrent.scene);
}
#[test]
fn deserialize_torrent_music_info() {
let response: TorrentResponse = serde_json::from_str(RED_RESPONSE).unwrap();
let music_info = response.group.music_info.expect("music_info should exist");
assert_eq!(music_info.artists.len(), 1);
assert_eq!(music_info.artists[0].name, "Test Artist");
}
#[test]
fn deserialize_torrent_numeric_fields() {
let response: TorrentResponse = serde_json::from_str(RED_RESPONSE).unwrap();
assert_eq!(response.torrent.file_count, 2);
assert_eq!(response.torrent.size, 30_487_522);
assert_eq!(response.torrent.seeders, 15);
assert_eq!(response.torrent.leechers, 0);
assert_eq!(response.torrent.snatched, 55);
assert_eq!(response.group.year, 2015);
assert_eq!(response.torrent.remaster_year, Some(2015));
}
#[test]
fn deserialize_torrent_file_list() {
let response: TorrentResponse = serde_json::from_str(OPS_RESPONSE).unwrap();
assert!(response.torrent.file_list.contains("Track.flac"));
let flacs = response.torrent.get_flacs();
assert_eq!(flacs.len(), 1);
}
}