Compare commits

..

2 commits

Author SHA1 Message Date
7589ec5051 fix: testfile paths 2023-04-23 15:04:20 +02:00
79ef071751 fix: add expect message to client construction 2023-04-22 11:59:32 +02:00
5 changed files with 35 additions and 14 deletions

2
.env.example Normal file
View file

@ -0,0 +1,2 @@
MUSIXMATCH_EMAIL=mail@example.com
MUSIXMATCH_PASSWORD=super-secret

View file

@ -49,6 +49,7 @@ env_logger = "0.10.0"
dotenvy = "0.15.5" dotenvy = "0.15.5"
tokio = { version = "1.20.0", features = ["macros"] } tokio = { version = "1.20.0", features = ["macros"] }
futures = "0.3.21" futures = "0.3.21"
path_macro = "1.0.0"
[profile.release] [profile.release]
strip = true strip = true

View file

@ -24,3 +24,12 @@ lyrics site/app. If you want to use Musixmatch data for this purpose,
you will have to give them money (see their you will have to give them money (see their
[commercial plans](https://developer.musixmatch.com/plans)) [commercial plans](https://developer.musixmatch.com/plans))
and use their [official API](https://developer.musixmatch.com/documentation). and use their [official API](https://developer.musixmatch.com/documentation).
## Development info
Running the tests requires Musixmatch credentials. The credentials are read
from the `MUSIXMATCH_EMAIL` and `MUSIXMATCH_PASSWORD` environment variables.
To make local development easier, I have included `dotenvy` to read the
credentials from an `.env` file. Copy the `.env.example` file
in the root directory, rename it to `.env` and fill in your credentials.

View file

@ -112,7 +112,7 @@ impl Musixmatch {
.gzip(true) .gzip(true)
.cookie_store(true) .cookie_store(true)
.build() .build()
.unwrap(); .expect("http client could not be constructed");
Self { Self {
inner: Arc::new(MusixmatchRef { inner: Arc::new(MusixmatchRef {

View file

@ -1,3 +1,6 @@
use std::path::{Path, PathBuf};
use path_macro::path;
use rstest::rstest; use rstest::rstest;
use time::macros::{date, datetime}; use time::macros::{date, datetime};
@ -19,7 +22,7 @@ fn init() {
.unwrap(); .unwrap();
} }
pub fn new_mxm() -> Musixmatch { fn new_mxm() -> Musixmatch {
Musixmatch::new( Musixmatch::new(
&std::env::var("MUSIXMATCH_EMAIL").unwrap(), &std::env::var("MUSIXMATCH_EMAIL").unwrap(),
&std::env::var("MUSIXMATCH_PASSWORD").unwrap(), &std::env::var("MUSIXMATCH_PASSWORD").unwrap(),
@ -27,6 +30,10 @@ pub fn new_mxm() -> Musixmatch {
) )
} }
fn testfile<P: AsRef<Path>>(name: P) -> PathBuf {
path!(env!("CARGO_MANIFEST_DIR") / "testfiles" / name)
}
mod album { mod album {
use super::*; use super::*;
use musixmatch_inofficial::models::AlbumType; use musixmatch_inofficial::models::AlbumType;
@ -679,7 +686,7 @@ mod track {
mod lyrics { mod lyrics {
use futures::stream::{self, StreamExt}; use futures::stream::{self, StreamExt};
use std::{fs::File, io::BufWriter, path::Path}; use std::{fs::File, io::BufWriter};
use super::*; use super::*;
@ -756,7 +763,7 @@ mod lyrics {
#[tokio::test] #[tokio::test]
async fn download_testdata() { async fn download_testdata() {
let json_path = Path::new("testfiles/lyrics.json"); let json_path = testfile("lyrics.json");
if json_path.exists() { if json_path.exists() {
return; return;
} }
@ -772,7 +779,7 @@ mod lyrics {
#[tokio::test] #[tokio::test]
async fn download_testdata_translation() { async fn download_testdata_translation() {
let json_path = Path::new("testfiles/translation.json"); let json_path = testfile("translation.json");
if json_path.exists() { if json_path.exists() {
return; return;
} }
@ -818,7 +825,7 @@ mod lyrics {
} }
mod subtitles { mod subtitles {
use std::{fs::File, io::BufWriter, path::Path}; use std::{fs::File, io::BufWriter};
use super::*; use super::*;
use musixmatch_inofficial::models::SubtitleFormat; use musixmatch_inofficial::models::SubtitleFormat;
@ -925,7 +932,7 @@ mod subtitles {
#[tokio::test] #[tokio::test]
async fn download_testdata() { async fn download_testdata() {
let json_path = Path::new("testfiles/subtitles.json"); let json_path = testfile("subtitles.json");
if json_path.exists() { if json_path.exists() {
return; return;
} }
@ -946,15 +953,17 @@ mod subtitles {
} }
mod translation { mod translation {
use std::{fs::File, io::BufReader, path::Path}; use std::{fs::File, io::BufReader};
use musixmatch_inofficial::models::{Lyrics, Subtitle, TranslationList, TranslationMap}; use musixmatch_inofficial::models::{Lyrics, Subtitle, TranslationList, TranslationMap};
use crate::testfile;
#[test] #[test]
fn translation_test() { fn translation_test() {
let lyrics_path = Path::new("testfiles/lyrics.json"); let lyrics_path = testfile("lyrics.json");
let subtitles_path = Path::new("testfiles/subtitles.json"); let subtitles_path = testfile("subtitles.json");
let translation_path = Path::new("testfiles/translation.json"); let translation_path = testfile("translation.json");
let lyrics: Lyrics = let lyrics: Lyrics =
serde_json::from_reader(BufReader::new(File::open(lyrics_path).unwrap())).unwrap(); serde_json::from_reader(BufReader::new(File::open(lyrics_path).unwrap())).unwrap();
@ -966,13 +975,13 @@ mod translation {
let t_map = TranslationMap::from(translations); let t_map = TranslationMap::from(translations);
let lyrics_trans = t_map.translate_lyrics(&lyrics.lyrics_body); let lyrics_trans = t_map.translate_lyrics(&lyrics.lyrics_body);
let expected_lyrics = std::fs::read_to_string("testfiles/translated_lyrics.txt").unwrap(); let expected_lyrics = std::fs::read_to_string(testfile("translated_lyrics.txt")).unwrap();
assert_eq!(lyrics_trans.trim(), expected_lyrics.trim()); assert_eq!(lyrics_trans.trim(), expected_lyrics.trim());
let subtitles_trans = t_map.translate_subtitles(&subtitle.to_lines().unwrap()); let subtitles_trans = t_map.translate_subtitles(&subtitle.to_lines().unwrap());
let expected_lrc = std::fs::read_to_string("testfiles/translated_subtitles.lrc").unwrap(); let expected_lrc = std::fs::read_to_string(testfile("translated_subtitles.lrc")).unwrap();
let expected_ttml = std::fs::read_to_string("testfiles/translated_subtitles.xml").unwrap(); let expected_ttml = std::fs::read_to_string(testfile("translated_subtitles.xml")).unwrap();
assert_eq!(subtitles_trans.to_lrc().trim(), expected_lrc.trim()); assert_eq!(subtitles_trans.to_lrc().trim(), expected_lrc.trim());
assert_eq!(subtitles_trans.to_ttml().trim(), expected_ttml.trim()); assert_eq!(subtitles_trans.to_ttml().trim(), expected_ttml.trim());