//! Pure unit tests of the podcast store helpers (no database). use music_dht::podcast::ProgressStatus; use super::*; #[test] fn search_text_never_matches_across_title_and_description() { let norm = episode_search_norm("Hello, World!", Some("WORLD peace")); assert_eq!(norm, "hello world\nworld peace"); // A needle is itself normalized, so it never contains the separator. let needle = music_dht::normalize_name("world world"); assert!(!norm.contains(&needle)); assert!(norm.contains(&music_dht::normalize_name("WORLD PEACE"))); assert_eq!(episode_search_norm("Only", None), "only\n"); } #[test] fn progress_status_round_trips_through_sql() { for status in [ ProgressStatus::NotStarted, ProgressStatus::InProgress, ProgressStatus::Completed, ] { assert_eq!(status_from_sql(status_to_sql(status)), status); } // The CHECK constraint admits exactly these values. for sql in schema::CREATE_PODCAST_STORE { if sql.contains("CREATE TABLE IF NOT EXISTS furumusic__podcast_progress (") { for value in ["not_started", "in_progress", "completed"] { assert!(sql.contains(&format!("'{value}'"))); } } } } #[test] fn generations_beyond_bigint_are_rejected() { assert_eq!(generation_to_sql(0).unwrap(), 0); assert_eq!(generation_to_sql(i64::MAX as u64).unwrap(), i64::MAX); assert!(matches!( generation_to_sql(i64::MAX as u64 + 1), Err(PodcastStoreError::Invalid(_)) )); assert_eq!(generation_from_sql(-1), 0); } #[test] fn contract_numbers_outside_u32_are_dropped() { assert_eq!(u32_from_sql(Some(7)), Some(7)); assert_eq!(u32_from_sql(Some(-1)), None); assert_eq!(u32_from_sql(Some(i64::from(u32::MAX) + 1)), None); assert_eq!(u32_from_sql(None), None); } #[test] fn progress_columns_are_prefixed() { assert_eq!( progress_columns("g", "g_"), "g.episode_id AS g_episode_id, g.podcast_id AS g_podcast_id, \ g.position_ms AS g_position_ms, g.status AS g_status, \ g.generation AS g_generation, g.session_id AS g_session_id, \ g.hlc_ms AS g_hlc_ms, g.device_id AS g_device_id, g.op_id AS g_op_id" ); } #[test] fn queries_sort_undated_and_unnumbered_episodes_last() { for sort in [ EpisodeSort::NewestFirst, EpisodeSort::OldestFirst, EpisodeSort::EpisodeNumber, EpisodeSort::EpisodeNumberDesc, ] { let order = queries::order_by_for_tests(sort); assert!( order.starts_with("e.published_at_ms IS NULL") || order.starts_with("e.season IS NULL") ); assert!(order.ends_with("e.episode_id"), "{order}"); } }