summaryrefslogblamecommitdiff
path: root/tests/db.rs
blob: 16061d023b573c17f0838541d99a9ee09263a888 (plain) (tree)























































































































































































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
use std::{fs::{File, create_dir_all}, path::Path, sync::Once};

use modlist::{config::{Cfg, Apis}, db::{mods_insert, db_setup, mods_get_all_ids, mods_get_id, mods_remove, userlist_insert, lists_insert, userlist_get_all_ids, userlist_remove, mods_get_versions, userlist_get_applicable_versions, lists_remove, lists_get, lists_get_all_ids, userlist_get_all_current_version_ids, userlist_change_versions, s_userlist_update_download, userlist_get_all_downloads, config_change_current_list, config_get_current_list, s_config_update_version, s_config_create_version, s_config_get_version}, Modloader, List};

static INIT: Once = Once::new();

fn setup() -> Cfg {
    let db_pathstr = "./test_tmp/db";

    let config = Cfg { data: String::from(db_pathstr), downloads: String::from("-"), clean_remove: false, apis: Apis { modrinth: String::from("-") } };
    
    INIT.call_once(|| {
        let db_path = Path::new(db_pathstr);
        create_dir_all(db_path).unwrap();
        let db_filestr = format!("{}/data.db", db_pathstr);
        File::create(db_filestr).unwrap();
        println!("INIT TEST DB");
        db_setup(config.clone()).unwrap();
    });
    config
}

#[test]
fn test_mods_insert() {
    let config = setup();

    mods_insert(config.clone(), String::from("I"), String::from("INSERT_TEST"), vec![String::from("INSERT_VER1"), String::from("INSERT_VER2")]).unwrap();
    let ids = mods_get_all_ids(config).unwrap();

    assert!(ids.contains(&String::from("I")));
}

#[test]
fn test_mods_get_all_ids() {
    let config = setup();

    mods_insert(config.clone(), String::from("GAI1"), String::from("GETALLIDS_TEST1"), vec![String::from("GAI1_VER1"), String::from("GAI1_VER2")]).unwrap();
    mods_insert(config.clone(), String::from("GAI2"), String::from("GETALLIDS_TEST2"), vec![String::from("GAI2_VER1"), String::from("GAI2_VER2")]).unwrap();
    let ids = mods_get_all_ids(config).unwrap();

    assert!(ids.contains(&String::from("GAI1")));
    assert!(ids.contains(&String::from("GAI2")));
}

#[test]
fn test_mods_get_id() {
    let config = setup();

    mods_insert(config.clone(), String::from("GI"), String::from("GETID_TEST"), vec![String::from("GI_VER1"), String::from("GI_VER2")]).unwrap();
}

#[test]
fn test_mods_remove() {
    let config = setup();

    mods_insert(config.clone(), String::from("R"), String::from("REMOVE_TEST"), vec![String::from("R_VER1"), String::from("R_VER2")]).unwrap();
    let ids = mods_get_all_ids(config.clone()).unwrap();
    assert!(ids.contains(&String::from("R")));
    mods_remove(config.clone(), String::from("R")).unwrap();
    assert!(mods_get_id(config, String::from("REMOVE_TEST")).is_err());
}

#[test]
fn test_mods_get_versions() {
    let config = setup();
    
    mods_insert(config.clone(), String::from("M_GVs1"), String::from("M_GVs_TEST1"), vec![String::from("M_GVs1_VER1"), String::from("M_GVs1_VER2")]).unwrap();
    mods_insert(config.clone(), String::from("M_GVs2"), String::from("M_GVs_TEST2"), vec![String::from("M_GVs2_VER1"), String::from("M_GVs2_VER2")]).unwrap();
    let versions = mods_get_versions(config, vec![String::from("M_GVs1"), String::from("M_GVs2")]).unwrap();

    assert!(versions.contains(&modlist::db::DBModlistVersions { mod_id: String::from("M_GVs1"), versions: String::from("M_GVs1_VER1|M_GVs1_VER2") }));
    assert!(versions.contains(&modlist::db::DBModlistVersions { mod_id: String::from("M_GVs2"), versions: String::from("M_GVs2_VER1|M_GVs2_VER2") }));
}

//user_list
#[test]
fn test_userlist_insert() {
    let config = setup();
    
    lists_insert(config.clone(), String::from("UL_I_LIST"), String::from("UL_I_MC"), Modloader::Fabric).unwrap();
    userlist_insert(config, String::from("UL_I_LIST"), String::from("UL_I"), String::from("UL_I_VER1"), vec![String::from("UL_I_VER1"), String::from("UL_I_VER2")], String::from("localhost:8080/dl/UL_I_VER1.test")).unwrap();
}

#[test]
fn test_userlist_get_all_ids() {
    let config = setup();

    lists_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI_MC"), Modloader::Fabric).unwrap();
    userlist_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI1"), String::from("UL_GAI1_VER1"), vec![String::from("UL_GAI2_VER1"), String::from("UL_GAI1_VER2")], String::from("localhost:8080/dl/UL_GAI1_VER1.test")).unwrap();
    userlist_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI2"), String::from("UL_GAI2_VER1"), vec![String::from("UL_GAI2_VER1"), String::from("UL_GAI2_VER2")], String::from("localhost:8080/dl/UL_GAI2_VER1.test")).unwrap();
    let ids = userlist_get_all_ids(config, String::from("UL_GAI_LIST")).unwrap();
    
    assert!(ids.contains(&String::from("UL_GAI1")));
    assert!(ids.contains(&String::from("UL_GAI2")));
}

#[test]
fn test_userlist_remove() {
    let config = setup();

    lists_insert(config.clone(), String::from("UL_R_LIST"), String::from("UL_R_MC"), Modloader::Fabric).unwrap();
    userlist_insert(config.clone(), String::from("UL_R_LIST"), String::from("UL_R"), String::from("UL_R_VER1"), vec![String::from("UL_R_VER1"), String::from("UL_R_VER2")], String::from("localhost:8080/dl/UL_R_VER1.test")).unwrap();
    let ids = userlist_get_all_ids(config.clone(), String::from("UL_R_LIST")).unwrap();
    assert!(ids.contains(&String::from("UL_R")));
    userlist_remove(config.clone(), String::from("UL_R_LIST"), String::from("UL_R")).unwrap();
    assert!(userlist_get_all_ids(config, String::from("UL_R_LIST")).is_err())
}

#[test]
fn test_userlist_get_applicable_versions() {
    let config = setup();
    
    lists_insert(config.clone(), String::from("UL_GAV_LIST"), String::from("UL_GAV_MC"), Modloader::Fabric).unwrap();
    userlist_insert(config.clone(), String::from("UL_GAV_LIST"), String::from("UL_GAV"), String::from("UL_GAV_VER1"), vec![String::from("UL_GAV_VER1"), String::from("UL_GAV_VER2")], String::from("localhost:8080/dl/UL_GAV_VER1.test")).unwrap();
    assert_eq!(userlist_get_applicable_versions(config, String::from("UL_GAV_LIST"), String::from("UL_GAV")).unwrap(), String::from("UL_GAV_VER1|UL_GAV_VER2"));
}

#[test]
fn test_userlist_get_all_current_version_ids() {
    let config = setup();

    lists_insert(config.clone(), String::from("UL_GACVI_LIST"), String::from("UL_GACVI_MC"), Modloader::Fabric).unwrap();
    userlist_insert(config.clone(), String::from("UL_GACVI_LIST"), String::from("UL_GACVI1"), String::from("UL_GACVI1_VER1"), vec![String::from("UL_GACVI2_VER1"), String::from("UL_GACVI1_VER2")], String::from("localhost:8080/dl/UL_GACVI1_VER1.test")).unwrap();
    userlist_insert(config.clone(), String::from("UL_GACVI_LIST"), String::from("UL_GACVI2"), String::from("UL_GACVI2_VER1"), vec![String::from("UL_GACVI2_VER1"), String::from("UL_GACVI2_VER2")], String::from("localhost:8080/dl/UL_GACVI2_VER1.test")).unwrap();

    let ids = userlist_get_all_current_version_ids(config, String::from("UL_GACVI_LIST")).unwrap();

    assert!(ids.contains(&String::from("UL_GACVI1_VER1")));
    assert!(ids.contains(&String::from("UL_GACVI2_VER1")));
}

#[test]
fn test_userlist_change_versions() {
    let config = setup();

    lists_insert(config.clone(), String::from("UL_CV_LIST"), String::from("UL_CV_MC"), Modloader::Fabric).unwrap();
    userlist_insert(config.clone(), String::from("UL_CV_LIST"), String::from("UL_CV"), String::from("UL_CV_VER1"), vec![String::from("UL_CV_VER1"), String::from("UL_CV_VER2")], String::from("localhost:8080/dl/UL_CV_VER1.test")).unwrap();
    let versions = userlist_get_all_current_version_ids(config.clone(), String::from("UL_CV_LIST")).unwrap();
    assert!(versions.contains(&String::from("UL_CV_VER1")));

    userlist_change_versions(config.clone(), String::from("UL_CV_LIST"), String::from("UL_CV_VER3"), String::from("UL_CV_VER1|UL_CV_VER2|UL_CV_VER3"), String::from("localhost:8080/dl/UL_CV_VER3.test"), String::from("UL_CV")).unwrap();
    let versions = userlist_get_all_current_version_ids(config, String::from("UL_CV_LIST")).unwrap();
    assert!(!versions.contains(&String::from("UL_CV_VER1")));
    assert!(versions.contains(&String::from("UL_CV_VER3")));
}

#[test]
fn test_userlist_get_all_downloads() {
    let config = setup();

    lists_insert(config.clone(), String::from("UL_GAD_LIST"), String::from("UL_GAD_MC"), Modloader::Fabric).unwrap();
    userlist_insert(config.clone(), String::from("UL_GAD_LIST"), String::from("UL_GAD1"), String::from("UL_GAD1_VER1"), vec![String::from("UL_GAD1_VER1"), String::from("UL_GAD1_VER1")], String::from("localhost:8080/dl/UL_GAD1_VER1.test")).unwrap();
    userlist_insert(config.clone(), String::from("UL_GAD_LIST"), String::from("UL_GAD2"), String::from("UL_GAD2_VER1"), vec![String::from("UL_GAD2_VER1"), String::from("UL_GAD2_VER1")], String::from("localhost:8080/dl/UL_GAD2_VER1.test")).unwrap();
    let links = userlist_get_all_downloads(config, String::from("UL_GAD_LIST")).unwrap();

    assert!(links.contains(&String::from("localhost:8080/dl/UL_GAD1_VER1.test")));
    assert!(links.contains(&String::from("localhost:8080/dl/UL_GAD2_VER1.test")));
}


//lists
#[test]
fn test_lists_insert() {
    let config = setup();

    lists_insert(config, String::from("L_I_LIST"), String::from("L_I_MC"), Modloader::Fabric).unwrap();
}

#[test]
fn test_lists_remove() {
    let config = setup();

    lists_insert(config.clone(), String::from("L_R_LIST"), String::from("L_R_MC"), Modloader::Fabric).unwrap();
    lists_remove(config, String::from("L_R_LIST")).unwrap();
}

#[test]
fn test_lists_get() {
    let config = setup();

    lists_insert(config.clone(), String::from("L_G_LIST"), String::from("L_G_MC"), Modloader::Fabric).unwrap();

    assert_eq!(lists_get(config, String::from("L_G_LIST")).unwrap(), List { id: String::from("L_G_LIST"), mc_version: String::from("L_G_MC"), modloader: Modloader::Fabric });
}

#[test]
fn test_lists_get_all_ids() {
    let config = setup();

    lists_insert(config.clone(), String::from("L_GAI1_LIST"), String::from("L_GAI1_MC"), Modloader::Fabric).unwrap();
    lists_insert(config.clone(), String::from("L_GAI2_LIST"), String::from("L_GAI2_MC"), Modloader::Fabric).unwrap();
    let ids = lists_get_all_ids(config).unwrap();

    assert!(ids.contains(&String::from("L_GAI1_LIST")));
    assert!(ids.contains(&String::from("L_GAI2_LIST")));
}

//config
#[test]
fn test_config_change_current_list() {
    let config = setup();

    config_change_current_list(config, String::from("C_CCL_LIST")).unwrap();
}

#[test]
fn test_config_get_current_list() {
    let config = setup();

    config_change_current_list(config.clone(), String::from("C_GCL_LIST")).unwrap();
    assert_eq!(config_get_current_list(config).unwrap(), String::from("C_GCL_LIST"));
}

//setup
#[test]
fn test_s_userlist_update_download() {
    let config = setup();

    lists_insert(config.clone(), String::from("UL_UD_LIST"), String::from("UL_UD_MC"), Modloader::Fabric).unwrap();
    userlist_insert(config.clone(), String::from("UL_UD_LIST"), String::from("UL_UD"), String::from("UL_UD_VER1"), vec![String::from("UL_UD_VER1"), String::from("UL_UD_VER1")], String::from("localhost:8080/dl/UL_UD_VER1.test")).unwrap();
    s_userlist_update_download(config.clone(), String::from("UL_UD_LIST"), String::from("UL_UD"), String::from("localhost:8080/dl/UL_UD_VER1X.test")).unwrap();
    let links = userlist_get_all_downloads(config, String::from("UL_UD_LIST")).unwrap();

    assert!(links.contains(&String::from("localhost:8080/dl/UL_UD_VER1X.test")));
    assert!(!links.contains(&String::from("localhost:8080/dl/UL_UD_VER1.test")));
}

#[test]
fn test_s_config_create_version() {
    let config = setup();

    s_config_create_version(config).unwrap();
}

#[test]
fn test_s_config_update_version() {
    let config = setup();

    s_config_update_version(config, String::from("S_C_UV")).unwrap();
}

#[test]
fn test_s_config_get_version() {
    let config = setup();

    s_config_update_version(config.clone(), String::from("S_C_GV")).unwrap();
    assert_eq!(s_config_get_version(config).unwrap(), String::from("S_C_GV"));
}