oxyromon 0.24.0

Rusty ROM OrgaNizer
use super::super::database::establish_connection;
use super::*;
use std::path::{Path, PathBuf};
use tempfile::{NamedTempFile, TempDir};
use tokio::fs;

#[tokio::test]
async fn test() {
    // given
    let _guard = MUTEX.lock().await;

    let test_directory = Path::new("tests");
    let progress_bar = ProgressBar::hidden();

    let db_file = NamedTempFile::new().unwrap();
    let pool = establish_connection(db_file.path().to_str().unwrap()).await;
    let mut connection = pool.acquire().await.unwrap();

    let tmp_directory = TempDir::new_in(test_directory).unwrap();
    set_tmp_directory(&mut connection, PathBuf::from(tmp_directory.path())).await;

    let nsz_path = tmp_directory.path().join("Test Game (USA).nsz");
    fs::copy(test_directory.join("Test Game (USA).nsz"), &nsz_path)
        .await
        .unwrap();
    let nsz = CommonRomfile::from_path(&nsz_path)
        .unwrap()
        .as_nsz()
        .unwrap();

    // when
    let (sha1, sha1_size) = nsz
        .get_hash_and_size(&mut connection, &progress_bar, 1, 1, &HashAlgorithm::Sha1)
        .await
        .unwrap();
    let (md5, md5_size) = nsz
        .get_hash_and_size(&mut connection, &progress_bar, 1, 1, &HashAlgorithm::Md5)
        .await
        .unwrap();

    // then
    // an NSZ is matched against the DB by its decompressed NSP
    assert_eq!(sha1, "8dfa6a7045f8bd56f8c5d33c20a48b07659ecb76");
    assert_eq!(md5, "bbddf6ccb6451061eaa16c2de22dc469");
    assert_eq!(sha1_size, 40946);
    assert_eq!(md5_size, 40946);
}